Two buttons, one LED, on an Arduino Uno
Button A on pin 9 lights the LED on pin 5 and button B on pin 8 puts it out. Neither button needs a resistor: the chip’s own pull-ups hold both pins high. 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.
What you'll learn
- Using
INPUT_PULLUPwith two buttons - Active low inputs
- A variable that remembers state
- Latching behavior from two buttons
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
Two buttons control one LED. Button A on pin 9 turns it on, and button B on pin 8 turns it off. The LED is on pin 5 through a 220 ohm resistor.
Neither button needs a resistor. INPUT_PULLUP turns on the chip's own pull up, so a pin reads HIGH when the button is up and LOW when held.
The LED stays as the last button left it. A variable named lit remembers the state, and the sketch prints "on" or "off" only when it changes.
Parts list
6 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 2 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
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 pin | Connects to |
|---|---|
| LED, red pin C | Arduino Uno R3 pin GND |
| Pushbutton (1) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (2) pin 2a | Arduino Uno R3 pin GND |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin 9 |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin 8 |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 5 |
| LED, red pin A | Resistor, 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.
// Two buttons, one LED: the left button turns it on, the right one off.
//
// pin 5 the LED, through 220 ohm to GND
// pin 9 button A (on), its other leg to GND
// pin 8 button B (off), its other leg to GND
//
// Neither button has a resistor. INPUT_PULLUP switches on the chip's own
// pull-up, which holds the pin at 5 V until the button joins it to ground.
// So a pin reads HIGH while its button is up and LOW while it is held.
//
// The LED stays the way the last button left it. That is the difference from
// a button wired straight to an LED: the board remembers.
const int LED_PIN = 5;
const int BUTTON_ON = 9;
const int BUTTON_OFF = 8;
bool lit = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_ON, INPUT_PULLUP);
pinMode(BUTTON_OFF, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Mokxi Uno: button A on pin 9 lights the LED, button B on pin 8 puts it out");
}
void loop() {
if (digitalRead(BUTTON_ON) == LOW && !lit) {
lit = true;
digitalWrite(LED_PIN, HIGH);
Serial.println("on");
}
if (digitalRead(BUTTON_OFF) == LOW && lit) {
lit = false;
digitalWrite(LED_PIN, LOW);
Serial.println("off");
}
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes the LED pin an output, sets both button pins to INPUT_PULLUP, and starts the serial port with a message.
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_ON, INPUT_PULLUP);
pinMode(BUTTON_OFF, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Mokxi Uno: button A on pin 9 lights the LED, button B on pin 8 puts it out");
}loop sketch.ino
Checks both buttons. If A is LOW and the LED is off, it turns it on. If B is LOW and the LED is on, it turns it off. Both print a message. A 5 ms delay ends each pass.
void loop() {
if (digitalRead(BUTTON_ON) == LOW && !lit) {
lit = true;
digitalWrite(LED_PIN, HIGH);
Serial.println("on");
}
if (digitalRead(BUTTON_OFF) == LOW && lit) {
lit = false;
digitalWrite(LED_PIN, LOW);
Serial.println("off");
}
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The buttons seem swapped This project
Button A goes to pin 9 and turns the LED on. Button B goes to pin 8 and turns it off. - The LED never lights This project
Both buttons need their other leg on ground. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.