Categories
Examples

Arduino RTC based Digital Clock with 7 segment module

Digital Clock with Arduino is an important and simple project to start with RTC. RTC Stands for Real-time Clock. Whenever we require the real-time clock management system we need to use some RTC interface.

Componenet List

Here is the list of components that we are going to use for building this setup

  • Arduino Nano
  • TM1637 4 digit 7 segment Module
  • Push button x 2
  • 10K Resistors 1/4 watt x 2
  • DS3132 RTC Module

Circuit Diagram

Here is the Circuit diagram which we are going to use for building the Digital Clock using the RTC

Arduino Nano based digital clock circuit diagram
Digital Clock using RTC and Arduino Nano

Libraries Requried

For building the RTC based Digital Clock we need the following two libraries because we are using two modules so we need two libraries. The libraries which we are going to use are

#include <DS3231.h>
#include <TM1637Display.h>Code language: CSS (css)

Digital Clock Arduino Code

Here is the complete code to read the DS3231 RTC module and then display the time on TM1637 7 segment module. In this code, we had used the two push buttons to adjust the time. We also used the millis() function to blink the colon after 500 milliseconds.


#include <DS3231.h>
#include <TM1637Display.h>

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);
// Init a Time-data structure
Time  t;

// Module connection pins (Digital Pins)
#define CLK A3
#define DIO 3
TM1637Display seg7display(CLK, DIO);
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };



#define SW1 A1
#define SW2 A2
#define SW3 A0

//-------------------------------------------
long preMillis = 0;
uint8_t blink_status = 0x40;
int mHour=0,mMinute=0;
int curTimeInt = 0;
//--------------------------------------------
void setup() {
  pinMode(SW1,INPUT);
  pinMode(SW2,INPUT);
  pinMode(SW3,INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  rtc.begin();
  seg7display.setBrightness(0x0f);
  t = rtc.getTime();
  mHour = t.hour;
  mMinute = t.min;
  curTimeInt = (mHour*100)+mMinute;
  seg7display.showNumberDecEx(curTimeInt,0x00, true, 4, 0); 

  // The following lines can be uncommented to set the date and time
  //rtc.setDOW(SATURDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(15, 40, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(25, 12, 2021);   // Set the date to January 1st, 2014
}
  
void loop() {
  
  
  if((millis()-preMillis)>499){
    preMillis = millis();
    
    if(blink_status ==0){
      blink_status = 0x40;
      t = rtc.getTime();
      mHour = t.hour;
      mMinute = t.min;
      curTimeInt = (mHour*100)+mMinute;
  
    }else{
      blink_status = 0;
    }
    seg7display.showNumberDecEx(curTimeInt,blink_status, true, 4, 0); 
  }
  
  if(digitalRead(SW1)==LOW){
    mHour++;
    if(mHour>23)mHour=0;
    curTimeInt = (mHour*100)+mMinute;
    seg7display.showNumberDecEx(curTimeInt,0x40, true, 4, 0); 
    preMillis = 0;
    rtc.setTime(mHour, mMinute, 0);
    do{delay(100);}while(digitalRead(SW1)==LOW);
  }else if(digitalRead(SW2)==LOW){
    mMinute++;
    if(mMinute>59)mMinute=0;
    curTimeInt = (mHour*100)+mMinute;
    seg7display.showNumberDecEx(curTimeInt,0x40, true, 4, 0); 
    preMillis = 0;
    rtc.setTime(mHour, mMinute, 0);
    do{delay(100);}while(digitalRead(SW2)==LOW);
  }

  
  /*
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  //Serial.println(rtc.getTimeStr());
  Serial.print("Today is the ");
  Serial.print(t.date, DEC);
  Serial.print(". day of ");
  Serial.print(rtc.getMonthStr());
  Serial.print(" in the year ");
  Serial.print(t.year, DEC);
  Serial.println(".");




  //rtc.setTime(15, 40, 0);     // Set the time to 12:00:00 (24hr format)
  seg7display.showNumberDecEx(curTimeInt,0x40, true, 4, 0); 
  // Wait one second before repeating :)
  
  seg7display.showNumberDecEx(curTimeInt,0x00, false, 4, 0); 
  
*/  
}
Code language: PHP (php)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

6 replies on “Arduino RTC based Digital Clock with 7 segment module”

Problem with code I don’t understand when trying to compile, as follows.” Compilation error: no matching function for call to ‘DS3231::DS3231(const uint8_t&, const uint8_t&)’ “

Hi Abdul, I have tried about 6 or 7 of the TM1637 and DS3231 libraries and cannot get your sketch to work.
Please advise which are the respective libraries that you use in your sketch.
I have been trying for two days to try to get your sketch to work.
I am 85 years old and need some help.
I look forward to your response, please.

The two libraries listed in you web presentation, this is not sufficient info, I possibly need a specific TM1637 and DS3231 libraries. HELP

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.