Built-in project · Arduino Uno R3

Doorbell: button on pin 2, buzzer on pin 8, on an Arduino Uno

Press the button on pin 2, held low by a 10k pull-down, and the buzzer on pin 8 plays a two-note ding-dong. The whole build, an Arduino Uno and 3 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 $31. On Mokxi they're free.

Doorbell: button on pin 2, buzzer on pin 8 · Arduino Uno R3live0.000 s 0.00x
Press 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

  • Reading a button with a pull-down resistor
  • Making notes with tone
  • Making a decision with if
  • Frequency and pitch
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

One side of the button goes to 5V. The other side goes to pin 2 and to ground through a 10k resistor. That resistor is a pull-down: it holds pin 2 at LOW until you press, and then 5V wins and the pin reads HIGH.

While the button is held, the sketch plays two notes on the buzzer on pin 8: a higher one at 659 Hz, then a lower one at 523 Hz. It prints "ding" and "dong" as it goes. Release the button and it stays quiet.

Parts list

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

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
Piezo speaker pin 2Arduino Uno R3 pin GND
Piezo speaker pin 1Arduino Uno R3 pin 8
Pushbutton pin 2aArduino Uno R3 pin 2
Pushbutton pin 1aArduino Uno R3 pin 5V
Resistor, 10k Ω pin 2Pushbutton pin 2b

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 doorbell: press the button and the buzzer plays ding-dong.
//
//   one side of the button -> 5V, the other -> pin 2 and 10k to GND
//   buzzer + -> pin 8, buzzer - -> GND
//
// The button reads HIGH while it is pressed, thanks to the 10k pull-down.
// tone() swings pin 8 at a frequency, and the buzzer turns that into a note:
// a high one, then a lower one.

const int BUTTON = 2;
const int BUZZER = 8;

void setup() {
  pinMode(BUTTON, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: doorbell. Press the button on pin 2.");
}

void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    Serial.println("ding");
    tone(BUZZER, 659, 300);
    delay(350);
    Serial.println("dong");
    tone(BUZZER, 523, 500);
    delay(800);
  }
}

Code walkthrough

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

setup sketch.ino

Sets BUTTON (pin 2) as an input and starts serial at 9600 baud. The buzzer pin does not need setup because tone handles it.

void setup() {
  pinMode(BUTTON, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: doorbell. Press the button on pin 2.");
}

loop sketch.ino

If digitalRead(BUTTON) is HIGH, it plays 659 Hz for 300 ms with tone, waits 350 ms, then plays 523 Hz for 500 ms and waits 800 ms. If the button is not pressed, nothing happens.

void loop() {
  if (digitalRead(BUTTON) == HIGH) {
    Serial.println("ding");
    tone(BUZZER, 659, 300);
    delay(350);
    Serial.println("dong");
    tone(BUZZER, 523, 500);
    delay(800);
  }
}

Troubleshooting

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

  • It rings without a press, or erratically This project
    Without the 10k pull-down the pin floats. Check the resistor goes from pin 2's line to GND.
  • No sound when pressed This project
    Check the buzzer is on pin 8 and ground, and the button connects 5V to pin 2.
  • 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.
  • A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
    A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
  • Sound is very quiet or the pin seems overloaded. Piezo speaker
    Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.

Change it and keep it

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