I've been working on a simple project involving a Raspberry pi and some switches. I would never thought I would have any problem for something as simple as a switch, but here I am.

The 2 wires from my switch are connected to the raspberry pi. One is connected to ground, the other is connected to a GPIO. The internal pull-up resistor of the raspberry pi is used.

The length of the cable is around 7m.

The program is just a matter of reading the GPIO and sending commands according to state changes.

My problem is simple: everything works fine for some time, but eventually the switch ends up not "working" anymore (reading low state whether or not the switch is pushed).

I checked the switch, and of course it still opens and closes as expected, so the problem comes from somewhere else. What I did is I disconnected the switch, and measured the voltage between the two wires: I get 0V.

This is my first clue for thinking something bad happened to the raspberry pi. Because the internal pullup is used, I should have measured around 3.3V. And this is indeed what I measure if I replace the raspberry pi with a new one.

I thought this might be caused by EMI, causing voltage spikes? I really have no experience dealing with this. So what I did is I replaced the raspberry pi, and I added an external pull-up resistor with a lower value (the internal resistor is 30k, I used 1k as external resistor), guessing that this would reduce the noise. But the same thing happened after one day.

This time, when I measure the voltage at the GPIO, I have around 1.2V. According to the reading, using a simple voltage divider, this is equivalent to a resistor value of 600Ohms pulling down the signal inside the raspberry. Definitely not normal.

Then I tried to replace the wires with shielded audio cable, connecting the the shielding to ground. And half a day later, yet another raspberry pi was good for retirement.

I have now placed a ceramic capacitor between gnd and the GPIO (100nF), and for now it seems to be OK (it's only been 12h) but I have to say I have no confidence because I'm not sure what is the reason for this problem.

On the same system, I have another switch with a much shorter wire (1m) and I never had any issue with this one. This is why I guess my wires are acting as antennas, but I don't know for sure (especially since shielded wires did not help).

Any idea about what could happen would be appreciated.

  • 6
    \$\begingroup\$ I would try putting a bidirectional transient voltage suppressor (TVS) on the input. The long wire could be picking up transients that are then coupled into the PI input. The TVS will also act to clamp static discharges. \$\endgroup\$ – crj11 Oct 5 '18 at 14:19
  • \$\begingroup\$ How much runtime and switching was there before each raspberry failed? \$\endgroup\$ – Bort Oct 5 '18 at 18:09
  • \$\begingroup\$ It could be EMI (wires acting as antenna), or ESD (electrostatic discharge). One thing you could do is put a buffer chip between the switch and RasPi. Better to replace a $1 chip than a $35 RasPi while you're getting this sorted out. \$\endgroup\$ – jwygralak67 Oct 5 '18 at 19:26
  • \$\begingroup\$ The runtime before failure was less than 24h. But from what I understand thanks to Andy's answer, it seems that the runtime is not really a factor of failure, but the number of time the switch is pressed is (each time the switch is pressed, there is a voltage spike due to the inductance of the wires) About the buffer chip, I think it can indeed be a good temporary solution. What about optocouplers ? \$\endgroup\$ – Fardenco Oct 5 '18 at 19:53
  • \$\begingroup\$ So ho much switching was there? \$\endgroup\$ – Bort Oct 5 '18 at 20:06

The wire has inductance and, the longer the wire is the more inductance it has. An inductance likes to maintain the current flowing through itself so, when your switch opens, the small stored energy in the cables magnetic field tries to maintain current flow and produces a sizable voltage (aka back-emf) in doing so. This can easily exceed the maximum voltage rating of your IO pin and pop goes the chip.

The capacitor provides a path to maintain current flow when the switch opens and normally will restrict any voltage spike (back emf) considerably. I have faith in it working but if it doesn't then it could be pick-up from AC power lines or some rogue RF transmitter nearby. In this case use a capacitor and a zener diode rated at a little below your power rail voltage.

| improve this answer | |
  • \$\begingroup\$ Thank you for your answer. I will try to see this voltage spike using an oscilloscope and confirm that the capacitor eliminates it. I'll come back after one week if the system is still alive to validate your answer ! \$\endgroup\$ – Fardenco Oct 5 '18 at 14:44
  • \$\begingroup\$ I'm not knowledgeable enough for an answer, but how about a resistor between port pin+capacitor and cable? I think I've seen that done before. \$\endgroup\$ – AndreKR Oct 5 '18 at 18:18
  • \$\begingroup\$ @AndreKR that can be done on some devices to limit the maximum current into the pin when there is an over-voltage situation but plenty of data sheets are unclear about this because they still state that such and such a voltage cannot be exceeded and the resistor relies on parasitic diodes on the chip that may "latch" and cause the chip problems. Other devices are clear about using a current limiter for protection and therefore a resistor can be decisively chosen. \$\endgroup\$ – Andy aka Oct 5 '18 at 18:29
  • \$\begingroup\$ Today I did a test, using 30m shielded cable, 1k pullup resistor and the same switch I used on the system. Here are the results : First, we can see there is a lot of bouncing , and each bounce leads to a negative voltage spike. We can also see that the spikes can go down to -2V. The spikes would not be as bad with less cable length, and less current flowing (so higher pullup resistor value), right ? \$\endgroup\$ – Fardenco Oct 6 '18 at 16:18
  • \$\begingroup\$ What was your setup when making these tests. There are anomalies that I cannot explain without knowing exactly where you had the scope connected and exactly where the switch was connected. Full circuit of test to make any sense please. \$\endgroup\$ – Andy aka Oct 6 '18 at 16:58

The issue is almost certainly not inductance but voltage drop. Particularly with thin wires you’re likely using, you can have significant voltage drop over the 14m round trip you’ve got to the switch and back.

That’s why a shorter wire works ok.

You could try a thicker wire, which will give less voltage drop. Also check your connections to ensure you’ve not got any high resistance connections.

Also, if it were inductance, a spike would have killed the GPIO pin permanently. If you can get a signal through the pin with a shorter wire, it not that.

| improve this answer | |
  • \$\begingroup\$ I did not say that I can get the signal to go through with a shorter wire on the same gpio, I said that I had other switches on other io that were not causing me problems, probably because they have shorter wires. After the problem occurs, the used gpio is dead, this is for sure. A voltage drop could occur if I had relatively high currents going through the wires or if I had really long cables, but I don't. And a voltage drop would certainly not kill the gpio, would it ? \$\endgroup\$ – Fardenco Oct 7 '18 at 10:44
  • \$\begingroup\$ Ah, sorry, I misunderstood - you tested the GPIO was dead by measuring the pull-up? Voltage drop doesn’t only occur with high currents, though it’s more an issue as high currents imply a low resistance load. But voltage drop can stop a digital signal. Antenna effect sounds more likely if the GPIO is killed. Try twisted pair, or an opto-isolator. \$\endgroup\$ – Dan W Oct 7 '18 at 10:52
  • \$\begingroup\$ I replaced the wire with shielded wire and had the same problem \$\endgroup\$ – Fardenco Oct 7 '18 at 15:40
  • \$\begingroup\$ I believe switching in a shielded wire won’t necessarily solve the antenna issue, but I’ve found twisted pairs effective at reducing noise. I’d recommend an optoisolator though; it’s more than you should need, but otherwise diagnosing this is likely to get expensive. \$\endgroup\$ – Dan W Oct 7 '18 at 17:37
  • \$\begingroup\$ If the problem were inductance from switching (back EMF), the solution would be a diode. However, I’ve never heard of back EMF from switching a switch. You get it from motors and solenoids - inductive loads. A 7m wire and switch should not be an inductive load. \$\endgroup\$ – Dan W Oct 7 '18 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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