ElEscalador
Posts: 862
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Pullup vs Pulldown - Reliability With Long Wire Runs

Wed Oct 19, 2016 9:57 pm

Has anyone toyed with both and found a significant difference? I need to reliably detect 9 inputs - currently works perfectly on the breadboard, but I'm concerned about unshielded wire runs through the house...probably up to 50 feet or so. So far I've been internally pulling the pins low and sending 3.3 volts to the switches, and detecting when 3.3v gets back. I'm wondering if it might be more reliable and/or allow longer distance to internally pull my pins high then "send ground to my switch and back."

Thoughts? Whats the greatest distance you've used the pi gpio for digital sensors without resorting to external pullup/down resistors?
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

Moe
Posts: 230
Joined: Sun Jan 25, 2015 2:44 pm

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Wed Oct 19, 2016 11:15 pm

Depends on the application. If the switches spend most of their time open and only close to indicate an event, say, then use pullups and let the switch pull to ground. Reasons are:
1. When the switch is open it doesn't draw any current;
2. Interference (e.g. switching spikes from nearby equipment) can easily induce a high-enough voltage to be detected as a false high on a normally-low line, but is very unlikely to to cause a false low on a normally-high line.

It also depends what you want it to do under fault conditions, like if the connection is cut.

Distance-wise I don't think it'll make much difference; same current flows and the system has to detect both states regardless.
Submarine communication systems engineer and amateur robot enthusiast.

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 2:19 am

Mandatory:
Use a strong pull-up at the input on Ras-pi end. (1k to 5k or so)

Really good idea, if at all possible:
Use twisted pair -- about 1 twist per centimeter or two

If there are problems with noise:
You might try putting 10uF to 100uF across the input and ground right at the input along with 100Ω to 500Ω at the input and in series with the switch. This will form a divider with the pull up when the switch is closed, so keep the ratio around 10:1. Don't use a large capacitor without a series resistor or you could reduce the life of the switch (although the inductance of long leads mitigates that effect as well)

User avatar
karrika
Posts: 1281
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 3:11 am

My suggestion is to use Cat cables. The Ethernet cables work very well for long distances. You have 4 twisted pairs in one cable. 50 ft is no problem.

To reduce noise you need a much smaller capacitor. This is the way I have connected the pins. The capacitor to ground is 10nF, the series resistor to damp the noise is 1k.

The current going to the switch is just 1mA with the 10k resistor. If you are using the internal 50k resistor you could drop the series resistor to 200 ohms. The capacitor could also be a bit smaller in this case.
gpiolines.png
gpiolines.png (17.48 KiB) Viewed 2724 times
I even have a photo of my DiscoCap board http://discohat.com/discocap/

But the most important thing is the software. Read the switch value a few times before accepting a change. I read it 3 times during 150ms. If all 3 readings are the same I believe in the state of the switch.

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 3:44 am

Yeah, I agree 10nF to 100nF is a good idea -- especially if we are concerned with picking up wifi, garage door openers, microwave ovens, etc... I went with the large cap thinking about trying to minimize 50/60Hz stuff coupling onto the lines. Although the values I picked don't generate an RC constant that is really optimum even for that.

If it is a problem, both can be used.

Edit: Oh, and for the software debounce ... I like to keep a counter that is incremented when the switch is closed and decremented when the switch is open. When the count hits zero, declare the button pushed. When the counter hits max, call the switch released. Don't let the counter overflow or underflow. When the count is in between, the state is indeterminate. Add an output state that is set to true when the count is full, and false when the count is empty. This is an extension to karrika's suggestion
Last edited by ewaller on Thu Oct 20, 2016 3:09 pm, edited 1 time in total.

ElEscalador
Posts: 862
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 4:16 am

Great info - thanks. The cable is already in place as what I'm doing is read the state of three thermostats. Pulling new wire isn't out of the question but would be a fairly big pain, so probably only after proving that the system is unreliable with the existing thermostat wire. The good news is that any resulting output only happens as long as the switch is made and thermostat events should generally last on the order of minutes - all the time in the world to confirm state. I like the repeated sampling to prevent relay chattering.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

User avatar
rpdom
Posts: 17736
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 5:06 am

For long cables or noisy environments I use opto-isolators at the Pi end and use 5V to the switch to drive the input of the isolators via a suitable resistor. A little noise on the line is unlikely to be enough to trigger the LED inside the isolator, so you won't get false readings. Because the output stage of the isolator is right next to the Pi, you don't need strong pulls so can use the internal pull-up/down.

User avatar
karrika
Posts: 1281
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 5:24 am

Yes. Opto-isolators are good. And if you want to protect the optos from blowing up you could also add a reverse diode. I actually do this on my DiscoHAT - the pro-version :)

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 3:19 pm

For the thermostats, definitely use a series resistor -- especially if you have a capacitor across the line. The reed relays used in some thermostats can literally weld themselves shut if they try to dump a capacitor without something to limit the current. Again, long wires mitigate this as well because of their inductance.

Moe
Posts: 230
Joined: Sun Jan 25, 2015 2:44 pm

Re: Pullup vs Pulldown - Reliability With Long Wire Runs

Thu Oct 20, 2016 8:34 pm

In your application the only things you need to worry about are:
a) switching transients causing false readings, which capacitors will reduce but even better to filter them out in software;
b) straightforward volt drop along the cable, which I don't think will be a problem over 50m.

This is a single-ended DC signal so twisted pair cable (or even paired cable) will make absolutely no difference, and you don't need to worry about the RF stuff. 'Cat' cable has a tight impedance spec and can be useful if you can get hold of it cheaply, but mains cable or speaker wire would be just as good. If the existing cable was designed for the thermostats then it should be fine.
Submarine communication systems engineer and amateur robot enthusiast.

Return to “Automation, sensing and robotics”