0
\$\begingroup\$

I have a contact (pushbutton) that's about 10 metres away of a Raspberry Pi.

However; it behaves quite erratic and would trigger when for example an exhaust fan is started in proximity of the wire. I believe the AC electromagnetic interference causes it to trigger.

What I've tried:

Software fix #1 Whenever an active signal is received; I wait 100ms and check again. If it's active twice; it's verified. This actually 'fixes' the problem; though I would like to know how to make the hardware layer more resistant against emi.

Soft/hardware fix #2 Enabled internal pull-up of the Raspberry Pi. In this way it has a predefined state and thus has less interference (though still notable amounts).

Hardware fix #1 Use an external pull-up resistor. I've tried 5K (not quite good enough) and 1K (seems about right), though various sources discourage low resistance as obviously causes higher current.

Hardware fix #2 use an cat5e foiled network cable instead of regular power cable. This didn't quite seem to work (its foil wasn't really connected to anything), should it be connected to GND? (or is shield not GND?). The resistance of the network cable could be higher as electical cable; since it has less copper?

What I'm searching for:

  1. Actual proper ways of implementing such systems; possibly for up to 50m.

I'm thinking of putting 24V on the wire with the button and a opto-isolator or relay on the Raspberry Pi's side. Though this would require quite some extra components and I don't understand why it would be required at ~10m.

  1. Theory and best practices?

How can we calculate the required amount of voltage? Should I use a shielded cable (how to connect the shield?) or a cable with low resistance?

\$\endgroup\$
7
  • \$\begingroup\$ Try a capacitor across the line, to absorb spikes. The bomb-proof way, up to many km distances, is a current-mode link, into an opto-isolator. \$\endgroup\$
    – Neil_UK
    Commented Aug 14, 2019 at 7:24
  • \$\begingroup\$ I don't understand why it would be required at ~10m. Because all wires also act as an antenna and pick up all signals which are present in your location (like 50/60 Hz from mains power). Longer wires can pick up larger signals. So much that these signals can get similar in magnitude as the on/off signal you're using. \$\endgroup\$ Commented Aug 14, 2019 at 7:32
  • \$\begingroup\$ I would go for your Hardware Fix #1 and use an even stronger pull-up resistor to make it really robust (maybe something like 330Ω). A capacitor to ground on the board side should also help against fast transient interference. If you really want to make it bullet proof the opto-based solution pointed by @Neil_UK is the way to go. \$\endgroup\$
    – joribama
    Commented Aug 14, 2019 at 7:41
  • \$\begingroup\$ Hmm, I figure it may be interesting to check using a scope, just to see exactly how big the interference is (how many volts it's peak is, how long it is and of there's multiple peaks). Interestingly enough I didn't even try the capacitor. \$\endgroup\$
    – aaa
    Commented Aug 14, 2019 at 7:42
  • 1
    \$\begingroup\$ If you have a scope available, you should definitely probe it. I bet you'll find AC power (50 or 60Hz, I'm not sure where you live) and maybe even some AM radio signals if you are close to a station. \$\endgroup\$
    – joribama
    Commented Aug 14, 2019 at 7:49

3 Answers 3

1
\$\begingroup\$

Make sure you need a bit of current to trigger the input, not only voltage.

When you only use voltage, into a high impedance input, like 10 kOhm, then you will have erratic detections on nearby relays or parallel ac wires and you will have to resort to dirty software method, if that works at all.

So, hardware fix #3: IEC 61131-2
IEC 61131-2
Require at least 2 mA to consider it a logic high. If the environment is especially noisy, use the Type 2 with at least 6 mA.

This can be easily approached by a voltage divider into a schmitt trigger. Optional RC to slow down detection. See here for an example implementation.

\$\endgroup\$
4
  • \$\begingroup\$ Hmm, by that chart using 5V of the PI's input wouldn't even suffice. If I'm going 24V, I'd think of using an opto-isolator, but the input of the isolator needs to require at least 2mA to consider it a logic high as well in that case? Would adding a resistor in front of it help? \$\endgroup\$
    – aaa
    Commented Aug 14, 2019 at 8:44
  • \$\begingroup\$ @Paul You could go 24V, but you should't have to. Opto is not required, but it does need current to turn on, so you get this bit of current implicitly. Opto is useful when you need the isolation, otherwise not so much. Just beware that running 5V common away from the Pi in a cable is subject to the same noise and isn't without risk, an opto on the input does not remove this risk. \$\endgroup\$
    – Jeroen3
    Commented Aug 14, 2019 at 9:22
  • \$\begingroup\$ For that reason a 24V supply and 24V->5V unit for the Pi would give less risks. Or power both individually from the wall (though I don't mind the 24V being single point of failure). But I don't understand how (on 3,3V) a voltage divider into Schmitt trigger (the Pi has a Schmitt trigger internally and a "slew rate" option?) would cause it to only go on at at least 6mA, but my analog electronics knowledge is quite limited. \$\endgroup\$
    – aaa
    Commented Aug 14, 2019 at 10:19
  • \$\begingroup\$ @Paul If you don't need to get the voltage down you don't need the primary divider, only the pull-down resistor (~1 kOhm for 3.3V), but you will need the secondary impedance of a few k to prevent damage to the chip in case of transients. This resistor depends on the impedance of the chip itself, and together should keep it above Vin-high for the chip. 2 Volts usually. Did you check this schematic? \$\endgroup\$
    – Jeroen3
    Commented Aug 14, 2019 at 10:42
2
\$\begingroup\$

As Neil_ok says: The bomb-proof way, up to many km distances, is a current-mode link, into an opto-isolator.

A simple solution is to make it difficult for external sources to put energy in your system. A very low input impedance helps with this. e.g. you can put a 220 Ohm resistor from the input to the ground. But in order to make the input high when the switch is open, you need now a much lower pull-up resistor too. Lets says about 82 Ohm which gives 220/(220+82)*3.3 = 2.4 Volts.
You can add Niel's capacitor too.

The disadvantage is that this continuously uses 3.3/(220+82)= ~10mA.
When the switch is pressed, the 220 ohm resistor is shorted and it uses 3.3/82 = ~ 40mA.

\$\endgroup\$
1
  • \$\begingroup\$ I don't see the benefit of having the 220Ω pull-down other than lowering the AC input impedance from 82Ω to 220Ω//82Ω (~60Ω). Not having a pull-down would cost no DC current when the switch is open. I'm assuming that the 82Ω is a pull up connected directly to the GPIO and the external switch is connected between the GPIO and GND. \$\endgroup\$
    – joribama
    Commented Aug 14, 2019 at 8:11
1
\$\begingroup\$

As soon as you have longish wires (I'd say 1m or more, but it depends on the environment) connected to a uP port, isolation (with an opto usually) is advisable, along with some filter components (R's, C's, clamp diodes). It just reduces the energy present in transients substantially. Any common mode voltage induced in the wire is now not having little effect on the uP side, as the two wires are free to be at any voltage (within sensible limits). Also it is good practice to use twisted pair cable for this, which also helps ensure that common mode signals are of equal magnitude.

When the connection is direct, induced pulses are directly connected to the uP signal port and GND, which means all kinds of transients can get into your circuit.

You should pull the diode side of your opto (which is connected to your switch closure via a resistor) up to your system unregulated DC (NOT the Vcc for the uP) or any other voltage source available - also helping isolation. You can even use a totally separate V+/GND pair if available. (I realise a diagram would be helpful here - I don't have time right now.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.