r/arduino • • Oct 15 '26

Beginner Help

(Solved)

I know this is going to be a dumb fix, but for the life of me I cannot figure out why the LED is not coming on. I tried the LED both ways but still nothing. (220ohm resistor used)

const int slidepin = 2;

const int ledpin = 9;

int swstate = 0;

void setup()

{

pinMode(slidepin, INPUT);

pinMode(ledpin, OUTPUT);

Serial.begin(9600);

}

void loop(){

swstate = digitalRead(slidepin);

if(swstate == 0){

digitalWrite(ledpin, HIGH);

Serial.println("HIGH");

}

else{

digitalWrite(ledpin, LOW);

Serial.println("LOW");

}

delay(100);

}

Toggle switch HIGH
2 Upvotes

7 comments sorted by

6

u/GypsumFantastic25 Anti Spam Sleuth Oct 15 '26

The LED is connected to the top + rail.

The top + rail isn't connected to anything.

1

u/ventus1b Oct 15 '26

In your images, the second pin of the LED is connected to the top red rail, which isn’t connected to anything.

1

u/AbruptOyster456 Oct 15 '26

Thank you!! So dumb lol

1

u/n_sh_44 Oct 15 '26

Since the serial monitor prints HIGH, the code is running that branch and pin 9 is being driven high, so it's almost certainly wiring, not code. Quick checks: pull the LED and resistor off and put a multimeter (or the LED+resistor alone) from pin 9 to GND, you should see ~5V. Make sure the LED's short leg goes to a GND that's actually connected to the Arduino's GND (a lot of breadboards have the power rails split in the middle, so the rail can look connected but isn't). And confirm the resistor and LED are in different rows, not both legs in the same 5-hole strip, which shorts them out.

Separate issue: with pinMode(slidepin, INPUT) the switch pin floats when the switch is open, so it'll read random values. Use INPUT_PULLUP and wire the switch between pin 2 and GND (it'll read 0 when closed).

1

u/AbruptOyster456 Oct 15 '26

Thank you! WDYM by "Use INPUT_PULLUP and wire the switch between pin 2 and GND".

1

u/n_sh_44 Oct 15 '26

In code, change pinMode(slidepin, INPUT) to pinMode(slidepin, INPUT_PULLUP). That turns on a resistor inside the chip that pulls pin 2 up to 5V, so it reads HIGH when the switch is open instead of floating.

Then wire one side of the switch to pin 2 and the other side to GND (nothing to 5V). Closing the switch connects pin 2 to ground, so it reads LOW. No external resistor needed, just remember the logic is flipped: LOW = switch closed, HIGH = open.