Interfacing a RTC(Real Time Clock) module using Arduino
Hardware Required
- ARDUINO UNO
- Connecting Wires
- DS3231 RTC module
About ARDUINO
Arduino is a popular open-source development board for engineers and makers to develop electronics projects in an easy way. It consists of both a physical programmable development board (based on AVR series of microcontrollers) and a piece of software or IDE which runs on your computer and used to write and upload the code to the microcontroller board.
Where DS3231 RTC MODULE are Used?
For understanding the use of DS3231 RTC MODULE consider:
Case1: Where you want accurate time and date. Although there are many RTC modules present in the market DS3231 is one of the most popular one because of its accuracy. The chip keeps the time up to date more accurately than most modules.
Case2: Where power consumption is issue. DS3231 RTC MODULE consumes very less power to function. So this module can be used on mobile systems.
Case3: Where high speed communication is of need. DS3231 RTC MODULE capable of communicating with high speed TWI interface.
The DS3231 can also work on both 24Hr and 12Hr format finding applications in GPS systems. With two alarm clock and temperature sensor on board the use of DS3231 module is promoted even further than other modules.
Connections
- SDA to the analog pin A4 of the Arduino board
- SCL to the analog pin A5 of the Arduino board
- VCC to the +5v pin of the Arduino board
- GND to the GND pin of the Arduino board
Library used
- Wire.h
- RTClib.h
Link to buy the devices
- For the DS3231 module click here
- For Arduino UNO click here
Code
#include <Wire.h>
#include "RTClib.h"
#include "RTClib.h"
RTC_DS3231 rtc;
// rtc.adjust(DateTime(2014,1,21,3,0,0));
// rtc.adjust(DateTime(2014,1,21,3,0,0));
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};// to define the days
void setup () {
rtc.begin();// initialise RTC communication
Serial.begin(9600); // initialise Serial communication
}
Serial.begin(9600); // initialise Serial communication
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);// to print the current year
Serial.print('/');
Serial.print(now.month(), DEC);// to print the current month
Serial.print('/');
Serial.print(now.day(), DEC);// to print the current day
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);// to print the name of the day
Serial.print(") ");
Serial.print(now.hour(), DEC);// to print the current hour
Serial.print(':');
Serial.print(now.minute(), DEC);// to print the current minute
Serial.print(':');
Serial.print(now.second(), DEC);// to print the current second
Serial.println();
}
Explanation of the code
The commented lines explain everything about the code but I will still explain you; the first three lines are required to define the libraries, and you shall uncomment the fourth line if you want to change the date and time. And the fifth line will define the names of the seven days of the week. In setup just the initialisation of RTC and Serial communication is done. In the loop just printing the date, time and name of the day on Serial monitor is done.
Do visit my other blog if you have any doubts regarding setting the new time and original time. The link is given below.
Very helpful
ReplyDeleteThank u very much
ReplyDelete