Built-in project · Arduino Uno R3

Push button on pin 12 toggles the LED on pin 9, on an Arduino Uno

Each press of the button on pin 12 (10k pull-up) flips the LED on pin 9: on, then off. The sketch waits out the contact’s bounce before it believes a press. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

Push button on pin 12 toggles the LED on pin 9 · Arduino Uno R3live0.000 s 0.00x
Press the button, let go, and press it again
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Pull up resistors and active low buttons
  • Switch bounce and a simple debounce
  • A boolean variable that remembers state
  • Toggling with !
  • Waiting for release so one press counts once
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

One press turns the LED on and the next press turns it off. The button is on pin 12 with a 10k resistor to 5 V. That resistor holds the pin high, and pressing the button connects it to ground so it reads LOW.

When the sketch sees LOW, it waits 10 ms and checks again. Real buttons bounce for a few milliseconds, and this second look filters it out. Then it flips the lit variable and writes it to pin 9.

After that it waits in a while loop until the button is released, so holding the button counts as one press only.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Pushbutton pin 1aArduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin 12
Resistor, 10k Ω pin 1Arduino Uno R3 pin 12
Resistor, 220 Ω pin 1Arduino Uno R3 pin 9
Resistor, 10k Ω pin 2Arduino Uno R3 pin 5V
LED, red pin AResistor, 220 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A push button toggles an LED: press once for on, again for off.

//   pin 9 -> 220 ohm -> LED -> GND
//   10k from 5V -> pin 12; the push button from pin 12 -> GND
//
// The 10k pull-up holds pin 12 HIGH while the button is up; pressing it
// joins the pin to ground and it reads LOW.

// A contact bounces for a few milliseconds as it closes, so one press can
// look like several. The sketch reads the button, waits 10 ms and reads it
// again; only a press that is still there counts. Then it waits for the
// button to come back up before it will count another.

const int BUTTON = 12;
const int LED_PIN = 9;
bool lit = false;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: press the button on pin 12 to toggle the LED");
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    delay(10);
    if (digitalRead(BUTTON) == LOW) {
      lit = !lit;
      digitalWrite(LED_PIN, lit ? HIGH : LOW);
      Serial.println(lit ? "LED on" : "LED off");
      while (digitalRead(BUTTON) == LOW) {
      }
      delay(10);
    }
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets the button pin as an input and the LED pin as an output, and starts serial. The pull up here is the external 10k resistor, so pinMode just uses INPUT.

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: press the button on pin 12 to toggle the LED");
}

loop sketch.ino

Reads the button. If LOW, waits 10 ms and reads again. If still LOW, it flips lit, updates the LED, prints "LED on" or "LED off", and holds in a while loop until the button comes back up.

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    delay(10);
    if (digitalRead(BUTTON) == LOW) {
      lit = !lit;
      digitalWrite(LED_PIN, lit ? HIGH : LOW);
      Serial.println(lit ? "LED on" : "LED off");
      while (digitalRead(BUTTON) == LOW) {
      }
      delay(10);
    }
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED flickers or toggles twice This project
    Keep the 10 ms recheck. Without it, bounce looks like extra presses.
  • The button seems always pressed This project
    The 10k resistor must go from pin 12 to 5V, and the button from pin 12 to ground.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.