Built-in project · Arduino Uno R3

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

A push button on pin 12 with a 10k pull-up: hold it and the LED on pin 9 lights, let go and it goes out. 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Pull-up resistors
  • Reading a pin with digitalRead
  • A pressed button reads LOW
  • Using a condition to set an output
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

A 10k resistor connects pin 12 to 5 V, so the pin normally reads HIGH. The button connects pin 12 to GND. Pressing it pulls the pin down, and the pin reads LOW.

The sketch checks pin 12 over and over. While it reads LOW, it sets pin 9 HIGH, which lights the LED through a 220 ohm resistor. Let go, and the LED goes out.

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 controls an LED: hold it to light it.

//   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.

const int BUTTON = 12;
const int LED_PIN = 9;

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

void loop() {
  digitalWrite(LED_PIN, digitalRead(BUTTON) == LOW ? HIGH : LOW);
}

Code walkthrough

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

setup sketch.ino

Sets BUTTON (pin 12) as an input and LED_PIN (pin 9) as an output. It also starts the serial port at 9600 baud and prints a hello message.

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

loop sketch.ino

Each pass reads the button with digitalRead. If it is LOW (pressed), digitalWrite sends HIGH to the LED pin. Otherwise it sends LOW. There is no delay, so the LED follows the button at once.

void loop() {
  digitalWrite(LED_PIN, digitalRead(BUTTON) == LOW ? HIGH : LOW);
}

Troubleshooting

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

  • The LED is always on, or always off. This project
    Check the 10k resistor. One end goes to 5V and the other to pin 12, where the button also connects.
  • The LED lights when the button is released. This project
    The pin reads LOW when pressed. Keep the test as == LOW.
  • 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.