A proper way to use a button and an LED

Could somebody please explain the reason the following circuit works "fine" (LED is on if the button is pressed) only if the Pin 12 is in the OUTPUT-mode?

I need to be able to digitalRead() the button state (pressed/not pressed) and the LED should be on every time the button is pressed without using two pins (one input and one output).

What is the proper way to achieve that?

With the button example sketches that ship with the IDE.

It works only when pin 12 is configured as an output, because that is the only way the pin can deliver current.

But the cathode of the LED is connected to the Pin 12, not the anode, so the LED shouldn't be working at all?

I don't understand your thinking, but it is wrong. Try connecting the same black wire to ground instead of pin 12. Now it should work, and you can disconnect and sell the Arduino. :slight_smile:

1 Like

and you can disconnect and sell the Arduino

It's a funny comment for sure, but as I mentioned before I need to be able to read the state of the button and turn the LED on without using a second pin just for that.

So I'm trying to avoid using one input pin (for the button) and one output pin (for the LED) in this "standard" circuit:

button_and_led2

Then put the switch and LED circuit in parallel. Your series circuit will get you in trouble with the LED's forward voltage bias.

Try this with

pinMode(12,INPUT_PULLUP);

LED resistor 220~470, switch resistor ~10K

1 Like

Yes, like that. Actually you can remove the 10k with using INPUT_PULLUP.

1 Like

How wrong is this one? I'm reading 1 if the button isn't pressed and 0 if it is pressed.

Looks like a similar situation (one pin, one LED, one button) is described here:

It's the same circuit @mancera1979 posted.
The only difference is that it takes about 200 times longer to read in this wayward Fritzy kind of form.

1 Like

Great! It works almost as it should. Is there a way to get actually 0 instead of 1 if the button is not pressed?

You could budge something together with a mosfet or npn transistor to buffer the input to the pin. But why bother? In your code it doesn't really matter if 1 is 'pressed' or 0.

2 Likes

Why on earth would you want that? :roll_eyes:

bool isButtonPressed() {
  return digitalRead(buttonPin) == LOW;
}

or however you like

I'm trying to make a simple circuit with as few potential for things going wrong as possible for young students. For example it surely makes no difference for me, if it's 1 or 0, and I'm certainly able to get the value I want with code, but for a young student, trying to build his or hers first circuit, it's definitely not that simple.

I was looking for a way to use one pin less without additional parts or code, or understanding how the circuit works, but it seems to be impossible, so I should (and will) rather stick to the classic "one pin as input, one pin as output" example.

No, it doesn't, but it'll require additional electronics. So either the studens will have to understand what the inverting logic port of whatever implementation on their breadboard is for, or they have to wrap their head around the concept of "microcontroller sees high voltage when button is not pressed and vice versa". There is indeed no solution that's simpler on both fronts.
And now of course you'll still have to explain to them why the button-associated led isn't physically connected with the button...
I'm afraid you're not going to get out of explaining them something, one way or the other :wink:

No harm to try!!
sw1led1x

void setup()
{
  pinMode(2, INPUT_PULLUP);
  while (digitalRead(2) != LOW)
  {
    ;
  }
  delay(1000);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
}

void loop() 
{

}
1 Like

Using a LED and switch on the same I/O pin, not for the faint of heart:

3 Likes

Whether or not it is simple, it is important for any student not to imagine that there is a strict causal relationship between positive and negative, "high" and "low" - and "on" and "off".

Otherwise they will become - as some enquirers here have proven to be - terribly confused! :astonished:

as some enquirers here have proven to be - terribly confused!

I'm quite sure that a "stupid" question is much better than no question at all, but thank you for the pointing out that I have a lot to learn about circuits! I surely do :slight_smile: