Built-in project · Arduino Uno R3

On and off desk lamp: button on 7, relay on 2, on an Arduino Uno

Press the button on pin 7 (internal pull-up) once and the relay module on pin 2 clicks on and lights the lamp LED through its contact; press again and it lets go. The sketch looks for the moment of the press, not the press itself. 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 $32. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Edge detection: acting once per button press
  • Debouncing with a short delay
  • Switching a load with a relay module
  • INPUT_PULLUP wiring for a button
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

One button toggles a relay, and the relay switches the lamp. Here the lamp is a red LED behind a 220 ohm resistor. The button is on pin 7 with the internal pull-up, so it reads HIGH until pressed. The relay module's input is on pin 2.

Holding a button is one long LOW, and loop sees it thousands of times. To toggle once per press, the sketch remembers the last reading and acts only on the change from HIGH to LOW. It checks again after 20 ms to ignore contact bounce, then waits for release.

A real desk lamp would be mains power, which is why the board never touches it directly.

Parts list

6 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.

Kits that can build it

These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.

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 2aArduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Relay module pin GNDArduino Uno R3 pin GND
Pushbutton pin 1aArduino Uno R3 pin 7
Relay module pin INArduino Uno R3 pin 2
Relay module pin VCCArduino Uno R3 pin 5V
Relay module pin COMArduino Uno R3 pin 5V
Relay module pin NOResistor, 220 Ω pin 1
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
// An on and off desk lamp: one button toggles a relay, the relay the lamp.
//
//   relay module S -> pin 2   + -> 5V   - -> GND
//   relay COM -> 5V; relay NO -> 220 ohm -> LED -> GND (the "lamp")
//   button: one side -> pin 7, the other -> GND (internal pull-up)
//
// A relay is a switch worked by a magnet: pin 2 HIGH pulls the contact from
// NC across to NO, and NO feeds the lamp. Here the lamp is an LED; in a real
// lamp it would be mains, which is why the board never touches it directly.
//
// Holding a button down is one long LOW, and loop() sees it thousands of
// times. To toggle once per press, the sketch remembers what the button read
// last time and acts only when it changes from HIGH to LOW: the edge. It
// checks again 20 ms later, so a bounce of the contact is not taken for a
// press, then waits for the button to come back up.

const int RELAY = 2;
const int BUTTON = 7;

int lastButton = HIGH;
bool lampOn = false;

void setup() {
  pinMode(RELAY, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: press the button on pin 7 to switch the lamp");
}

void loop() {
  int now = digitalRead(BUTTON);
  if (lastButton == HIGH && now == LOW) {
    delay(20);
    if (digitalRead(BUTTON) == LOW) {
      lampOn = !lampOn;
      digitalWrite(RELAY, lampOn ? HIGH : LOW);
      Serial.println(lampOn ? "relay on: lamp on" : "relay off: lamp off");
      while (digitalRead(BUTTON) == LOW) {
      }
      delay(50);
      now = HIGH;
    }
  }
  lastButton = now;
}

Code walkthrough

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

setup sketch.ino

Makes RELAY an output and BUTTON an INPUT_PULLUP, so the button needs no outside resistor. It opens serial at 9600 baud.

void setup() {
  pinMode(RELAY, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: press the button on pin 7 to switch the lamp");
}

loop sketch.ino

Reads the button and looks for a change from HIGH to LOW. Then it waits 20 ms and checks it is still LOW. If so, it flips lampOn, writes the relay pin to match and prints the state. It waits until the button is released, pauses 50 ms, and updates lastButton.

void loop() {
  int now = digitalRead(BUTTON);
  if (lastButton == HIGH && now == LOW) {
    delay(20);
    if (digitalRead(BUTTON) == LOW) {
      lampOn = !lampOn;
      digitalWrite(RELAY, lampOn ? HIGH : LOW);
      Serial.println(lampOn ? "relay on: lamp on" : "relay off: lamp off");
      while (digitalRead(BUTTON) == LOW) {
      }
      delay(50);
      now = HIGH;
    }
  }
  lastButton = now;
}

Troubleshooting

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

  • The lamp flickers or toggles twice This project
    Check the 20 ms recheck is still in the code. Without it, contact bounce counts as extra presses.
  • Pressing does nothing This project
    The button must join pin 7 to GND. Check the other side is on GND, not 5V.
  • 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.