Hi,
I have an arduino uno, that has a digital input on pin 5 and reads the output of a simple interruptor.
The interruptor receives 5 V from the arduino and the output lead is sent to ground. The 5V lead is also sent to the arduino pin 5 so I can read the state of the interruptor.
When the switch is not depressed, the arduino reads a HIGH value because there is conductivity through the switch and there is a potential difference between the arduino's 5V and ground.
When I press the switch, the current is interrupted so the input pin 5 on the arduino has zero volts.
I tested all this with a multimeter. The voltage across the leads on the switch and between pin 5 and ground on the arduino.
However, on the serial console, where I am outputting the pin 5 value as an int, it starts reading a 1 (one). When I press the switch the output stops and the serial ports on the arduino IDE change. For example if my board is on /dev/ACM01, when I press the switch the serial port on the IDE disappears and /dev/ACM02 appears. The current serial console does not output anymore data, and if I close it, and open the console on the new serial port, the output continues there, until I press the switch again and the same happens reverting back to /dev/ACM01.
What can be happening here ?
Can anyone try to reason on this based on my description ?
Thanks,
Regards
You may have mentioned the problem. When the switch is not depressed the circuit is interrupted and I have a difference of potential of 5 V.
When I press the switch it indeed lets current flow from arduino 5V to arduino Ground. That should be the problem. So I suspect that adding a resistor after or before the switch may fix it, right ?
If it's just a switch then just connect one side to GND and the other end to any Arduino pin.
Then use pinMode (pin, INPUT_PULLUP).
Now when the switch is not pressed the input will be high and go low when pressed.
Jim-P got the solution for you. Lets hope the Arduino survived.
If you're not aware of it already, buttons etc needs to be debounced. It can be done in hardware as well as software. Check out the library Bounce for a sw solution.
It didn't fry.
As for the bouncing, I am using two variables to store the state of the switch : one that stores the current state that is read on each loop cycle, and another that stores the value in the previous cycle. When the value changes, my program does stuff. When I detect a value change from the switch being activated I use a small delay to eleminate the effects of the bounce. It has worked satisfactory for the time being.