Digital Clock
In this workhop we will learn how to how to make a digital clock using arduino and RTC module.
Objective
Build a digitalClock using arduino and RTC module, that will keep the time even arduino is turne off .
Prototype Building
Here we are using an Arduino as controller and DS323 Real Time Clock module to Count and keep the time and 16x2 LCD Display to Display the Time and Date.
Things we need
- Arduino Uno
- Real Time Clock Module
- 16x2 LCD Module
- 10k ohm potentiometer
- 220 ohm resistor
- Jumper Wires
- Breadboard
DS3231 Real Time Clock
The DS3231 is a low-cost, highly accurate Real Time Clock which can maintain hours, minutes and seconds, as well as, day, month and year information. Also, it has automatic compensation for leap-years and for months with fewer than 31 days.
The module can work on either 3.3 or 5 V which makes it suitable for many development platforms or microcontrollers. The battery input is 3V and a typical CR2032 3V battery can power the module and maintain the information for more than a year.
The module uses the I2C Communication Protocol which makes the connection to the Arduino Board very easy.
LCD Module
An LCD is an electronic display module which uses liquid crystal to produce a visible image. The 16×2 LCD display is a very basic module commonly used in DIYs and circuits. The 16×2 translates o a display 16 characters per line in 2 such lines. In this LCD each character is displayed in a 5×7 pixel matrix.
Pin Description
Step 1: Arduino Setup
1.1: Install Arduino IDE
Download the Arduino IDE and install it on your computer.
1.2 walk-through the Arduino Introduction page to learn basics
If you are new to the arduino system, you can learn the Arduino basics from here , after reading then go to the next step.
Step 2: Programming
2.1 Algorithm
2.2 Open Arduino IDE and Start a new Sketch
2.3 Read then Copy and Paste the Code
Testing the RTC Module.
To test the RTC you need to downlaod two Arduino Library.
after downlaoding we need to add the libraries into arduino ide. for that open Libraray manager by clicking Sketch -> Include Library -> Add .ZIP Library
Click the Add .ZIP Library
SetTime
First we need to set the RTC Module Time, for that insert Coin (CR2032) Cell battery on RTC Module and Connct with Arduino Uno.
Upload Sktech
#include <Wire.h> #include <TimeLib.h> #include <DS1307RTC.h> const char *monthName[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; tmElements_t tm; void setup() { bool parse=false; bool config=false; // get the date and time the compiler was run if (getDate(__DATE__) && getTime(__TIME__)) { parse = true; // and configure the RTC with this info if (RTC.write(tm)) { config = true; } } Serial.begin(9600); while (!Serial) ; // wait for Arduino Serial Monitor delay(200); if (parse && config) { Serial.print("DS1307 configured Time="); Serial.print(__TIME__); Serial.print(", Date="); Serial.println(__DATE__); } else if (parse) { Serial.println("DS1307 Communication Error :-{"); Serial.println("Please check your circuitry"); } else { Serial.print("Could not parse info from the compiler, Time=\""); Serial.print(__TIME__); Serial.print("\", Date=\""); Serial.print(__DATE__); Serial.println("\""); } } void loop() { } bool getTime(const char *str) { int Hour, Min, Sec; if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; tm.Hour = Hour; tm.Minute = Min; tm.Second = Sec; return true; } bool getDate(const char *str) { char Month[12]; int Day, Year; uint8_t monthIndex; if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false; for (monthIndex = 0; monthIndex < 12; monthIndex++) { if (strcmp(Month, monthName[monthIndex]) == 0) break; } if (monthIndex >= 12) return false; tm.Day = Day; tm.Month = monthIndex + 1; tm.Year = CalendarYrToTm(Year); return true; }
Upload the sktech and open the Serial Monitor and it will shows like this
DS1307 configured Time=11:24:26, Date=Mar 10 2020
That means our RTC module is set with the time.
Tesing LCD Module
Here we are using LCD Module with 12C addons for I2C Communication , which will only need two wires for communication, in other way we need to use 14 pins.
Since the I2C is a addon we need to insert the library on Arduino IDE first. For that first downlaod the Arduino-LiquidCrystal-I2C-library.zip, Then click Sketch -> Include Library -> Add .ZIP Library
then select the .Zip file and click open.
now we successfully added the I2C library.
Uplaod and Compaile Code
#include <Wire.h> #include <LiquidCrystal_I2C.h> // Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { // initialize the LCD lcd.begin(); // Turn on the blacklight and print a message. lcd.backlight(); lcd.print("Hello, world!"); } void loop() { lcd.setCursor(0, 1); lcd.print(millis() / 1000); }
Connect LCD Module with Arduino
Demonstrates the use a 16x2 LCD display. The I2C LiquidCrystal Module comes with 4-pins and connect them with follwing model.
- LCD SCL - Arduino SCL
- LCD SDA - Arduino SDA
- LCD GND - Arduino GND
- LCD VCC - Arduino VCC
This sketch prints “Hello World!” to the LCD and shows the time.
Step 3: Combine both RTC and LCD Module Sktech
We tested both and now we need to combine the both hardware and progrmmes into one.
Pin Diagram.
Connect the RTC and I2C LCD module with the arduino using same VCC and GND also , we can use same SDA and SCL since I2C based on I2C
Sketch.
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <TimeLib.h> #include <DS1307RTC.h> // Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { // initialize the LCD lcd.begin(); Serial.begin(9600); // Turn on the blacklight and print a message. lcd.backlight(); } void loop() { tmElements_t tm; if (RTC.read(tm)) { lcd.setCursor(0, 0); lcd.print("Time: "); lcd.print(tm.Hour); lcd.write(':'); lcd.print(tm.Minute); lcd.write(':'); lcd.print(tm.Second); Serial.print("Time: "); Serial.print(tm.Hour); Serial.write(':'); Serial.print(tm.Minute); Serial.write(':'); Serial.print(tm.Second); lcd.setCursor(0, 1); lcd.print("Date: "); lcd.print(tm.Day); lcd.write('/'); lcd.print(tm.Month); lcd.write('/'); lcd.print(tmYearToCalendar(tm.Year)); Serial.print("Ok, Time = "); print2digits(tm.Hour); Serial.write(':'); print2digits(tm.Minute); Serial.write(':'); print2digits(tm.Second); Serial.print(", Date (D/M/Y) = "); Serial.print(tm.Day); Serial.write('/'); Serial.print(tm.Month); Serial.write('/'); Serial.print(tmYearToCalendar(tm.Year)); Serial.println(); delay(1000); lcd.clear(); } else { if (RTC.chipPresent()) { Serial.println("The DS1307 is stopped. Please run the SetTime"); Serial.println("example to initialize the time and begin running."); Serial.println(); lcd.print("run SetTime"); lcd.clear(); } else { Serial.println("DS1307 read error! Please check the circuitry."); Serial.println(); lcd.print("RTC Error"); lcd.clear(); } delay(9000); } } void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0'); } Serial.print(number); }
Upload the sketch to the arduino and connect power.
Final Demo
ToDO
-
Add button to display time when only the button pressed.
-
Add buzzer and set alarms.
-
Designa and 3D Print Enclosure.
Thank You, Hope you enjoyed!
Please share your feedback.