long wires connecting a switch to a processor pin - Page 1
Forum section: electronics / projects

author: trevwhite
author_rank: Frequent Contributor
timestamp: November 14, 2012, 11:10:41 am
content: Hi all.I want to make a small alarm board. I want to have some reed switches connected. They will be maybe 30m to 40m away from the processor board. I was wondering what the best way to filter the lines into the processor board is. The wires could pass by all sorts of other cables. Mains cables, ethernet, phone lines...I dont know. All sorts really.My thought was that I would connect one side of the reed switch to the 0v and the other to the processor pin. To limit input spike currents I added a series resistor to the input. I also added a capacitor across the processor port pin and 0v. I was wondering if there are better ways to filter the inputs lines ?Any thoughts are appreciated.ThanksTrev

author: Psi
author_rank: Super Contributor
timestamp: November 14, 2012, 11:45:27 am
content: If the switch is connected between ground and the mcu input you must be relying on the mcu pullup.Those pullups are quite week, around ~50K.I would add your own external pullup, maybe 1K.Use the resistor and capacitor you have added to form a RC filter and set its cutoff frequency to filter any signal changes which are faster than the actual signal your trying to detect.So for example if it was a button which a human presses you might filter to 20Hz (50ms). That way it will block higher frequencies from changing the mcu input.It's also a good idea to put a 20pF ceramic cap across it somewhere, just to help block RFIf you're worried about high voltage getting onto the cable you can add a TVS diode across it.They block voltage higher than their rated voltage and can absorb really huge spikes.So if the MCU is 5V you might use a 5.5V TVS diode.

author: SeanB
author_rank: Super Contributor
timestamp: November 14, 2012, 11:50:42 am
content: Add a sacrificial hex TTL buffer to the inputs. That way you only need to change a 14 pin DIL package if something happens. You need a 2k2 pullup resistor along with the capacitor and series 330R resistor. Use TTL only, not CMOS or HCMOS, as they tend to be more robust. You will need an input diode as well for each input, one to the supply rail and one to ground.

author: trevwhite
author_rank: Frequent Contributor
timestamp: November 14, 2012, 12:16:53 pm
content: Hi, thanks for the responses. All really useful.As long as the series resistor is high enough, can I not use the internal diode clamps of the processor. They must be good for a few mA.With regard to weak pullups. What kind of signals can drag a pullup of 50K down? The processor can debounce some noise. Capacitors can filter as well. Any details on problems with weak pullups?Thanks.Trev

author: Psi
author_rank: Super Contributor
timestamp: November 14, 2012, 12:29:27 pm
content: You can, but when it comes to protecting a circuit you should keep the built-in mcu protection as a last resort.

author: trevwhite
author_rank: Frequent Contributor
timestamp: November 14, 2012, 02:31:45 pm
content: Why should we keep the built in diodes as a last resort? Why not use them as a component? Are they unreliable? Not made for repeat operation?I was originally wondering if the noise from the 0v line would find its way into the processor. The idea of placing a small capacitor of 20pF across the inputs sounds good. Hopefully that can stop the high frequency noise.Trev

author: SeanB
author_rank: Super Contributor
timestamp: November 14, 2012, 02:39:33 pm
content: Built in diodes are a last resort component, there to prevent damage from any accidental transients during manufacture and assembly. Not intended for use in normal operation. For that you have to add extra protection, otherwise you get strange long tem problems, like increased supply current, increased leakage current and odd latch up issues.

author: trevwhite
author_rank: Frequent Contributor
timestamp: November 14, 2012, 03:12:27 pm
content: I remember a Microchip app note that uses the internal diode for zero crossing detection. Granted they had a few Meg resistors connected in series with the mains input and the io pin. So there must be some levels of reliable operation to it. Would be nice to know what they are.

author: SeanB
author_rank: Super Contributor
timestamp: November 14, 2012, 03:59:07 pm
content: If you can guarantee the current is under around 10uA then the internal diodes are fine. In using them for protection you may get 10mA currents through them, which will not be good for them, as it will cause degradation with time.YMMV, and unless you are prepared to toss the unit when it fails, and failure will not cause danger to you or other damage then use the internal diodes. Have a look at how alarm systems do it, they use series resistors, diodes and capacitors on the inputs, so that the system will operate reliably for 10 years plus. The digital inputs and outputs are via separate IC devices, just to protect the main microcontroller.

author: trevwhite
author_rank: Frequent Contributor
timestamp: November 14, 2012, 04:11:48 pm
content: SeanB, I appreciate you taking the time to reply. ThanksTrev

author: Psi
author_rank: Super Contributor
timestamp: November 14, 2012, 10:33:49 pm
content: If you could see the physical size of those internal protection diodes you would understand

author: nctnico
author_rank: Super Contributor
timestamp: November 15, 2012, 04:36:58 pm
content: Quote from: SeanB on November 14, 2012, 11:50:42 amI always use a 74HC14 for digital inputs. It has extra hysteresis. In the software I use a counter to see if the input is stable for at least 100ms. Never ever use an interrupt for I/O because that may cause excessive CPU load when the input is bounching for some reason.

author: Kremmen
author_rank: Super Contributor
timestamp: November 15, 2012, 05:12:34 pm
content: Never say never. I/O interrupts are a powerful tool if used correctly. You are right that when used naively a GPIO interrupt can overwhelm a processor but there are straightforward defenses against that. For instance it is usually trivially easy to disable the particular interrupt once it has been triggered, and re-enable it only after a delay, whatever makes sense in the context.Also it is bad design to let interference in on such a level that it will confuse the system inputs. One simply needs to design such noise away. If not, the input makes hardly sense even without interrupts.

author: ptricks
author_rank: Frequent Contributor
timestamp: November 16, 2012, 01:33:45 am
content: I don't see the main problem as damage to the pin but rather the noise you will inject into the line causing false readings. Depending on the length of wire , a +5V logic based approach may have problems. Usually when you run a long wire you want to be able to keep as wide a difference between High and Low as possible and the more noise and longer the wire the harder that gets. A couple options.10-15V logic level parts, much wider difference between what is High and Low, but require a 10V supply, this is what alarm systems do but they use 12V or 24V.Use AND gates at the MCU. Connect two wires on the same points at the switch and then to an AND gate at the MCU, if either wire picks up noise causing it to appear High it will not trigger unless both wires appear High.1wire setup . You can use the Maxim 1wire devices to place the decision making parts at the switch and then just send back data and power over the same wire. Data that is corrupt would be rejected so no false triggers. They have very cheap easy to use devices.http://www.maximintegrated.com/datasheet/index.mvp/id/7263Device requires only +5V and ground, and uses the same +5V for data using the parasite power mode. You can also put many of these on each wire so you only use 1 processor pin for all switches. Connected to the power through your reed switch so that the 1wire device always answers a query, contact breaks and you no longer get a reply means switch broken. In the processor a simple routine of polling all the devices connected can tell when a switch is broken. This technique is actually used in a lot of modern alarms. In the past an intruder could jumper the two wires going to the switch to defeat the reed switch , or the really smart one would probe the actual wires checking voltage levels in case the circuit was a normally open type. By using the alarm wire for data and not just voltage it makes it really hard to tamper . Cutting or using a jumper will trigger the data switch type alarms.

author: Baliszoft
author_rank: Frequent Contributor
timestamp: November 16, 2012, 09:32:28 am
content: Quote from: trevwhite on November 14, 2012, 03:12:27 pmMaximum input clamp currents are usually specified in the datasheet for the particular device.

author: nctnico
author_rank: Super Contributor
timestamp: November 19, 2012, 09:28:05 am
content: Quote from: Kremmen on November 15, 2012, 05:12:34 pmThat won't work at all. A glitch will trigger the interrupt but you don't know whether the event was long enough to be a valid key press. The only way to do it right is polling or external de-glitching / filtering.

author: Kremmen
author_rank: Super Contributor
timestamp: November 19, 2012, 12:04:26 pm
content: Huh? Sure it will work. Known fact. Or why else have the feature in the first place?Firstly, it is you who mentions keypress input, not me. I haven't said keypresses should be detected by GPIO interrupts. They can but if it is the best way to do so, depends. I do that differently.Secondly, a glitch is an error signal that you don't want to let into the system. So do as you say yourself: de-glitch/filter/protect the inputs to the degree needed.For maximum determinism in external switch type event detection and timing i do recommend GPIO interrupts but they must not be implemented naively.And just to remind: my original reply was a response to the recommendation to _never_ use GPIO interrupts. Not keypress reading specifically.

author: nctnico
author_rank: Super Contributor
timestamp: November 19, 2012, 04:37:31 pm
content: Quote from: Kremmen on November 19, 2012, 12:04:26 pmI've seen that fail catastrophically (*). The thing is that you can either do debouncing in hardware or software. Proper debouncing in hardware is an art in its own. If you have a controller then you can do the debouncing in software and save some costs on the hardware.QuoteThe thread is about connecting a switch...(*) One of the companies I worked for had designed a video-door phone. The hardware designer used a 1.8V I/O pin with just some RC filtering to connect the doorbell. At some point they used a different circuit which had output levels close to the threshold which slowed the entire device down to a grinding halt. Then the software engineer got mad at the hardware engineer because he got too many interrupts. The hardware engineer said he expected the filtering to be done in software. I suddenly understood why the product was behaving so bad in the field. The software engineer used your exact argument: if there are GPIO interrupts I should be able to use them for whatever purpose...Fortunately it wasn't my project

author: Kremmen
author_rank: Super Contributor
timestamp: November 19, 2012, 05:58:45 pm
content: Now you are playing the devil's advocate. Anything fails if you botch the implementation. I haven't said GPIO interrupts must be usable for any purpose just like that, without any considerations. What i have said is that used correctly they can be a powerful tool. Emphasis is on the word "correctly".You make these special cases of yours into generalizations that just don't apply like that. Incompetent design or design coordination is not a valid excuse to dismiss an entire design principle or feature. You maybe don't want to apply the principle to a particular design problem if you don't know how. The other alternative is to improve your design process.As for debouncing contacts. Been there, done it in software and hardware. In more ways and for more kinds of contacts than i can easily remember, not just the kind you push with a sweaty finger. That was not the issue here, as i wasn't specifically commenting debouncing, but use of GPIO interrupts in general.Quote from: nctnico on November 19, 2012, 04:37:31 pmSo what. I commented a specific claim specifically - do read the words.

author: poptones
author_rank: Frequent Contributor
timestamp: November 19, 2012, 07:30:02 pm
content: Wow you guys. All this over connecting an input signal to a processor through a very long wire? They solved this dilemna in the 50's - it's called a current loop.

author: Kremmen
author_rank: Super Contributor
timestamp: November 19, 2012, 07:35:47 pm
content: They probably did but then we were already a long way off that track.