LED in input signal

Hello,

I need to turn on a LED when I press a pushbutton. I’ll use that pushbutton to do different things so it is an input as well. But as I’m running out of pins, I wonder if I could do a circuit like the attached, so I’d save a digital output.

Thank you very much :slight_smile:

It might work at first glance, but please don't use it. When you set the output LOW (so the led is off) and press the button, you have a shortcut from 5V into pin D11.

There are many ways to combine pins, even without extra shift registers. For example the analog pins can be used as normal digital pins. For leds you can use charlieplexing, for buttons a matrix.

Do you need that led ? Is the system led at pin 13 not enough ? What is on the other pins ?

Yes, you certainly could ...

But,

A minor and a major problem. The minor problem is that you have depicted a switch to 5V, which is bad practice; you should have the switch to ground, use the INPUT_PULLUP function instead of the 10k resistor and of course, the LED to 5V with its 220 ohm resistor.

The more major concern is that you should have the switch via a 330 ohm resistor to the Arduino pin, just in case you were to get the code wrong and set the output HIGH while the switch is pressed. This is somewhat more likely since you do in fact write the output HIGH to enable the INPUT_PULLUP.

Needless to say, this works by setting the pin to output and writing it HIGH (and in that exact order) to read the pushbutton. So long as the LED is to be not lit, you can leave it that way. If you want to light the LED, you write the pin LOW and then set it as an output (in that exact order) and leave it that way until you need to read the button next in which case you briefly perform the button read sequence and immediately re-enable the LED.

Thank you for your replies, but I think I didn't explain myself well. D11 will always be an input. I don't want D11 to be used as an input and an output. Just I want to read in D11 the state of the switch and, at the same time, when the switch is closed, light the LED up (so this will be straightforward, I wouldn't really need to use another PIN).

Thank you again

Okay, that is no problem at all.

When you turn things upside down, you don't need the 10k resistor. Led with 220 ohm to +5V. Button to ground. Instead of the 10k resistor you can enable the interal 50k pullup resistor. The 50k internal pullup parallel with the led + 220 ohm resistor should be enough to make the input HIGH when the button is not pressed.

Great Peter!!! I got you! Actually I usually use the internal pullup resistor, but I didn’t see I had that resistor parallel with the LED’s resistor.

Thank you again for your help. I am attaching my diagram, so it could help somebody else.

That is fine just as long as you do not intend to ever set the pin as an output which would mean that it might be set HIGH.

That is basically what I do in this project. http://www.thebox.myzen.co.uk/Hardware/Pendulum.html

Never was a fan of seriese resistors on inputs, you have to be very stupid to need them and lacking in confidence to want to use them.