Hi,
I would like to read a switch on an I/O pin, and have an LED illuminate or extinguish when the switch is pressed. How can that be done using a single I/O pin?
thanks
Hi,
I would like to read a switch on an I/O pin, and have an LED illuminate or extinguish when the switch is pressed. How can that be done using a single I/O pin?
thanks
Simply connect the led + series resistor between 5V and the Arduino pin. Place the button between the Arduino pin and ground. When the button is pressed, current will flow and the led will light, and the Arduino pin will be pulled to ground. When the button is not pressed, the Arduino pin will be pulled to 5V. A very very tiny current will flow into the Arduino pin, not enough to light the led.
If you want the led to extinguish when the button is pressed, use a push-to-break button.
This seems so simple, I am wondering if I understood your question.
PaulRB:
Simply connect the led + series resistor between 5V and the Arduino pin. Place the button between the Arduino pin and ground. When the button is pressed, current will flow and the led will light, and the Arduino pin will be pulled to ground. When the button is not pressed, the Arduino pin will be pulled to 5V. A very very tiny current will flow into the Arduino pin, not enough to light the led.If you want the led to extinguish when the button is pressed, use a push-to-break button.
This seems so simple, I am wondering if I understood your question.
thanks. what about for a momentary switch?
tim77777:
thanks. what about for a momentary switch?
I assumed you were talking about a momentary switch. You didn't actually say what type of switch.
PaulRB:
This seems so simple, I am wondering if I understood your question.
I was wondering that too, what's the catch? I suppose it depends on the OP's purpose here.
tim77777:
I would like to read a switch on an I/O pin, and have an LED illuminate or extinguish when the switch is pressed.
I would surmise that the led is a visual indication / confirmation of whatever the digitalRead()of the pin says?
Unless the "momentary switch" comment means he wants to toggle the LED. So first push of switch, LED on, second push, LED off. That's not so easy!
Steve
As post #2 said.
And post 334 here:
https://forum.arduino.cc/index.php?topic=445951.msg3336467#msg3336467
And post 391 here:
https://forum.arduino.cc/index.php?topic=445951.msg3430326#msg3430326
PaulRB:
Simply connect the led + series resistor between 5V and the Arduino pin. Place the button between the Arduino pin and ground. When the button is pressed, current will flow and the led will light, and the Arduino pin will be pulled to ground. When the button is not pressed, the Arduino pin will be pulled to 5V. A very very tiny current will flow into the Arduino pin, not enough to light the led.If you want the led to extinguish when the button is pressed, use a push-to-break button.
This seems so simple, I am wondering if I understood your question.
Assuming a latching switch, what are the advantages/disadvantages of doing it the way you have suggested, compared to tieing the port to ground with 10k. A switch from 5v to the port. and a LED with series resistor from port to ground?
thanks
tim77777:
Assuming a latching switch, what are the advantages/disadvantages of doing it the way you have suggested, compared to tieing the port to ground with 10k. A switch from 5v to the port. and a LED with series resistor from port to ground?
A latching switch is electrically no different from a momentary switch, only mechanically different.
Your suggested circuit would also work. I assume the 10K acts as a pull-down? It is unessessary because the led+series resistor will act as a pull-down.
The advantage of the circuit I described in post #1 compared to your circuit is that, often, a switch will be mounted on a panel away from the circuit board/Arduino on long leads. These long leads are more susceptible to breakage/disconnection. If one of the leads carries 5V, then a disconnected lead could cause a damaging short circuit by touching something it shouldn't. In my suggestion, neither lead connects directly to 5V.
larryd:
For a LED and switch to be used on the same pin see wiring:
I get what you and groundfungus are saying, but the way I interpret the wording of post #0, there is no requirement for the Arduino to control the led. Only the switch controls the led. The Arduino needs only to read the switch state. The possibility remains, of course, that post #0 is not a complete and accurate description of what the OP wants to achieve!
Possibly, but you can just guess the next thing is ‘I want to add LED program control’.
Either way, the schematic offered will work fine.
Yes, you are very likely correct. But, on the other hand, by anticipating the OP's true requirements, you might deprive them of the important lesson of learning to ask accurate questions. By using your superpower of "experience", they won't ever realise they asked a wrong question, and will continue to assume everyone knows what they mean when they ask anything. When they realise they got a wrong answer, they will assume it's someone else's fault, not their own. But I know you know all this...