Arduino - 7-segment Clock

In this tutorial, we are going to learn how to make 7-segment clock using Arduino. In detail, we will learn two cases:

You can also use the DS1307 RTC module instead of the DS3231 RTC module by changing a single line of code. See DS3231 vs DS1307

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×TM1637 4-digit 7-segment Display
1×Real-Time Clock DS3231 Module
1×(Alternatively) Real-Time Clock DS1307 Module
1×CR2032 battery
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About TM1637 display, DS3231 and DS1307 RTC modules

If you do not know about TM1637 7-segment display, DS3231 and DS1307 (pinout, how it works, how to program ...), learn about them in the following tutorials:

Install TM1637 and RTC Libraries

  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “TM1637”, then find the TM1637Display library by Avishay Orpaz
  • Click Install button.
Arduino TM1637 4-digit 7-segment display library
  • Search “RTClib”, then find the RTC library by Adafruit. This library works with both DS3231 and DS1307
  • Click Install button to install RTC library.
Arduino RTC library
  • You may be asked for intalling some other library dependencies
  • Click Install All button to install all library dependencies.
Arduino rtc dependency library

Wiring Diagram

  • The wiring diagram between Arduino, TM1637 4-digit 7-segment display and DS3231 RTC module.
Arduino DS3231 7-segment display Wiring Diagram

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino, TM1637 4-digit 7-segment display and DS1307 RTC module.
Arduino DS1307 7-segment display Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code - Displaying minute and seconds on the 7-segment display

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-7-segment-clock */ #include <TM1637Display.h> #include <RTClib.h> // define the connections pins #define CLK 9 #define DIO 10 // create a display object of type TM1637Display TM1637Display display = TM1637Display(CLK, DIO); RTC_DS1307 rtc; // RTC_DS3231 rtc; // uncomment this line and comment the above line if using DS3231 module unsigned long time_m = 0; // the variable to store minute unsigned long time_s = 0; // the variable to store second unsigned long last_s = 0; // the variable to store the last updated second void setup() { Serial.begin(9600); display.clear(); display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest) // SETUP RTC MODULE if (!rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (true) ; } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } void loop() { DateTime now = rtc.now(); time_m = now.minute(); time_s = now.second(); if (time_s != last_s) { // only update if changed unsigned long time = time_m * 100 + time_s; display.showNumberDecEx(time, 0b11100000, false, 4, 0); Serial.print(time_m); Serial.print(":"); Serial.println(time_s); last_s = time_s; } }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Check out the result on the Serial Monitor and TM1637 display.

Code Explanation

Read the line-by-line explanation in comment lines of source code!

If you want to use the DS1307 RTC module instead of DS3231 RTC module, you just need to commment/uncomment a line specified in the code

Arduino Code - Displaying hour and minute on the 7-segment display

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-7-segment-clock */ #include <TM1637Display.h> #include <RTClib.h> // define the connections pins #define CLK 9 #define DIO 10 // create a display object of type TM1637Display TM1637Display display = TM1637Display(CLK, DIO); RTC_DS1307 rtc; // RTC_DS3231 rtc; // uncomment this line and comment the above line if using DS3231 module unsigned long time_h = 0; // the variable to store hour unsigned long time_m = 0; // the variable to store minute unsigned long last_m = 0; // the variable to store the last updated hour void setup() { Serial.begin(9600); display.clear(); display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest) // SETUP RTC MODULE if (!rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (true) ; } // automatically sets the RTC to the date & time on PC this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } void loop() { DateTime now = rtc.now(); time_h = now.hour(); time_m = now.minute(); if (time_m != last_m) { // only update if changed unsigned long time = time_h * 100 + time_m; display.showNumberDecEx(time, 0b11100000, false, 4, 0); Serial.print(time_h); Serial.print(":"); Serial.println(time_m); last_m = time_m; } }

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES