Go Down

Topic: LED and switch on same I/O pin (Read 336 times) previous topic - next topic

tim77777

Jan 12, 2019, 11:46 am Last Edit: Jan 12, 2019, 11:51 am by tim77777
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

PaulRB

#1
Jan 12, 2019, 11:52 am Last Edit: Jan 12, 2019, 11:58 am by 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.

groundFungus


tim77777

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?

PaulRB

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.

Willpatel_Kendmirez

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.

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?






slipstick

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

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

tim77777

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

larryd

For a LED and switch to be used on the same pin see wiring:





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

PaulRB

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.

PaulRB

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!

larryd

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.


No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

PaulRB

#13
Jan 13, 2019, 11:29 pm Last Edit: Jan 13, 2019, 11:37 pm by PaulRB
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...

Go Up