Built-in project · Arduino Uno R3

Starship control panel on an Arduino Uno

A green lamp waits and two red lamps flash in turn while you hold the button, read through a 10k pull-down on pin 2. The whole build, an Arduino Uno and 8 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.

Build it as the Starter Projects Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $48, free on Mokxi.

Starship control panel · Arduino Uno R3live0.000 s 0.00x
Hold the button
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

  • A pull-down resistor on a button input
  • Detecting a button change and printing once
  • Controlling several LEDs with digitalWrite
  • Timing a flash with delay()
  • How a pull-down differs from INPUT_PULLUP
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 10

How it works

A pushbutton on pin 2 has a 10k pull-down resistor, so the pin reads LOW when the button is up and HIGH when pressed. A green LED on pin 3 and two red LEDs on pins 4 and 5 each have a 220 ohm resistor.

While the button is up, the green lamp is on and the reds are off. While you hold it, green goes off and the two reds take turns every quarter second. The serial monitor prints launch! and ready when the button changes.

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
Resistor, 10k Ω pin 1Arduino Uno R3 pin GND
LED, green pin CArduino Uno R3 pin GND
LED, red (1) pin CArduino Uno R3 pin GND
LED, red (2) pin CArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 3
Pushbutton pin 2aArduino Uno R3 pin 2
Pushbutton pin 1aArduino Uno R3 pin 5V
Resistor, 10k Ω pin 2Pushbutton pin 2b
LED, green pin AResistor, 220 Ω (1) pin 2
LED, red (1) pin AResistor, 220 Ω (2) pin 2
LED, red (2) pin AResistor, 220 Ω (3) 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
// Starship control panel: a green "ready" lamp, and two red lamps that flash
// in turn while the launch button is held.
//
//   pin 2   the button: one leg to 5V, the other to pin 2 and through a 10k
//           resistor to GND (a pull-down)
//   pin 3   the green LED, through 220 ohm to GND
//   pin 4   the first red LED, through 220 ohm to GND
//   pin 5   the second red LED, through 220 ohm to GND
//
// The 10k resistor holds pin 2 at 0 V while the button is up, so it reads LOW
// rather than floating. Press the button and it joins pin 2 to 5 V: HIGH.
// This is the opposite of INPUT_PULLUP, where pressed reads LOW.

const int BUTTON = 2;
const int GREEN = 3;
const int RED_A = 4;
const int RED_B = 5;

int lastButton = LOW;

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED_A, OUTPUT);
  pinMode(RED_B, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: starship control panel");
}

void loop() {
  int button = digitalRead(BUTTON);
  if (button != lastButton) {
    lastButton = button;
    Serial.println(button == HIGH ? "launch!" : "ready");
  }

  if (button == LOW) {
    // Waiting: green on, both reds off.
    digitalWrite(GREEN, HIGH);
    digitalWrite(RED_A, LOW);
    digitalWrite(RED_B, LOW);
  } else {
    // Held: green off, the reds take turns every quarter second.
    digitalWrite(GREEN, LOW);
    digitalWrite(RED_A, HIGH);
    digitalWrite(RED_B, LOW);
    delay(250);
    digitalWrite(RED_A, LOW);
    digitalWrite(RED_B, HIGH);
    delay(250);
  }
}

Code walkthrough

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

setup sketch.ino

Sets BUTTON as an input and the three LED pins as outputs. The 10k resistor on the button does the pulling down, so no internal pull-up is used. It then opens the serial port and prints a hello line.

void setup() {
  pinMode(BUTTON, INPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(RED_A, OUTPUT);
  pinMode(RED_B, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: starship control panel");
}

loop sketch.ino

Reads the button and prints launch! or ready only when it changes. If the button is LOW, green is on and both reds are off. If it is HIGH, green is off and the two reds light in turn, 250 ms each.

void loop() {
  int button = digitalRead(BUTTON);
  if (button != lastButton) {
    lastButton = button;
    Serial.println(button == HIGH ? "launch!" : "ready");
  }

  if (button == LOW) {
    // Waiting: green on, both reds off.
    digitalWrite(GREEN, HIGH);
    digitalWrite(RED_A, LOW);
    digitalWrite(RED_B, LOW);
  } else {
    // Held: green off, the reds take turns every quarter second.
    digitalWrite(GREEN, LOW);
    digitalWrite(RED_A, HIGH);
    digitalWrite(RED_B, LOW);
    delay(250);
    digitalWrite(RED_A, LOW);
    digitalWrite(RED_B, HIGH);
    delay(250);
  }
}

Troubleshooting

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

  • Pin 2 seems to flicker without touching the button This project
    The 10k resistor must connect pin 2 to ground, so the pin never floats.
  • The reds respond slowly to a quick press This project
    Each red flash lasts 250 ms with delay, so the button is only read between flashes.
  • 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.