Built-in project · Arduino Uno R3

New Year's ball drop on an Arduino Uno

Press the button and a servo lowers a ball of LEDs over the last ten seconds, beeping each second, then the ball bursts into a rainbow and a piezo cheers. The whole build, an Arduino Uno and 5 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 25 minutes to build and run. Runs in your browser. Free, and no account needed.

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

New Year's ball drop · Arduino Uno R3live0.000 s 0.00x
Press Run, then press the button to start the countdown
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

  • Driving a servo from the clock with millis
  • Addressable LEDs on one data pin
  • Button with INPUT_PULLUP
  • Non-blocking timing with a started-at time
  • Playing notes with tone
  • Difficulty: Beginner
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 7

How it works

Press the button on pin 2 and a ten-second countdown starts. A servo on pin 9 lowers a ring of 12 LEDs from 180 degrees at the top of the pole to 0 at the bottom.

The angle follows the clock, not the beeps, so the ball glides. The piezo on pin 8 beeps once a second, and the last three beeps are higher. At zero a short tune plays and the ring spins through a rainbow for 8 seconds.

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
Piezo speaker pin 1Arduino Uno R3 pin 8
Resistor, 330 Ω pin 1Arduino Uno R3 pin 6
Pushbutton pin 1aArduino Uno R3 pin 2
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
Piezo speaker pin 2Arduino Uno R3 pin GND
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Addressable RGB LEDs pin DINResistor, 330 Ω 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
// New Year's ball drop: a ball of LEDs on a servo arm comes down over the
// last ten seconds, with a beep for every second, then lights up in a burst
// of color as the new year arrives.
//
//   button    pin 2 -> button -> GND        (INPUT_PULLUP, pressed reads LOW)
//   servo     PWM -> pin 9, VCC -> 5 V, GND -> GND
//   piezo     pin 8 -> piezo -> GND
//   ball      12 WS2812B LEDs in a ring, DIN <- 330 ohms <- pin 6
//
// Press the button and the countdown starts. The servo moves from 180 degrees
// (the top of the pole) to 0 (the bottom) in small steps across ten seconds,
// so the ball glides rather than jumping once a second. The last three beeps
// are higher, the way a crowd gets louder at "three, two, one".
//
// Build it with a ping-pong ball or a small plastic ornament around the LED
// ring, hung on a thread from the servo horn, or mount the servo at the top
// of a cardboard pole and let the arm lower the ball.
//
// Things to try. Change COUNT_FROM to 60 for a full minute. Write your own
// tune into CHEER.

#include <Servo.h>
#include <Adafruit_NeoPixel.h>

const int BUTTON_PIN = 2;
const int SERVO_PIN = 9;
const int PIEZO_PIN = 8;
const int BALL_PIN = 6;
const int LEDS = 12;
const int COUNT_FROM = 10;
const int TOP = 180, BOTTOM = 0;

Servo pole;
Adafruit_NeoPixel ball(LEDS, BALL_PIN, NEO_GRB + NEO_KHZ800);

const int CHEER[] = {523, 659, 784, 1047, 784, 1047};
const int CHEER_MS[] = {120, 120, 120, 300, 120, 500};

bool running = false;
unsigned long startedAt = 0;
int lastSecondShown = -1;
unsigned long celebrateUntil = 0;

void glow(uint8_t r, uint8_t g, uint8_t b) {
  for (int i = 0; i < LEDS; i++) ball.setPixelColor(i, r, g, b);
  ball.show();
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pole.attach(SERVO_PIN);
  pole.write(TOP);
  ball.begin();
  ball.setBrightness(150);
  glow(40, 40, 60);   // a soft silver while it waits
  Serial.println("Ball at the top. Press the button to start the countdown");
}

void loop() {
  unsigned long now = millis();

  if (!running && celebrateUntil == 0 && digitalRead(BUTTON_PIN) == LOW) {
    running = true;
    startedAt = now;
    lastSecondShown = -1;
    glow(120, 120, 160);
    Serial.println("Countdown started");
  }

  if (running) {
    unsigned long t = now - startedAt;
    unsigned long total = (unsigned long)COUNT_FROM * 1000;
    if (t >= total) {
      running = false;
      pole.write(BOTTOM);
      Serial.println("Happy New Year!");
      for (int n = 0; n < 6; n++) {
        tone(PIEZO_PIN, CHEER[n], CHEER_MS[n]);
        delay(CHEER_MS[n] + 30);
      }
      celebrateUntil = millis() + 8000;
      return;
    }
    // Glide: the angle follows the clock, not the beeps.
    pole.write(TOP - (int)((long)(TOP - BOTTOM) * t / total));
    int second = COUNT_FROM - (int)(t / 1000);
    if (second != lastSecondShown) {
      lastSecondShown = second;
      Serial.println(second);
      tone(PIEZO_PIN, second <= 3 ? 1760 : 880, 80);
    }
  }

  // After midnight: the ball spins through the rainbow for a while.
  if (celebrateUntil > 0) {
    if (now < celebrateUntil) {
      ball.rainbow((uint16_t)(now * 40));
      ball.show();
    } else {
      celebrateUntil = 0;
      glow(40, 40, 60);
      pole.write(TOP);
      Serial.println("Ball back at the top");
    }
  }
}

Code walkthrough

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

glow sketch.ino

Paints all 12 LEDs one color and shows it. The sketch uses it for the soft silver while it waits and the brighter white when the countdown begins.

void glow(uint8_t r, uint8_t g, uint8_t b) {
  for (int i = 0; i < LEDS; i++) ball.setPixelColor(i, r, g, b);
  ball.show();
}

setup sketch.ino

Sets the button to INPUT_PULLUP, attaches the servo and parks it at the top, then starts the LED ring and makes it silver. Nothing moves until the button is pressed.

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pole.attach(SERVO_PIN);
  pole.write(TOP);
  ball.begin();
  ball.setBrightness(150);
  glow(40, 40, 60);   // a soft silver while it waits
  Serial.println("Ball at the top. Press the button to start the countdown");
}

loop sketch.ino

Starts the countdown on a button press, then works out how much time has passed. It sets the servo angle from that time, and beeps whenever the shown second changes. When time is up it drops the ball, plays the cheer and starts the rainbow.

void loop() {
  unsigned long now = millis();

  if (!running && celebrateUntil == 0 && digitalRead(BUTTON_PIN) == LOW) {
    running = true;
    startedAt = now;
    lastSecondShown = -1;
    glow(120, 120, 160);
    Serial.println("Countdown started");
  }

  if (running) {
    unsigned long t = now - startedAt;
    unsigned long total = (unsigned long)COUNT_FROM * 1000;
    if (t >= total) {
      running = false;
      pole.write(BOTTOM);
      Serial.println("Happy New Year!");
      for (int n = 0; n < 6; n++) {
        tone(PIEZO_PIN, CHEER[n], CHEER_MS[n]);
        delay(CHEER_MS[n] + 30);
      }
      celebrateUntil = millis() + 8000;
      return;
    }
    // Glide: the angle follows the clock, not the beeps.
    pole.write(TOP - (int)((long)(TOP - BOTTOM) * t / total));
    int second = COUNT_FROM - (int)(t / 1000);
  // ... 20 more lines in the editor

Troubleshooting

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

  • The button does nothing This project
    It is ignored during the 8-second rainbow after midnight. Wait for the ball to return to the top, then press again.
  • The LEDs stay dark This project
    Check that DIN goes through the 330 ohm resistor from pin 6, and that VDD is on 5V and VSS on ground.
  • The ball moves the wrong way This project
    Swap the values of TOP and BOTTOM, or check which way your servo horn is mounted.
  • 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.

Change it and keep it

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