I'm trying to read the state of a switch with an Arduino. The switch is being pushed each time a garage door opens or closes, and is located about 2 meters away from the Arduino. The cables connecting the switch to the Arduino are running alongside a conduit carrying a 220V cable.
Everything works fine and the switch states are read correctly, but when I operate the electric window on the floor above the garage, the Arduino also registers a press of the switch. After hooking up my oscilloscope to my circuit, I noticed a 472ns pulse of +100V coming over the cables connected to the switch. I did not measure the exact voltage, I guess it's closer to 220V, but I fear any voltage that high might damage the Arduino.
Using the Falstad simulator, I tried to create a solution for this problem, but I'm stuck and I don't know how to continue. Here is my simple circuit so far:
Normally, the Arduino reads 0V. When I press the switch, the Arduino reads 5V. When a pulse comes through, the zener diode clamps the voltage to just below 5V so the digital pin won't be damaged. The only thing that's still wrong, is the original problem! A voltage spike is still registered...
Basically I'm left with two questions:
- How do I prevent the voltage spike from being registered by the Arduino? I've solved it in software by not registering pulses smaller than 10ms (a switch press by the garage door takes 400ms), but I'd like to solve it in hardware. I'm guessing I need to use a capacitor, but I can't figure out how and where to place it...
- How does the voltage spike register in the first place? I get that it's interference from the cable running alongside the switch cables, but when the switch is open, there is no circuit for current to flow. Does that mean there's a voltage without current? How is that possible?
A colleague already warned me never to let digital pins float, but I'm not convinced that this is the case here. When the switch is open, the digital pin is connected to ground via the 1k resistor, if it's closed, it's connected to 5V, so the pin never floats.
Thanks!
PS:
- the 1ohm resistor was added because Falstad deals with ideal components. If the pulse was connected directly, I'd get a matrix error, hence the resistor.
- I've decided to ask this on Electronics and not Arduino, because besides providing 5V and reading a voltage, I don't think the fact that it's an Arduino matters to this circuit. Using a power supply and a voltage probe would yield the same result imho.