Build it step by step
0 of 8 doneWire the display
CLK to D5, DIO to D6, power to 5V and GND.
Wire the clock chip
SDA to A4, SCL to A5, power to 5V and GND.
Wire the buttons
D2 and D3 each to GND.
Upload the sketch
The display shows roughly the compile time.
Press the hours button
The hour advances.
Press the minutes button
The minute advances.
Hold both buttons for a second
The clock switches between 12 and 24 hours.
Unplug and replug the power
The time is kept.
How do you build a four-digit clock with TM1637?
A four-digit clock with a blinking colon, set with two buttons and kept accurate by an RTC. You need Arduino Nano, TM1637 4-digit display, DS3231 RTC module, Tactile push button 6 mm × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| TM1637 CLK | Nano D5 | Display clock |
| TM1637 DIO | Nano D6 | Display data |
| TM1637 VCC and GND | Nano 5V and GND | Power |
| RTC SDA | Nano A4 | I2C data |
| RTC SCL | Nano A5 | I2C clock |
| RTC VCC and GND | Nano 5V and GND | Power |
| Hours button | Nano D2 to GND | Add one hour |
| Minutes button | Nano D3 to GND | Add one minute |
Code
A DS3231 real-time clock chip keeps accurate time, to within a minute or so a year, and carries on from a small backup cell when the board is switched off. The sketch reads the time over I2C and shows hours and minutes on the four-digit display, with the colon blinking every second by turning one segment bit on and off. The hours button adds an hour and the minutes button adds a minute, and both buttons held together switch between 24 hour and 12 hour mode. If the clock has never been set or lost power, the sketch sets it from the time it was compiled, which is usually a few seconds off, so use the buttons to correct it. Setting the minutes also resets the seconds to zero, which makes it easy to set the clock exactly against a reference. The display brightness is low by default; raise it with setBrightness for a bright room. A clean housing with a red filter makes a handsome bedside clock.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <TM1637Display.h>
#include <RTClib.h>
const int CLK = 5, DIO = 6;
const int HOUR_BTN = 2, MIN_BTN = 3;
TM1637Display display(CLK, DIO);
RTC_DS3231 rtc;
bool mode24 = true;
bool lastColon = false;
void showTime(const DateTime& t, bool colon) {
int h = t.hour();
if (!mode24) { h = h % 12; if (h == 0) h = 12; }
int value = h * 100 + t.minute();
display.showNumberDecEx(value, colon ? 0b01000000 : 0, mode24); // leading zero only in 24 hour mode
}
void setup() {
pinMode(HOUR_BTN, INPUT_PULLUP);
pinMode(MIN_BTN, INPUT_PULLUP);
display.setBrightness(4);
if (!rtc.begin()) { display.showNumberDec(8888); while (true) {} }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // start from the compile time
}
void loop() {
DateTime now = rtc.now();
bool hb = (digitalRead(HOUR_BTN) == LOW);
bool mb = (digitalRead(MIN_BTN) == LOW);
if (hb && mb) { // both held: switch 12 and 24 hour mode
delay(800);
if (digitalRead(HOUR_BTN) == LOW && digitalRead(MIN_BTN) == LOW) mode24 = !mode24;
while (digitalRead(HOUR_BTN) == LOW || digitalRead(MIN_BTN) == LOW) {}
} else if (hb) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), (now.hour() + 1) % 24, now.minute(), 0));
delay(250);
} else if (mb) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour(), (now.minute() + 1) % 60, 0));
delay(250);
}
bool colon = (now.second() % 2 == 0); // blink once a second
showTime(rtc.now(), colon);
delay(100);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | Time shown with a blinking colon |
| Hours pressed | Hour up by one |
| Minutes pressed | Minute up by one |
| Both held | 12 or 24 hour mode |
| Power off for a minute | Time still correct |
If it does not work
Display shows 8888
The clock chip is not found, check the I2C wires.
Time resets each power cycle
Fit the backup cell in the clock module.
Colon does not blink
Check showNumberDecEx colon mask for your display.
Time a few seconds off
Set the minutes to reset the seconds.
Display too dim
Raise the value in setBrightness.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the four-digit clock with TM1637?
Arduino Nano, TM1637 4-digit display, DS3231 RTC module, Tactile push button 6 mm × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the four-digit clock with TM1637 take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the four-digit clock with TM1637 works?
Power on: Time shown with a blinking colon. Hours pressed: Hour up by one. Minutes pressed: Minute up by one.
Display shows 8888: what should I check?
The clock chip is not found, check the I2C wires.
Time resets each power cycle: what should I check?
Fit the backup cell in the clock module.
Colon does not blink: what should I check?
Check showNumberDecEx colon mask for your display.
Time a few seconds off: what should I check?
Set the minutes to reset the seconds.
