By Abdul Rehman
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.
Here is the list of components that we are going to use for building this setup
Here is the Circuit diagram which we are going to use for building the Digital Clock using the RTC
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)
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)
20 November 2022
13 November 2022
07 September 2022
27 July 2022
28 February 2022
FYP Solutions Powered By Impressive Business WordPress Theme