I'm experimenting with buttons and LEDs and I would like my code to change a LED's brightness once a button is pressed. I connect the LED to the PWD digital pin.

My circuit looks like this one:

enter image description here

This is the code I'm using:

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

void setup() {
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);

boolean debounce(boolean last) {
    boolean current = digitalRead(switchPin);
    if (last != current) {
        current = digitalRead(switchPin);
return current;

void loop() {
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH) {
        ledLevel = ledLevel + 51;
    lastButton = currentButton;

    if (ledLevel > 255) ledLevel = 0;
    analogWrite(ledPin, ledLevel);

Though, the LED never turns on, but the light on the Arduino is on. I made sure the leads of the LED were in the right spot. Why doesn't the LED turn on?


Because you don't actually have a circuit? One side of the LED doesn't connect to anything.

  • Thanks a lot for your answer, Majenko. Do I have to use a wire to connect things. Could you please propose a fix for this? Thanks. As you figured out, I'm very new to this.
    – Cesare
    Sep 11 '15 at 17:57
  • 2
    One leg of the LED connects to the strip with a red line painted next to it. That then doesn't connect to anything unless you connect it to something. You should connect it to either +5V or GND depending on which way round your LED is. I can never tell which way the LEDs are in those awful fritzing drawings.
    – Majenko
    Sep 11 '15 at 17:59
  • 1
    Those breadboards do not automatically connect the top red line to the bottom red line (nor the blue ones). You have to do that yourself, if that is what you want.
    – Nick Gammon
    Sep 11 '15 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.