New Year's countdown on an Arduino Uno
A DS3231 and a four-digit display count down to midnight on New Year’s Eve, then a ring of WS2812B LEDs bursts into fireworks and the display shows the new year. 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $38. On Mokxi they're free.
What you'll learn
- Reading date and time from a DS3231 with
RTClib - Computing a target time instead of typing it in
- Splitting seconds into hours, minutes and seconds
- Animating addressable LEDs with fading bursts
- Doing two jobs in one loop without waiting
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
A DS3231 clock on A4 and A5 tells the Uno the date and time. The sketch works out the next January 1st at midnight from the clock's own year, so it counts down every December 31st without edits. The time left shows on a four-digit TM1637 on pins 4 and 5.
The display shows hours and minutes until the last hour, then minutes and seconds. At midnight it shows the new year, and a ring of 16 LEDs on pin 6 plays fireworks for 60 seconds.
In the simulator the clock starts 15 seconds before midnight, so you can watch the moment arrive. The Serial Monitor counts the last 10 seconds.
Parts list
6 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Real-time clock, DS3231In Christmas Lights Kit, Tide Clock Kit.
- 1 × Four-digit display, TM1637In Pinball Table Kit, Spaceship Panel Kit, Tide Clock Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
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 pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Four-digit display, TM1637 pin DIO | Arduino Uno R3 pin 5 |
| Four-digit display, TM1637 pin CLK | Arduino Uno R3 pin 4 |
| Real-time clock, DS3231 pin VCC | Arduino Uno R3 pin 5V |
| Four-digit display, TM1637 pin VCC | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Real-time clock, DS3231 pin GND | Arduino Uno R3 pin GND |
| Four-digit display, TM1637 pin GND | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Real-time clock, DS3231 pin SDA | Arduino Uno R3 pin A4 |
| Real-time clock, DS3231 pin SCL | Arduino Uno R3 pin A5 |
| Addressable RGB LEDs pin DIN | Resistor, 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.
// New Year's countdown: a clock for the party that counts down to midnight on
// a four-digit display, then sets off fireworks on a ring of LEDs.
//
// DS3231 SCL -> A5 SDA -> A4 VCC -> 5 V GND -> GND
// TM1637 CLK -> pin 4 DIO -> pin 5 VCC -> 5 V GND -> GND
// LED ring 16 WS2812B LEDs, DIN <- 330 ohms <- pin 6, VDD -> 5 V, VSS -> GND
//
// The display shows the time left: hours and minutes until the last hour,
// then minutes and seconds, so the final minute reads 00:59, 00:58 ... 00:00.
// At midnight the ring bursts into fireworks for a minute, and the display
// shows the new year.
//
// The target is worked out from the clock, never typed in: midnight at the
// start of January 1st of the year after the one the clock is in. So the same
// sketch counts down every December 31st, year after year.
//
// Set the clock once: uncomment the rtc.adjust() line, upload, then comment it
// out and upload again. In the simulator the clock starts 15 seconds before
// midnight, so you can watch the moment arrive.
//
// Things to try. Make the fireworks last longer with SHOW_SECONDS. Change the
// colors in PALETTE to your own.
#include <Wire.h>
#include <RTClib.h>
#include <TM1637Display.h>
#include <Adafruit_NeoPixel.h>
const int RING_PIN = 6;
const int LEDS = 16;
const unsigned long SHOW_SECONDS = 60;
RTC_DS3231 rtc;
TM1637Display display(4, 5); // CLK, DIO
Adafruit_NeoPixel ring(LEDS, RING_PIN, NEO_GRB + NEO_KHZ800);
const uint32_t PALETTE[] = {0xFF2020, 0xFFD040, 0x40A0FF, 0x40FF60, 0xFF40C0, 0xFFFFFF};
const int COLORS = sizeof(PALETTE) / sizeof(PALETTE[0]);
uint32_t lastSecond = 0;
uint32_t showUntil = 0;
unsigned long nextFrame = 0;
uint8_t level[LEDS];
uint32_t color[LEDS];
// Midnight at the start of the next year.
uint32_t nextNewYear(const DateTime &now) {
return DateTime(now.year() + 1, 1, 1, 0, 0, 0).unixtime();
}
void showLeft(uint32_t seconds) {
uint32_t h = seconds / 3600;
uint32_t m = (seconds / 60) % 60;
uint32_t s = seconds % 60;
int value = h > 0 ? (int)((h > 99 ? 99 : h) * 100 + m) : (int)(m * 100 + s);
display.showNumberDecEx(value, 0b01000000, true); // colon on
}
// One frame of fireworks: now and then a burst of one color lights a few
// LEDs around a random spot, and every LED fades a little each frame.
void fireworks() {
if (random(0, 6) == 0) {
int center = random(0, LEDS);
uint32_t c = PALETTE[random(0, COLORS)];
for (int d = -2; d <= 2; d++) {
int i = (center + d + LEDS) % LEDS;
level[i] = 255 - abs(d) * 60;
color[i] = c;
}
}
for (int i = 0; i < LEDS; i++) {
uint8_t r = (uint8_t)(color[i] >> 16), g = (uint8_t)(color[i] >> 8), b = (uint8_t)color[i];
ring.setPixelColor(i, r * level[i] / 255, g * level[i] / 255, b * level[i] / 255);
level[i] = level[i] > 12 ? level[i] - 12 : 0;
}
ring.show();
}
void setup() {
Serial.begin(9600);
display.setBrightness(5);
ring.begin();
ring.setBrightness(180);
ring.show();
if (!rtc.begin()) {
Serial.println("No DS3231 answered: check SDA and SCL");
while (true) {
}
}
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set once, see above
randomSeed(analogRead(A0));
Serial.println("New Year's countdown");
}
void loop() {
if (showUntil > 0 && millis() >= nextFrame) {
nextFrame = millis() + 30;
fireworks();
}
DateTime now = rtc.now();
uint32_t t = now.unixtime();
if (t == lastSecond) return;
lastSecond = t;
// The first minute of January 1st: the show, and the new year on the display.
if (now.month() == 1 && now.day() == 1 && now.hour() == 0 && now.minute() == 0) {
if (showUntil == 0) {
showUntil = t + SHOW_SECONDS;
Serial.print("Happy New Year! Welcome to ");
Serial.println(now.year());
}
display.showNumberDec(now.year(), false);
return;
}
if (showUntil > 0 && t >= showUntil) {
showUntil = 0;
ring.clear();
ring.show();
Serial.println("Show over, counting to next year");
}
uint32_t left = nextNewYear(now) - t;
showLeft(left);
if (left <= 10) {
Serial.println(left);
} else if (now.second() == 0) {
Serial.print(left / 60);
Serial.println(" minutes to go");
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
nextNewYear sketch.ino
Takes the current date and returns the time of midnight on January 1st of the following year as a Unix timestamp. Subtracting the current time from it gives the seconds left.
// Midnight at the start of the next year.
uint32_t nextNewYear(const DateTime &now) {
return DateTime(now.year() + 1, 1, 1, 0, 0, 0).unixtime();
}showLeft sketch.ino
Splits the seconds left into hours, minutes and seconds. If an hour or more remains it shows hours and minutes. In the last hour it shows minutes and seconds. The colon is switched on.
void showLeft(uint32_t seconds) {
uint32_t h = seconds / 3600;
uint32_t m = (seconds / 60) % 60;
uint32_t s = seconds % 60;
int value = h > 0 ? (int)((h > 99 ? 99 : h) * 100 + m) : (int)(m * 100 + s);
display.showNumberDecEx(value, 0b01000000, true); // colon on
}fireworks sketch.ino
Draws one frame. About one frame in six starts a burst of a random color from PALETTE around a random LED. Then every LED is dimmed a little, so each burst fades out.
// One frame of fireworks: now and then a burst of one color lights a few
// LEDs around a random spot, and every LED fades a little each frame.
void fireworks() {
if (random(0, 6) == 0) {
int center = random(0, LEDS);
uint32_t c = PALETTE[random(0, COLORS)];
for (int d = -2; d <= 2; d++) {
int i = (center + d + LEDS) % LEDS;
level[i] = 255 - abs(d) * 60;
color[i] = c;
}
}
for (int i = 0; i < LEDS; i++) {
uint8_t r = (uint8_t)(color[i] >> 16), g = (uint8_t)(color[i] >> 8), b = (uint8_t)color[i];
ring.setPixelColor(i, r * level[i] / 255, g * level[i] / 255, b * level[i] / 255);
level[i] = level[i] > 12 ? level[i] - 12 : 0;
}
ring.show();
}loop sketch.ino
Runs a fireworks frame every 30 ms while a show is on. Once per clock second it checks for the first minute of January 1st and starts the show. At other times it updates the countdown and prints to the Serial Monitor.
void loop() {
if (showUntil > 0 && millis() >= nextFrame) {
nextFrame = millis() + 30;
fireworks();
}
DateTime now = rtc.now();
uint32_t t = now.unixtime();
if (t == lastSecond) return;
lastSecond = t;
// The first minute of January 1st: the show, and the new year on the display.
if (now.month() == 1 && now.day() == 1 && now.hour() == 0 && now.minute() == 0) {
if (showUntil == 0) {
showUntil = t + SHOW_SECONDS;
Serial.print("Happy New Year! Welcome to ");
Serial.println(now.year());
}
display.showNumberDec(now.year(), false);
return;
}
if (showUntil > 0 && t >= showUntil) {
showUntil = 0;
ring.clear();
ring.show();
Serial.println("Show over, counting to next year");
}
// ... 9 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The sketch stops with no DS3231 answered This project
Check that SDA goes to A4 and SCL goes to A5, with 5V and GND. - The LED ring stays dark This project
Pin 6 goes through the 330 ohm resistor to DIN, and the ring needs 5V and GND. - The countdown is wrong on a real board This project
Set the clock once with thertc.adjustline, then comment it out and upload again. - The time shows odd numbers, such as 0x23 printed as 35, when you read it. Real-time clock
All time registers are BCD, not plain binary. Convert each byte with (b >> 4) * 10 + (b & 0x0F) to read, and the reverse to write. A library such as RTClib does this for you. - The clock does not respond, or the I2C bus hangs. Real-time clock
The DS3231 address is 0x68. Wire SDA and SCL to the right pins with pull-ups, about 4.7k, to the supply. Many breakout boards already have them. Join the grounds. - The display does nothing. Four-digit display
Connect CLK and DIO to two digital pins, plus VCC and GND. It is not I2C, so no address and no Wire library. Use a TM1637 library and give it the same two pins. - Digits look wrong or in a strange order. Four-digit display
Check the library's segment mapping and digit order. The colon is bit 7 of the second digit. Brightness is set separately and can be 0 to 7. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.