Is this something I can do to save on pins? (Push button read and LED)

I basically want to read the button getting pressed with the basic push button code but also want an LED to turn on when that happens. I wanted to read a button on every input pin on the arduino, so I thought I could save on pins by just adding the LED like in the attached diagram.

I do notice a decrease in voltage every time I click the button,, from 4.95 to 4.55 but I think that is normal (I think?). Is there any reason I shouldn't do it like this? Would I be better off connecting the led and resistor in series instead of parallel? Would the voltage issue be solved if I used a higher amp power supply instead of the arduino 5v?

Thanks guys!

Add a 1K resistor in series with the LED, otherwise you will burn out LED before too many presses.

Here is a better approach. The pin reads as a high until the button is pressed.

if (digitalRead(2) == LOW){
// button is pressed, do something
}

I wanted to read a button on every input pin on the arduino

Does that include the analogue pins ?

A0 to A5 can be read like digital pins.
A6, A7 need analogRead(), values returned will be around 1023 for a high, and around 0 for a low.
I don't recall if A6, A7 have internal pullups.

@XRoads, why is your better approach better, I don’t see it. I know that the button HIGH LOW correspondence to pressed/not pressed can be handled in software.

I like the pressed is HIGH lights the LED and so the original. I guess you save a resistor, but maybe there’s more.

In any case, for deployed projects I would rather not do this at all - if an LED lights up as feedback for a pushbutton it should be because some active process actually recognized it. Use you imagination, an LED labeled “bomb disarmed”, for example hung off a toggle switch like that to save a pin on the Arduino controlling it.

While I am at it, I am struggling to remember a science fiction movie where they thought everything was cool because the bell didn’t ring. But the bell was faulty, haha.

a7

UKHeliBob:
Does that include the analogue pins ?

Yes it does. This is actually for an already deployed project that I'm touching up and this was a simplified version of that project's cicruit. I'm using a Pro Micro, hence the need to save on inputs.

alto777:
@XRoads, why is your better approach better, I don’t see it. I know that the button HIGH LOW correspondence to pressed/not pressed can be handled in software.

In any case, for deployed projects I would rather not do this at all - if an LED lights up as feedback for a pushbutton it should be because some active process actually recognized it. Use you imagination, an LED labeled “bomb disarmed”, for example hung off a toggle switch like that to save a pin on the Arduino controlling it.

Not really sure either how always high would be better but I'll iterate on the circuit and see which one works better for me. Maybe it's a matter of the Arduino handling the change from high to low better than the other way around? I'm not getting voltage drops with his version so that's a win at least.
As for having the LEDs this way, it's mainly aesthetics. Each button has an individual action tied to it, and unlike my example diagram they're not momentary switches so both the light and signal stay on until the user turns it of. Speaking of movies, I think there's a scene in Wallace and Gromit with a device that does exactly as my project, in regards to the lights.

CrossRoads:
Add a 1K resistor in series with the LED, otherwise you will burn out LED before too many presses.

Here is a better approach.

Thanks!!! This helps a lot!

you can wire multiple buttons into a button matrix as keypad. You can save some pin. For example, if you have 9 buttons, you can wire them as keypad 3x3, which required only 6 Arduino pins. And then you and read them as reading a keypad

The method in the original post runs 5V thru a 2.2V Vf LED (a typical voltage) drawing lots of current and causing the voltage drop you see.
The way I connected it limits the current thru the LED to something reasonable (5V - 2.2V)/1000 ohm = 2.8mA, so you don't get the current surge and voltage drop.

If you wanted the LED to stay on after a press was recognized, you could use a smaller resistor, say 330 or 470 ohm, and spend say 1/2 of the time as an input reading the pin, and the other half of the time driving the output low to turn on the LED after a press was seen (so switch pinMode between INPUT_PULLUP and OUTPUT). Then later, after a 2nd press was recognized, stop driving low to turn the LED off, and then wait for the next press to turn it on again.

@XRoacs THX. I thought you meant wiring it to pull up and/or using the internal pullup was better than pulling down. Understand you were referring actually to better to have a current limiting resistor inseries which is of course not better but way better, like necessary.

And nice trick for dual purpose I/O.

a7

IoT_hobbyist:
you can wire multiple buttons into a button matrix as keypad.

CrossRoads:
If you wanted the LED to stay on after a press was recognized, you could use a smaller resistor, say 330 or 470 ohm, and spend say 1/2 of the time as an input reading the pin, and the other half of the time driving the output low to turn on the LED after a press was seen

Both awesome ideas!!! If I ever scale up this project both of those are definitely going to be implemented. Right now this is the intended behaviour (I need 17 inputs, the pro micro has 17 in pins and the smallest footprint I could get so it's perfect).

Thank you all for the ideas, suggestions and explanations.

CrossRoads:
I don't recall if A6, A7 have internal pullups.

They do not as they are not digital pins.

The pullups on the other "analog" pins are however very useful for taking readings of resistive devices up to 100k or so with no additional components.

alto777:
I thought you meant wiring it to pull up and/or using the internal pullup was better than pulling down.

a7

The reasons to use the long established convention to use pull-up resistors I believe are mostly legacy. I see it mentioned that it was needed for TTL circuits or for safety or fault modes. I don't think most of that applies anymore IDK.

These days the only compelling reason I see to use pull-up's is because most micro's have them built in so you don't need to add one separately.

No, the actual reason is extremely sensible.

If the switch is going to be any distance at all from the Arduino, then you have to run wires to it. You may wish to include plugs and sockets to allow connections to be changed/ moved.

If the wires are the switched wire and ground, then if a fault occurs which shorts one wire to another object or connection to another piece of equipment, the worst that is likely to happen is that it appears the switch is closed.

If however, you bring the supply voltage - 5 V or whatever - to to the switch, an accident which shorts that to ground will short out the power supply, the consequence of which depends on how well that power supply is current limited and/ or protected.

The fact it that most accidents will be a short to ground. Clearly a short to house wiring or such, another power supply would be equally bad whatever the arrangement, but is far less probable.

Also external wiring tends to pick up induced interference. You would prefer any such interference to be applied to the ground wire rather than the supply rail.

Of course, something similar but converse is true for external connections to an output device - a lamp, motor, solenoid or such. You would prefer them to be connected to ground and switch the supply to them rather than switching the ground.

Have you considered a rotary encoder, it has one push button built in and almost unlimited selections dependent on your software.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.