Door sensors randomly triggering on a Raspberry Pi

Thread Starter

Zorac

Joined Oct 9, 2016
23
So I have two door sensors and two gate sensors (magnetic) on a raspberry pi. The problem is that they periodically trip without the doors/gates being opened so I assume I am picking up noise on the circuit (roughly once a week on the longest run, longer on the shorter runs). The sensors are wired directly to the pi and are being fed with 3.3v with a 4.7k pull down resistor. The runs are pretty long, up to 50', and I am just using some thermostat wire on two runs, and some old phone wire on the other two runs. To fix the problem i wrote some code which reads the sensors twice with a 0.3sec delay between the readings and the readings must be the same or it ignores the readings until its time to read again (in another 0.3seconds) which has resolved the issue.

My questions are:
1. Should I be fixing the wiring to prevent the noise (and how) or is the coding an acceptable solution? What is best practice?
2. If I wanted to track the noise, how would I do that or see it? My volt meter doesn't see the noise on the circuit when I measure voltage.
 

Thread Starter

Zorac

Joined Oct 9, 2016
23
i went back to my pi and check it as its been a while and realized under under the shrink wrap were 1k current limiting resistors (although I had them wired on the wrong size (the gpio side, not the door sensor side) of the pull up resistor, that combined with the fact that the circuit is grounded 99.9% of the time (anytime the door is closed) is probably what saved my pi.

the link was good although i don't fully appreciate what it is trying to tell me. it looks like I need a bigger pull up resistor and a capacitor at the bare minimum. this leaves me with more questions than i started with though...

the link says 100k pullups, but most examples use 4.7k or 10k. would 10k be my best bet?
do i need to use clipping diodes?
instead, why not just use a single 3.3v zener and essentially make a basic voltage regulator?
is the filter/protection circuit instead of, or in addition to shielded wire? can i ground the shielding to the ground pin or should i be taking that to the power source?

this seems to be a common debounce circuit which the digikey link indicates will protect my pi, (adequately protect?)
https://hackadaycom.files.wordpress.com/2015/11/debounce-sch.png
the hackaday article also suggests using a logic chip such as 74HC14 (in addition to the circuit above) to provide hysteresis and while that makes good sense for deboucing and filtering out the noise, would it help protect the pi as well or am i back to needing clipping diodes?
 

ebp

Joined Feb 8, 2018
2,332
Because the current into or out of a CMOS input is very small (unless there is a pullup or pulldown resistor internal to the IC, and some microcontrollers have this as a very useful configurable feature), it is quite acceptable to use a resistor in series with the input. The value can be quite high, but the specs for the input should be checked to make sure that the resistor doesn't cause grief due to the "leakage current" at the input. Typically something in the range of 100k is quite acceptable. There is capacitance associated with the input, so the resistor will slow transitions, but this is of no consequence with switch inputs.

Inputs, with very very rare exceptions, have protection diodes between the input pin and the chip's common (ground) and supply pins. They will do a good job of protecting the input, provided the current through them is limited. That's where the series resistor comes in. It is now reasonably common to find a spec on the datasheet for maximum current into or out of an input, which really means through the protection diodes. Most will handle 5 mA, some 10, some only 1 or 2.

If you can tolerate the series resistance, there is little value in adding external protection diodes in many cases. They do have their merits if there is expectation of severe static discharge. Special low-capacitance transient voltage suppressors (essentially zener-like devices specially made for low capacitance and high transient current handling, relative to their size) are usually the best choice. Ordinary zener diodes, especially "true" zeners (up to about 5 volts) conduct moderate current well below their nominal voltage, so are unsuitable in many cases. You might do something like using a 10 volt zener (actually an avalanche diode, even if it is called a zener) "ahead" of a current limiting resistor. It is important to minimize the inductance in series with the protective device - layout should be tight and lead lengths minimized. With a very fast-rising current spike, a tiny amount of inductance can allow a large voltage. Good decoupling of the supply rail close to the protective devices is important .

When switches are used, pullup resistors sometimes need to be matched to the switch as much as to the signal input. Ordinary switches tend to be rather unreliable at very low voltage and current, so a pullup that sources several milliamps may be used. Reed switches are much more reliable at very low current. Gold-plated contacts generally fall between and are reliable with sub-milliamp currents at low voltage.

The hackaday circuit will do a good job of protecting an input if laid out well (again, pay attention to inductance). R1 is necessary to limit the current into the switch from discharging the cap, but with "ordinary" contacts allowing that current to be quite high can be beneficial. With reed switches or gold contacts care is required to be sure the current to low enough that it won't damage the contacts. Using and HC14 (or HC132) adds a lot of protection for the microcontroller input, with the added benefit of making nice clean edges. If the input is used to generate an interrupt or clock a counter, cleaning it up external to the controller can be highly advantageous.
 

Thread Starter

Zorac

Joined Oct 9, 2016
23
Thank you ebp, that is an excellent explanation. This is far from my background and I sometimes struggle to understand what is happening or even knowing where to start with google.

You suggest an HC132 as an alternative, but doesn't that need two inputs per output/gpio? Since I have a switch with only a single output shouldn't I stay with the HC14?

Hackaday was nice enough to also provide the schematic for the logic circuit as well:
https://hackadaycom.files.wordpress.com/2015/11/proper_debouncing-sch.png

The two things I notice is that they have now moved the pullup resistor to the switch side of the current limiting resistor (if its on the other size I'm guessing there may be an issue with the HC14 seeing a low signal with the current limiting resistor?) and they have increased the size of the current limiting resistor. Based on what you said, I presume the current into the HC14 is even smaller than the CMOS so they can do that. The advantage would be additional current protection for the HC14.

In case anyone is interested in reading, here is the full link to the hackaday article.
https://hackaday.com/2015/12/09/embed-with-elliot-debounce-your-noisy-buttons-part-i/

I usually just jam all my components onto a prototype pcb and try and minimize the footprint, I guess I now have some reading to do to minimize inductance on that board now... :)
 

ebp

Joined Feb 8, 2018
2,332
The HC132 is simply an alternative that might be useful on occasion because you need a gate or two or three along with a debouncer. If you use it as a switch debouncer you can either tie the unused input HIGH or connect both inputs together. Connecting both together does double the leakage current but it also doubles the protection diodes. If you are using surface mount, there are some nice tiny 6-pin packages with one HC132 type or two HC14 type devices in a package.

You can put the pullup on either side of the current limiting resistor, but then you must be careful you can still meet proper logic levels. If you use two equal-value resistors, such as the 10k parts in the hackaday circuit, with the pullup on the gate side, when you closed the switch the input could never be pulled down to less than half of Vcc. That is too high to meet the logic 0 voltage requirement.

The HC14 is CMOS (High-speed CMOS), so the input characteristics are similar to those for the inputs of a microcontroller. Most microcontroller inputs can also be configured as outputs, so there is extra circuitry that does alter the characteristics a little. The circuitry surrounding I/O pins can actually be pretty complex with some of the newer processors that have highly configurable pins. Even on some older processors some of the inputs have hysteresis, so you can use the RC debounce circuit without the need for an external schmitt trigger. I used to use 8-bit PICs quite often (not that I ever really liked them much) and started using spreadsheets to help keep track of which pins did what and even to make configuration incantations easy to copy and paste.
 

Thread Starter

Zorac

Joined Oct 9, 2016
23
No surface mount here, I have enough problems with leads through holes to do that yet.

You can put the pullup on either side of the current limiting resistor, but then you must be careful you can still meet proper logic levels. If you use two equal-value resistors, such as the 10k parts in the hackaday circuit, with the pullup on the gate side, when you closed the switch the input could never be pulled down to less than half of Vcc. That is too high to meet the logic 0 voltage requirement.
So to stay with this circuit, for R1 stick with the 10k, and for R2, drop to 1k, otherwise I might not meet the low level/voltage for the logic of the HC14/HC132. HC14 will work fine, but for double the diode protection go with the HC132 and tie the inputs together.
https://hackadaycom.files.wordpress.com/2015/11/proper_debouncing-sch.png

Sometimes there is something to be said for simpler. I was trying to get an addition serial port on an Arudino M0 the other day and I had no idea what most of the code meant to initiate the port although after a few good examples I had it working. The SAMD21 ports are pretty configurable compared to the older/simpler arduinos, or at least in my minimal experience.
 

ebeowulf17

Joined Aug 12, 2014
3,307
Using and HC14 (or HC132) adds a lot of protection for the microcontroller input, with the added benefit of making nice clean edges.
Doesn't that just move the other protection devices though, not eliminate them? The HC14 etc. inputs would need similar protections, right?
 

eetech00

Joined Jun 8, 2013
3,961
So I have two door sensors and two gate sensors (magnetic) on a raspberry pi. The problem is that they periodically trip without the doors/gates being opened so I assume I am picking up noise on the circuit (roughly once a week on the longest run, longer on the shorter runs). The sensors are wired directly to the pi and are being fed with 3.3v with a 4.7k pull down resistor. The runs are pretty long, up to 50', and I am just using some thermostat wire on two runs, and some old phone wire on the other two runs. To fix the problem i wrote some code which reads the sensors twice with a 0.3sec delay between the readings and the readings must be the same or it ignores the readings until its time to read again (in another 0.3seconds) which has resolved the issue.

My questions are:
1. Should I be fixing the wiring to prevent the noise (and how) or is the coding an acceptable solution? What is best practice?
2. If I wanted to track the noise, how would I do that or see it? My volt meter doesn't see the noise on the circuit when I measure voltage.
What type of sensor are you using? Part no?
I had an issue once with random alarms and wired sensors. The problem was related to sensor circuit resistance.
I changed them to wireless sensors and the problem stopped.
3.3v seems low for long wires...the alarm system I have is 12 volts.

eT
 

Thread Starter

Zorac

Joined Oct 9, 2016
23
Doesn't that just move the other protection devices though, not eliminate them? The HC14 etc. inputs would need similar protections, right?
doesn't the HC14 just become a sacrificial device then? with the current limiting resistor and capacitor it should be protected, but if not, better to loose the HC14 than a pi.

What type of sensor are you using? Part no?
I had an issue once with random alarms and wired sensors. The problem was related to sensor circuit resistance.
I changed them to wireless sensors and the problem stopped.
3.3v seems low for long wires...the alarm system I have is 12 volts.
eT
these are the sensors on my gates:
https://www.aliexpress.com/item/Saf...a211-419e-ab48-75f23dba24e7&priceBeautifyAB=0

i would expect higher voltage might make the the sensor less susceptible to noise, but i would either have to step the voltage down or use something like an optocoupler to trigger the pi. the voltage makes it back to the pi, so if i can provide adequate protection and address the noise this is probably the easier solution
 

ebeowulf17

Joined Aug 12, 2014
3,307
doesn't the HC14 just become a sacrificial device then? with the current limiting resistor and capacitor it should be protected, but if not, better to loose the HC14 than a pi.
Sure, if that works in your situation, that's great.

If it were critical to keep the whole system undamaged and operational, then there would be no benefit in sacrificing the HC14 to protect the Pi. If the primary concern is just the cost of replacement parts, then putting a cheap chip between the outside world and your Pi is a wise move.
 

AnalogKid

Joined Aug 1, 2013
11,056
If severe transients are a frequent problem (central Africa, Venezuela, Singapore, Florida, and Georgia), consider putting the HC14 in a socket. If you want an even higher level of protection, a ULN2003 gets you 7 channels rated for 50 V, bigger, fatter diode protection on the inputs and outputs, and one of the resistors included inside for free (but you might need to add pull-up resistors on the outputs). The pinout is different from an HC14, but better for high voltage arc-over protection. My go-to part for both input and output buffers in extreme outside-world environments.

ak
 
Last edited:

Thread Starter

Zorac

Joined Oct 9, 2016
23
If severe transients are a frequent problem (central Africa, Venezuela, Singapore, Florida, and Georgia), consider putting the HC14 in a socket. If you want an even higher level of protection, a ULN2003 gets you 7 channels rated for 50 V, bigger, fatter diode protection on the inputs and outputs, and one of the resistors included inside for free (but you might need to add pull-up resistors on the outputs). The pinout is different from an HC14, but better for high voltage arc-over protection. My go-to part for both input and output buffers in extreme outside-world environments.

ak
my power is pretty good, but its only induction current i'm worried about. the pi has been running for probably two years now with only a 1k current limiting resistor and besides the odd random trigger has been fine.
 

pyroartist

Joined Oct 9, 2015
131
You are looking at this the right way. Long outdoor wires do invite transients due to static (wind on a dry day) and local lighting discharges (even cloud to cloud radiates a powerful pulse). Shielded wire with a grounded shield will help greatly in long term survival of the RasPi. Keep your 1K resistors or bump them up to 10K if it makes you feel safer.
 

ebp

Joined Feb 8, 2018
2,332
Doesn't that just move the other protection devices though, not eliminate them? The HC14 etc. inputs would need similar protections, right?
Most definitely. Using a separate IC can improve repairability but it must be chosen well, in conjunction with other parts, to improve system reliability. Geometries are probably larger on simple logic ICs than most microcontrollers and that confers some advantage.

In very difficult environments, combinations of devices such as gas discharge tubes and zener-type transient suppressors may be required, and it may be advantageous to put some of the protection circuitry on a separate circuit board that can be located remotely (e.g. near a really good earth ground if lightning might be a problem - as is typically done with telephone lines entering premises). Inputs that come from switches are comparatively easy to protect because you usually get lots of room to maneuver in terms of delays and messing with impedances. Good protection on precision analog signals or fast digital signals can be pretty difficult. Protection devices can occupy considerable board real estate.

As analogkid recommends, transistor arrays can be useful because the diodes and transistors are large. Devices intended as line receivers, such as RS-422/485 receivers or if voltages permit, even RS-232 receivers might prove useful. Most such devices now are designed and quite well specified for ESD survival. The input hysteresis of receivers is useful for squaring up filtered inputs.
 
Just to add a few thought on the cause of the noise interference from an old time electronics tech. I am wondering if these doors are outside?

Long lengths of any wire make for a really good antennas, even when the pair is twisted. If someone in the area has a welder or very bad motor running nearby it can radiate via powerlines and you can even get some odd earth transients. Plus on some days there can be high static charges in the air purely due to wind, clouds and such like.

To see these sort of transients you use an oscilloscope on the pair, but you might have to be watching for a long time to capture it. Another option is to use a voice recorder on the line - connecting it into a small recorder for a day or so. Then listen to the line clicks. To make this much easier, view the recorded sound with a program like Audacity that show the sound levels like an oscilloscope. You migh see the spikes.

But even if you see them, how do you fix them? A lot of these kinds of issues can be REDUCED by putting ferrite donuts periodically along the cable... or at least one at each terminating end (about 6 to 12 inches back from the actual termination if you can). Its acting as a choke to high frequencies - spikes. To improve on this, try putting two small capacitors accross the pair at BOTH ends. 100 pf and 1pf capacitor. Use two because they each handle differnt frequency spikes. They effectively kill any residual spike that got past the choke... ort use instead of a choke. Finally I'd also like to suggest that you might want to make sure that at you CPU end that the line is actually grounded properly. 50 feet of cable can easily attract a lightning hit and not just fry the CPU but anyone near it at the time. Earthing it will help a lot. Yes if you wanted to you could try a 1 Watt zenner accross the line to try to stop peaks. Measure he switching voltage on the line and pick a zenner a volt above that.

Oh and I think many of the responses above are all smart too, but most require power supplied and consumed. Ferite cores, capacitors and earthing are all passive. Keep you software averaging section too because that also makes good sense.
 
Last edited:

Thread Starter

Zorac

Joined Oct 9, 2016
23
thanks, i hadn't though about using ferrite donuts. i did keep my logic checking and wired in an hc14 with current limiting resistors, pull down resistors, and denouncing capacitors, working well for about 6 months now without a single false alarm.
 

Attachments

Top