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:
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.
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?
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.