Photon: How to stop interference on Digital Input Pin

This is my first post. I have just finished my first project which involves a switch running to pin D4. The other end of the switch is connected to the 3V3 pin. The pin is set up via code as pinMode(D4,INPUT_PULLDOWN) so that when it closes, a 3.3V is applied to the pin and it raises an event “switch closed”. All works well. However, when I attached a 5m wire between the switch and the pin, I started to notice the event “switch closed”, followed by “switch opened” around 0.3 seconds later, even though the switch has not been touched. I suspect that this is interference in the wire. I have read on line that it is possible to supress this with a capacitor. Is this the best solution and could I please have more information on how to do it?

While adding caps to filter/decouple noise it’s also worth mentioning that the internal pull-down resistor is too weak for a 5m cable (~40k).
You should rather go with INPUT and an external pull-down - but on second thought, I’d actually go for a pull-up resistor (maybe even to 5V on Vin) and have the button close to GND (but for that you’d also need to invert the logic - LOW == closed).

3 Likes

I’ll also add that using a shielded wire harness would also help with any stray RF interference that could induce strange voltage behavior.

1 Like

It’s highly unlikely its stray RF. Do what @ScruffR says and use a 1k - 2.2k pullup resistor, and have the switch close to GND. You should be fine. Adding a 0.1uf cap at the pin to GND will also help with any denounce issues.

Thanks Tommy_boy and ScruffR for your suggestions. I will try all 3 ideas but have some practical questions about them please:

  1. Capacitor cap: do I attached the positive end of the capacitor to Pin D4 and the negative end to a resistor thence ground? What do you recommend for the size of the capacitor and resistor?

  2. External pull down resistor: no problem to reverse the code logic, but what size resistor should I use assuming I stick with 3.3v as my power source?

  3. Shielded cable: would shielded Ethernet cable work? Do I just take 2 of the tristed pairs and use these for the switch circuit? Do I need to attached the shield to the Particle GND?

@grovesj, you can go with the values and wiring @seulater has suggested and yes, a Cat5 cable should work.

Thanks for providing these values. I would like to understand the size of the cap please. I tried to estimate it as follows.

Charge building in wire = Q = I x t
Assuming max pin current I=25mA for half a second
Q=12.5 e-3C

Q = CV in the capacitor so assuming 3.3v then C = 4 e-3 farads = 4000 uF

So my estimate is clearly wrong but I am trying to understand why.

You don’t want the cap to store the “expected” charge but you want the cap to “short-circuit” spikes of induced voltage towards GND, for that you want to us the dynamic characteristics of caps as in an RC filter (δi = C δu/δt and X = 1 / ωC)

1 Like

A cap of the size you need won’t need to be a polarised (electrolytic or tantalum) type. A metallised polyester or ceramic type will do.

Also, for such a long run, which may pick up static charge and other transients, you should really also use a transorb, but this may be overkill in your situation (not knowing where your wiring will run).

Finally, I would recommend a small series resistance (100 ohms or so) to damp any oscillation between the series inductance of your 5m of wire pair and the bypass capacitor. May save the input of the photon from excessive voltage spikes on switch closure and the inevitable contact bounce.

Have a look at this link - there is a wealth of information you can find out for yourself. https://www.digikey.com/en/articles/techzone/2012/apr/protecting-inputs-in-digital-electronics

3 Likes

Thanks for your input, these suggestions worked.