Simple check of button and led circuit please

I have many toggle switches on my 'Flightgear' simpit. Now I wish to activate a led when the switch is actioned without using another pin and without more code.
This works in Wokwi, but in real life do I risk smoking my arduino?

const int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(115200);
}

void loop() {
Serial.print(digitalRead(buttonPin));
delay(50);
}

A LED will not reliably draw your input low when the button is not pressed.
Add a 10 k resistor in parallel with the LED.

No extra parts required if you do this.
Connect the LED/resistor between 5volt and pin.
Connect the switch between pin and ground.
Use pull up on the pin in setup(), like this.
pinMode(buttonPin, INPUT_PULLUP);
Adapt your code, because the pin is normally HIGH, and LOW when the button is pushed.
Leo..

What is really going on here? It's impossible to see how it's actually connected:
image

1 Like

Thanks for following.
So the resistor should only be in parallel with the LED not in series or I must add a second one?

The resistor always goes in series with the LED.
What aarg is confused about is the green line through the resistor body.
Leo..

The green line through the resistor a visual Wokwi problem, I could'nt resolve that.

But I should put a resisitor in parallel or series with the led? I've seen both solutions and now show my ignorance.

An additional 10k bleed resistor across the LED would be needed in your circuit (post#1)

If you follow the advice in post#3 then you only need a (1k) resistor in series with the LED.
The bleed resistor there is provided in software, with INPUT_PULLUP.
Leo..

You should add a second one from the button to gnd to be sure the pin is pulled low by default.
@Wawa's suggestion might be easier.

That diagram does not show how OP should connect the LED/switch.
Leo..

Wawa's solution works perfectly in Wokwi and I understand now.
The digital read will be tuned according to the default property behaviour.

led_switch

Thanks all.

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