Project 2: LEDs not working

I'm working on project 2 from the book and I've gotten it all wired up and have uploaded the code but my LEDs are not working. I took the code from the Examples section of the Arduino IDE so I would think that it's right. The LEDs are in the right way. I've been over my wiring many times and I think it's also right but it isn't working so something must be wrong. I've added a picture of my wiring as well as the picture of how it should be wired from the book. Thanks!


It appears correct, try unplugging one of the LEDs and reverse it.

So far everything looks correct.
If you have a voltmeter, check if your LEDs get any voltage.
But if you don't, try testing if your LEDs are working by wiring them to your GND and +5V with a series resistor like this picture.

You will need to show the code.

@al07
From the image posted, it appears that the LEDs are mounted correctly.

FYI

image

I tried, nothing changed.

I don't have a voltmeter, I checked all the LEDs the way you described and they all worked.

int switchstate = 0;

void setup() {
  // declare the LED pins as outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // declare the switch pin as an input
  pinMode(2, INPUT);
}

void loop() {

  // read the value of the switch
  // digitalRead() checks to see if there is voltage on the pin or not
  switchstate = digitalRead(2);

  // if the button is not pressed turn on the green LED and off the red LEDs
  if (switchstate == LOW) {
    digitalWrite(3, HIGH);  // turn the green LED on pin 3 on
    digitalWrite(4, LOW);   // turn the red LED on pin 4 off
    digitalWrite(5, LOW);   // turn the red LED on pin 5 off
  }
  // this else is part of the above if() statement.
  // if the switch is not LOW (the button is pressed) turn off the green LED and
  // blink alternatively the red LEDs
  else {
    digitalWrite(3, LOW);   // turn the green LED on pin 3 off
    digitalWrite(4, LOW);   // turn the red LED on pin 4 off
    digitalWrite(5, HIGH);  // turn the red LED on pin 5 on
    // wait for a quarter second before changing the light
    delay(250);
    digitalWrite(4, HIGH);  // turn the red LED on pin 4 on
    digitalWrite(5, LOW);   // turn the red LED on pin 5 off
    // wait for a quarter second before changing the light
    delay(250);
  }
}

Here's the code.

Your code is good.

Your power connection is bad. [edit] "power distribution"[/edit]

Simulated in Wokwi.com...

The code
int switchstate = 0;

void setup() {
  // declare the LED pins as outputs
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // declare the switch pin as an input
  pinMode(2, INPUT);
}

void loop() {

  // read the value of the switch
  // digitalRead() checks to see if there is voltage on the pin or not
  switchstate = digitalRead(2);

  // if the button is not pressed turn on the green LED and off the red LEDs
  if (switchstate == LOW) {
    digitalWrite(3, HIGH);  // turn the green LED on pin 3 on
    digitalWrite(4, LOW);   // turn the red LED on pin 4 off
    digitalWrite(5, LOW);   // turn the red LED on pin 5 off
  }
  // this else is part of the above if() statement.
  // if the switch is not LOW (the button is pressed) turn off the green LED and
  // blink alternatively the red LEDs
  else {
    digitalWrite(3, LOW);   // turn the green LED on pin 3 off
    digitalWrite(4, LOW);   // turn the red LED on pin 4 off
    digitalWrite(5, HIGH);  // turn the red LED on pin 5 on
    // wait for a quarter second before changing the light
    delay(250);
    digitalWrite(4, HIGH);  // turn the red LED on pin 4 on
    digitalWrite(5, LOW);   // turn the red LED on pin 5 off
    // wait for a quarter second before changing the light
    delay(250);
  }
}
diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 4.8, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -35,
      "left": 163.4,
      "rotate": 180,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -53.65,
      "left": 0,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -109.2,
      "left": 80.6,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": -109.2,
      "left": 100.2,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": -109.2,
      "left": 61.8,
      "attrs": { "color": "red", "flip": "1" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": -44.05,
      "left": 0,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": -34.45,
      "left": 0,
      "attrs": { "value": "1000" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r4",
      "top": -35,
      "left": 114.35,
      "rotate": 270,
      "attrs": { "value": "1000" }
    }
  ],
  "connections": [
    [ "nano:GND.2", "led2:C", "black", [ "v0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v-62.4", "h-19.7" ] ],
    [ "nano:GND.2", "led3:C", "black", [ "v-62.4", "h-38.9" ] ],
    [ "r1:2", "led3:A", "green", [ "v0", "h18" ] ],
    [ "led1:A", "r2:2", "green", [ "v0" ] ],
    [ "r3:2", "led2:A", "green", [ "v0", "h46.8" ] ],
    [ "r3:1", "nano:3", "green", [ "v0", "h-9.6", "v9.6", "h124.8" ] ],
    [ "r2:1", "nano:4", "green", [ "v0", "h-19.2", "v28.8", "h115.2" ] ],
    [ "r1:1", "nano:5", "green", [ "v0", "h-28.8", "v48", "h105.6" ] ],
    [ "nano:2", "r4:1", "green", [ "v0" ] ],
    [ "r4:1", "btn1:1.r", "green", [ "h0" ] ],
    [ "nano:GND.2", "r4:2", "black", [ "v0" ] ],
    [ "nano:5V", "btn1:2.l", "red", [ "v9.6", "h115.2", "v-96" ] ]
  ],
  "dependencies": {}
}

In what way is it bad and how can I fix it?

Measure the voltage of the red and black wire at the breadboard "rails" (the red line and blue line).

I don't have a voltmeter but if I run a wire from the red rail to my LEDs they light up.

  1. Remove the yellow wire to one LED at the breadboard.
  2. Move the red wire from the Vcc rail to where the yellow wire was.

The LED cathode, resistor and ground rail still complete the path to ground, so this should light up the LED. If the LED does not light... there is a power problem.

The LED lit up when I did that

Others have said the wiring looks good, perhaps they are right but I can't see from the photo whether the resistors are connected to the positive (red) rail (wrong) or the negative (blue) rail (right). Please check and confirm.

Edit: your reply #15 came just before I posted this reply, your test suggests the LEDs are correctly wired.

Well done for a nice, clear question with both an image of what you are trying to do and what you actually have, along with correctly posted code.

1 Like

Follow #1 and #2 with the other two LEDs.

The resistors are connected to the negative rail.

Thank you! I'd never posted to a forum before this so that's nice to hear!

I did, they all work.

1 Like

The next thing I'm wondering about is whether your code uploaded correctly, what was the last thing in it said in progress (or whatever it's called) window when you did the upload to the Uno?

It says Done Uploading.

1 Like