Built-in project · Arduino Uno R3

Sleigh-bell doorbell on an Arduino Uno

Press the doorbell and a servo shakes a strap of real sleigh bells in time while a speaker plays the chorus of Jingle Bells. 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.

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

Sleigh-bell doorbell · Arduino Uno R3live0.000 s 0.00x
Press Run, then press the doorbell
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

  • Playing a melody with tone
  • Notes and beats stored in arrays
  • Moving a servo in time with sound
  • Why a small speaker needs a series resistor
  • A button with INPUT_PULLUP
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

Press the doorbell button on pin 2 and two things happen together. A speaker on pin 8, through a 100 Ω resistor, plays the chorus of Jingle Bells. A servo on pin 9 swings back and forth to shake a strap of sleigh bells.

The servo swings to 60 degrees on one note and 120 on the next, so the bells ring in time with the tune. When the tune ends, the servo returns to 90.

While it plays, extra presses are ignored. After the tune it rests for 1.5 seconds, so a long press gives one chorus.

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
Hobby servo pin PWMArduino Uno R3 pin 9
Resistor, 100 Ω pin 1Arduino Uno R3 pin 8
Pushbutton pin 1aArduino Uno R3 pin 2
Hobby servo pin VCCArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
Speaker, 8 ohm pin 2Arduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Speaker, 8 ohm pin 1Resistor, 100 Ω 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
// Sleigh-bell doorbell: press the doorbell and a servo shakes a strap of real
// sleigh bells while a speaker plays the chorus of Jingle Bells.
//
//   doorbell   pin 2 -> button -> GND   (INPUT_PULLUP, so pressed reads LOW)
//   speaker    pin 8 -> 100 ohms -> 8 ohm speaker -> GND
//   servo      PWM -> pin 9, VCC -> 5 V, GND -> GND
//              tie a short strap of sleigh bells to the horn
//
// The servo swings the bells one way on every note and back on the next, so
// they ring in time with the tune. Real bells sound better than any speaker,
// and the tune makes it obvious it is the doorbell and not a passing sleigh.
//
// The speaker is 8 ohms, which is nearly a short across a pin, so it goes
// through 100 ohms, as in the Arduino's own melody example. For a louder bell,
// drive the speaker from a small amplifier module instead.
//
// While it plays, more presses are ignored, and it rests for a moment after,
// so a visitor leaning on the button gets one chorus, not five.
//
// On the porch: an outdoor button and its wire can pick up noise, so keep the
// run short or add a 100 nF capacitor across the button. Give the servo its
// own 5 V supply with the grounds joined.
//
// Things to try. Press the doorbell. Change TEMPO_MS for a faster jingle.
// Write in the verse as a second tune and play it on every other press.

#include <Servo.h>

const int BUTTON_PIN = 2;
const int SPEAKER_PIN = 8;
const int BELLS_PIN = 9;
const int TEMPO_MS = 180;   // one beat

const int E5 = 659, F5 = 698, G5 = 784, C5 = 523, D5 = 587;

// "Jingle bells, jingle bells, jingle all the way..." as notes and beats.
const int TUNE[] = {E5, E5, E5, E5, E5, E5, E5, G5, C5, D5, E5,
                    F5, F5, F5, F5, F5, E5, E5, E5, E5, D5, D5, E5, D5, G5};
const uint8_t BEATS[] = {1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4,
                         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
const int NOTES = sizeof(TUNE) / sizeof(TUNE[0]);

Servo bells;
unsigned int rings = 0;

void jingle() {
  rings++;
  Serial.print("Ding dong, ring ");
  Serial.println(rings);
  bool left = true;
  for (int i = 0; i < NOTES; i++) {
    int ms = BEATS[i] * TEMPO_MS;
    tone(SPEAKER_PIN, TUNE[i], ms - 30);   // a short gap keeps repeated notes apart
    bells.write(left ? 60 : 120);          // shake the bells on every note
    left = !left;
    delay(ms);
  }
  noTone(SPEAKER_PIN);
  bells.write(90);
  Serial.println("Jingle done");
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  bells.attach(BELLS_PIN);
  bells.write(90);
  Serial.println("Sleigh-bell doorbell ready");
}

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    jingle();
    delay(1500);   // rest, so one long press is one chorus
  }
}

Code walkthrough

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

jingle sketch.ino

Adds one to rings and prints a message. Then for each note it works out the length from BEATS and TEMPO_MS, plays the pitch for 30 ms less than that so repeated notes stay separate, and swings the servo to the other side. At the end it silences the speaker and recenters the servo.

void jingle() {
  rings++;
  Serial.print("Ding dong, ring ");
  Serial.println(rings);
  bool left = true;
  for (int i = 0; i < NOTES; i++) {
    int ms = BEATS[i] * TEMPO_MS;
    tone(SPEAKER_PIN, TUNE[i], ms - 30);   // a short gap keeps repeated notes apart
    bells.write(left ? 60 : 120);          // shake the bells on every note
    left = !left;
    delay(ms);
  }
  noTone(SPEAKER_PIN);
  bells.write(90);
  Serial.println("Jingle done");
}

setup sketch.ino

Sets the button to INPUT_PULLUP, attaches the servo to pin 9 and centers it at 90 degrees.

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  bells.attach(BELLS_PIN);
  bells.write(90);
  Serial.println("Sleigh-bell doorbell ready");
}

loop sketch.ino

Waits for the button to read LOW. When it does, it plays jingle and then waits 1500 ms.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    jingle();
    delay(1500);   // rest, so one long press is one chorus
  }
}

Troubleshooting

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

  • The speaker is very quiet or the pin gets hot This project
    Keep the 100 Ω resistor in series. An 8 ohm speaker straight on a pin is nearly a short.
  • The jingle is too slow or too fast This project
    Change TEMPO_MS, the length of one beat, now 180 ms.
  • 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 pin gets hot or the Arduino resets when the speaker plays. Speaker
    An 8 ohm speaker on 5 V would pull over 600 mA. Put at least 100 ohms in series, or drive it with a transistor from a separate supply.
  • Only a click or hum, not a tone. Speaker
    Use tone(pin, frequency) to make a square wave. Plain digitalWrite HIGH just pushes DC through the coil. Never leave the pin HIGH when you are done.

Change it and keep it

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