Interfacing a RTC(Real Time Clock) module to set Time
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"
RTC_DS3231 rtc;
rtc.adjust(DateTime(2014,1,21,3,0,0));// Use this line to set the time as you wish
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); uncomment this line if you want to set the current(original) time in RTC
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
}
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();
}
No comments:
Post a Comment