Built-in project · Arduino Uno R3

Button module on pin 3 lights the LED on 13, on an Arduino Uno

A button module on pin 3, its 10k holding the pin low until the button joins it to 5 V: hold it and the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Button modules with a built-in resistor
  • Reading a digital input with digitalRead
  • Remembering the last state
  • Printing only on a change
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The KY-004 button module has its own 10k resistor that holds the signal pin S at LOW. Pressing the button joins S to 5 V, so pin 3 reads HIGH.

The sketch reads pin 3 every 10 ms and copies the result to pin 13, the board's own L lamp. It also prints pressed or released, but only when the state changes.

Parts list

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

Kits that can build it

This starter kit has 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
Button module, KY-004 pin GNDArduino Uno R3 pin GND
Button module, KY-004 pin SArduino Uno R3 pin 3
Button module, KY-004 pin VCCArduino Uno R3 pin 5V

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
// Push button on pin 3 lights the LED on 13.
//
//   module S -> pin 3   + -> 5V   - -> GND   (the board's L lamp on pin 13)
//
// The module's 10k holds S LOW until the button joins it to 5 V.
//
// Hold the button.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: push button on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "pressed" : "released");
  }
  delay(10);
}

Code walkthrough

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

setup sketch.ino

Makes SENSOR (pin 3) an input and LED_PIN (pin 13) an output. It starts the serial port at 9600 baud and prints a hello message.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: push button on pin 3, the LED on pin 13");
}

loop sketch.ino

Reads pin 3 into now, where 1 means HIGH. It writes that value to the LED. If now differs from last, it saves the new value and prints pressed or released. A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "pressed" : "released");
  }
  delay(10);
}

Troubleshooting

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

  • The L lamp never lights. This project
    Check the three module wires: VCC to 5V, GND to GND, and S to pin 3.
  • Pressed and released appear swapped. This project
    This module reads HIGH when pressed. A bare button wired to GND would read the other way.
  • The output does not change, or the module seems dead. Button module
    The middle pin is power, S is the signal, and the minus pin is ground. Check the order printed on the board. Power it from 5 V or 3.3 V, and join the grounds.
  • The code thinks the button is pressed when it is not. Button module
    The board has a 10 k resistor, so S reads high at rest and low while pressed. Test for LOW as pressed. You do not need INPUT_PULLUP, though it does no harm.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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