This document describes the design and implementation of a real time clock using an Arduino Mega 2560 microcontroller and DS3231 RTC IC. The DS3231 IC measures time and temperature even without main power using a backup battery. It communicates with the Arduino via I2C protocol to display date, time, and temperature on a LCD screen in real time. The project aims to build a low power RTC that can keep accurate time even during power failures.
Analog to Digital Conversion Using Microcontroller Education Boards
Final Report
1. DESIGN AND IMPLEMENTATION OF
REAL TIME CLOCK
A
Project Report submitted towards partial fulfilment of the requirements for the Degree
of
Bachelor in Technology
In
ELECTRONICS AND TELECOMMUNICATION ENGINEERING
By
RUCHI SMITA JENA (12010237)
TILAK MAHARANA (12010190)
Under the guidance of
Asst. Prof. Aditya Kumar Hota
VEER SURENDRA SAI UNIVERSITY OF TECHNOLOGY, BURLA, ODISHA
(Formerly University College of Engineering)
SAMBALPUR-768018
APRIL 2016
2. DEPARTMENT OF
ELECTRONICS AND TELECOMMUNICATION ENGINEERING,
VEER SURENDRA SAI UNIVERSITY OF TECHNOLOGY, BURLA
CERTIFICATE
This is to certify that the project entitled ‘DESIGN AND IMPLEMENTATION OF
REAL TIME CLOCK’ was carried out by Ruchi Smita Jena (12010237) and Tilak
Maharana (12010190) in the Department of Electronics & Telecommunication
Engineering, Veer Surendra Sai University of Technology, Burla by the virtue of their
diligence and adherence to my guidance and advice.
Dr. Debasis Mishra, Asst. Prof. Aditya Kumar Hota,
Head of the Department, Supervisor,
El. & Telecomm. Engg. El. & Telecomm. Engg.
VSSUT, Burla VSSUT, Burla
3. ACKNOWLEDGEMENT
We are pleased to acknowledge out indebtedness to Prof. Aditya Kumar Hota of Electronics &
Telecommunication Engineering for his kind operation and inspiration to help us in submitting our
project “DESIGN AND IMPLEMENTATION OF REAL TIME CLOCK”.
Further we are thankful to The H.O.D and entire staff of the Department of Electronics and
Telecommunication Engineering for their kind cooperation and providing adequate facilities for
successful completion of this project and also to our friends for their moral support.
RUCHI SMITA JENA (12010237)
TILAK MAHARANA (12010190)
Date:
4. CONTENTS
Page No.
1. Introduction……………………………………………………… 01
2. Purpose………………………………………………………….. 01
3. Microcontroller Arduino mega2560…………………………….. 01
4. ATmega 2560……….……………………………………............ 02
5. Technical specification…………………………………………… 03
6. Pin configuration ……………………………………………….... 05
7. Mega 2560-CORE data sheet……………………………………. 06
8. What is RTC?.............…………………………………………… 06
6. DS3231……….………………………………………………….. 07
6.1 Features……………………………………………………... 08
6.2 Pin configuration……………………………………............ 08
6.3 Block diagram……………………………………………… 10
6.4 Working……………………………………………………. 10
7. Power control. …………………………………………………… 11
8. Address map……………………………………………………… 12
9. I2C data bus………………………………………………….......... 14
9.1 Data transfer from a master transmitter to a slave receiver…... 15
9.2 Data transfer from a slave transmitter to a master receiver….. 16
9.3 Slave receiver mode………………………………………….. 17
9.4 Slave transmitter mode……………………………………...... 18
10. LCD and its features…………………………………………………. 19
11. Connection diagram………………………………………………….. 20
12. Contrast adjustment………………………………………………….. 21
13. Power supply………………………………………………………… 22
14. List of components………………………………………………........ 22
15. Program code………………………………………………………… 22
16. Circuit Diagram……………………………………………………… 30
17. Result and Discussion………………………………………………... 31
18. Summary……………………………………………………………... 32
19. References…………………………………………………………….. 33
5. LIST OF FIGURES:
Figure 1: Arduino Mega 2560.........................................................................................................04
Figure 2: Arduino Mega 2560 Pin Diagram....................................................................................05
Figure 3: DS3231 Pin Diagram.......................................................................................................09
Figure 4: DS3231 Block Diagram...................................................................................................09
Figure 5: Data transfer is accomplished on the I2C bus. ......................................................................16
Figure 6: Data transfer from slave transmitter to master receiver.............. Error! Bookmark not defined.7
Figure 7: Slave receiver mode ............................................................... Error! Bookmark not defined.8
Figure 8: Slave transmitter mode ........................................................... Error! Bookmark not defined.8
Figure 9: 16x2 character LCD........................................................... Error! Bookmark not defined.19
Figure 10: 16x2 character LCD interfaced with 12c module..........................................................20
Figure 11: 16x2 character LCD interfaced with 12c module and Arduino Mega 2560..................21
Figure 12:16x2 character LCD interfaced with I2c Module with contrast adjuster .....Error! Bookmark not
defined.1
Figure 13:Circuit Diagram of whole setup.........................................................................................30
6. ABSTRACT
Real-time clock (RTC) ICs (DS 3231) measure time and temperature, even when the power of the
main device is off. During these times, RTC ICs draw power from an auxiliary battery or
supercapacitor. As expected, power consumption is a key factor in most RTC designs, but accuracy
and small package size are also important. We need to generalize our knowledge with the best
available things and redesign the entire thing into a small compact cluster which can fulfil the basic
needs of livelihood with the use of Arduino MEGA 2560 which interfaced with a small electronic
integrated unit with temperature sensor and real time clock will be fixed on any system that need real
time analysis i.e., DS3231. The data and commands for the device will be preinstalled during its
manufacture. This will automatically update as the time passes by and will show the temperature at
that time. Generally implemented in house hold appliance, industrial application and many more.
This whole setup works with the I2C protocol.
Keywords: Real Time Clock, DS3231, I2C, Arduino MEGA 2560.
7. 1 | P a g e
INTRODUCTION:
This project is to display ‘REAL TIME’ using microcontroller Arduino MEGA 2560 as well
as RTC IC DS3231 such that it gives the time in terms of hours, minutes and seconds and
displays the date as “DD/MM/YY” and temperature in °C.
The term is used to avoid confusion with ordinary hardware clocks which are only signals
that govern digital electronics, and do not count time in human units. RTC should not be
confused with real-time computing, which shares its three-letter acronym but does not
directly relate to time of day.
PURPOSE:
Although keeping time can be done without an RTC, using one has benefits:
Low power consumption (important when running from alternate power)
Frees the main system for time-critical tasks
Sometimes more accurate than other methods
MICROCONTROLLER ARDUINO MEGA 2560:
The Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital
input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs
(hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP
header, and a reset button. It contains everything needed to support the microcontroller;
simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or
battery to get started. The Mega 2560 board is compatible with most shields designed for the
Uno and the former boards Duemilanove or Diecimila.
8. 2 | P a g e
ATmega 2560:
The ATmega640/1280/1281/2560/2561 is a low-power CMOS 8-bit microcontroller based on
the AVR enhanced RISC architecture. By executing powerful instructions in a single clock
cycle, the ATmega640/1280/1281/2560/2561 achieves throughputs approaching 1 MIPS per
MHz allowing the system designer to optimize power consumption versus processing speed.
The Mega 2560 board has a number of facilities for communicating with a computer, another
board, or other microcontrollers. The ATmega2560 provides four hardware UARTs for TTL
(5V) serial communication. An ATmega16U2 (ATmega 8U2 on the revision 1 and revision 2
boards) on the board channels one of these over USB and provides a virtual com port to
software on the computer (Windows machines will need a .inf file, but OSX and Linux
machines will recognize the board as a COM port automatically. The Arduino Software (IDE)
includes a serial monitor which allows simple textual data to be sent to and from the board.
The RX and TX LEDs on the board will flash when data is being transmitted via the
ATmega8U2/ATmega16U2 chip and USB connection to the computer (but not for serial
communication on pins 0 and 1).
Rather than requiring a physical press of the reset button before an upload, the Mega 2560 is
designed in a way that allows it to be reset by software running on a connected computer.
One of the hardware flow control lines (DTR) of the ATmega8U2 is connected to the reset
line of the ATmega2560 via a 100 nanofarad capacitor. When this line is asserted (taken low),
the reset line drops long enough to reset the chip. The Arduino Software (IDE) uses this
capability to allow you to upload code by simply pressing the upload button in the Arduino
environment. This means that the bootloader can have a shorter timeout, as the lowering of
DTR can be well-coordinated with the start of the upload.
The maximum length and width of the Mega 2560 PCB are 4 and 2.1 inches respectively,
with the USB connector and power jack extending beyond the former dimension. Three screw
9. 3 | P a g e
holes allow the board to be attached to a surface or case. Note that the distance between
digital pins 7 and 8 is 160 mil (0.16"), not an even multiple of the 100 mil spacing of the
other pins.
The Mega 2560 is designed to be compatible with most shields designed for the Uno and the
older Diecimila or Duemilanove Arduino boards. Digital pins 0 to 13 (and the adjacent AREF
and GND pins), analog inputs 0 to 5, the power header, and ICSP header are all in equivalent
locations. Furthermore, the main UART (serial port) is located on the same pins (0 and 1), as
are external interrupts 0 and 1 (pins 2 and 3 respectively). SPI is available through the ICSP
header on both the Mega 2560 and Duemilanove / Diecimila boards. Please note that I2C is
not located on the same pins on the Mega 2560 board (20 and 21) as the Duemilanove /
Diecimila boards (analog inputs 4 and 5).
TECHNICAL SPECIFICATION
13. 7 | P a g e
RTC:
A Real Time Clock (RTC) is basically just like a watch - it runs on a battery and keeps time
for you even when there is a power outage. Using an RTC, you can keep track of long
timelines, even if you reprogram your microcontroller or a power plug.
The real time clock (RTC) is widely used device that provides accurate time and date for
many applications. Many systems such as IBM pc come with RTC chip on mother board.
RTC chip uses an internal battery which keeps time and date even when the power is off. In
some microcontrollers have inbuilt RTC while others requires interfacing.
DS3231:
The DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated
temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a
battery input, and maintains accurate timekeeping when main power to the device is
interrupted. The integration of the crystal resonator enhances the long-term accuracy of the
device as well as reduces the piece-part count in a manufacturing line. The DS3231 is
available in commercial and industrial temperature ranges, and is offered in a 16-pin, 300-mil
SO package.
The RTC maintains seconds, minutes, hours, day, date, month, and year information.
The date at the end of the month is automatically adjusted for months with fewer than 31
days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour
format with an AM/PM indicator. Two programmable time-of-day alarms and a
programmable square-wave output are provided. Address and data are transferred serially
through an I2C bidirectional bus.
A precision temperature-compensated voltage reference and comparator circuit monitors the
status of VCC to detect power failures, to provide a reset output, and to automatically switch
14. 8 | P a g e
to the backup supply when necessary. Additionally, the RST pin is monitored as a pushbutton
input for generating a μP reset.
FEATURES:
Highly Accurate RTC Completely Manages All Timekeeping Functions
o Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month,
Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100.
o Accuracy ±2ppm from 0°C to +40°C.
o Accuracy ±3.5ppm from -40°C to +85°C.
o Digital Temp Sensor Output: ±3°C Accuracy
o Register for Aging Trim
o RST Output/Pushbutton Reset Debounce Input
o Two Time-of-Day Alarms
o Programmable Square-Wave Output Signal
Simple Serial Interface Connects to Most Microcontrollers
o Fast (400kHz) I2C Interface
Battery-Backup Input for Continuous Timekeeping
o Low Power Operation Extends Battery-Backup Run Time
o 3.3V Operation
Operating Temperature Ranges: Commercial (0°C to +70°C) and Industrial (-40°C to
+85°C)
15. 9 | P a g e
PIN CONFIGURATION:
Figure 3: DS3231 Pin Diagram
BLOCK DIAGRAM:
Figure 4: DS3231 Block Diagram
17. 11 | P a g e
WORKING:
The DS3231 is a serial RTC driven by a temperature-compensated 32kHz crystal oscillator.
The TCXO provides a stable and accurate reference clock, and maintains the RTC to within
±2 minutes per year accuracy from -40°C to +85°C. The TCXO frequency output is available
at the 32 kHz pin. The RTC is a low-power clock/calendar with two programmable time-of-
day alarms and a programmable square-wave output. The INT/SQW provides either an
interrupt signal due to alarm conditions or a square-wave output. The clock/calendar provides
seconds, minutes, hours, day, date, month, and year information. The date at the end of the
month is automatically adjusted for months with fewer than 31 days, including corrections for
leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM
indicator. The internal registers are accessible though an I2C bus interface.
A temperature-compensated voltage reference and comparator circuit monitors the level of
VCC to detect power failures and to automatically switch to the backup supply when
necessary. The RST pin provides an external pushbutton function and acts as an indicator of a
power-fail event.
POWER CONTROL:
This function is provided by a temperature-compensated voltage reference and a comparator
circuit that monitors the VCC level. When VCC is greater than VPF, the part is powered by
VCC. When VCC is less than VPF but greater than VBAT, the DS3231 is powered by VCC.
If VCC is less than VPF and is less than VBAT, the device is powered by VBAT.
18. 12 | P a g e
To preserve the battery, the first time VBAT is applied to the device, the oscillator will not
start up until VCC exceeds VPF, or until a valid I2C address is written to the part. Typical
oscillator start up time is less than one second. Approximately 2 seconds after VCC is
applied, or a valid I2C address is written, the device makes a temperature measurement and
applies the calculated correction to the oscillator. Once the oscillator is running, it continues
to run as long as a valid power source is available (VCC or VBAT), and the device continues
to measure the temperature and correct the oscillator frequency every 64 seconds.
On the first application of power (VCC) or when a valid I2C address is written to the part
(VBAT), the time and date registers are reset to 01/01/00 01 00:00:00 (DD/MM/YY DOW
HH:MM:SS).
ADDRESS MAP:
The address map for the DS3231 timekeeping registers. During a multi-byte access, when the
address pointer reaches the end of the register space (12h), it wraps around to location 00h.
On an I2C START or address pointer incrementing to location 00h, the current time is
transferred to a second set of registers. The time information is read from these secondary
registers, while the clock may continue to run. This eliminates the need to reread the registers
in case the main registers update during a read.
19. 13 | P a g e
The time and calendar information is obtained by reading the appropriate register bytes.
Figure 1 illustrates the RTC registers. The time and calendar data are set or initialized by
writing the appropriate register bytes. The contents of the time and calendar registers are in
the binary-coded decimal (BCD) format. The DS3231 can be run in either 12-hour or 24-hour
mode. Bit 6 of the hours register is defined as the 12- or 24-hour mode select bit. When high,
the 12-hour mode is selected. In the 12-hour mode, bit 5 is the AM/PM bit with logic-high
being PM. In the 24-hour mode, bit 5 is the 20-hour bit (20–23 hours). The century bit (bit 7
of the month register) is toggled when the years register overflows from 99 to 00.
20. 14 | P a g e
The day-of-week register increments at midnight. Values that correspond to the day of week
are user-defined but must be sequential (i.e., if 1 equals Sunday, then 2 equals Monday, and
so on). Illogical time and date entries result in undefined operation.
When reading or writing the time and date registers, secondary (user) buffers are used to
prevent errors when the internal registers update. When reading the time and date registers,
the user buffers are synchronized to the internal registers on any START and when the
register pointer rolls over to zero. The time information is read from these secondary
registers, while the clock continues to run. This eliminates the need to reread the registers in
case the main registers update during a read.
The countdown chain is reset whenever the seconds register is written. Write transfers occur
on acknowledge from the DS3231. Once the countdown chain is reset, to avoid rollover
issues the remaining time and date registers must be written within 1 second. The 1Hz square-
wave output, if enabled, transitions high 500ms after the seconds data transfer, provided the
oscillator is already running.
I2C DATA BUS
The DS3231 supports a bidirectional I2C bus and data transmission protocol. A device that
sends data onto the bus is defined as a transmitter and a device receiving data is defined as a
receiver. The device that controls the message is called a master. The devices that are
controlled by the master are slaves. The bus must be controlled by a master device that
generates the serial clock (SCL), controls the bus access, and generates the START and
STOP conditions. The DS3231 operates as a slave on the I2C bus. Connections to the bus are
made through the SCL input and open-drain SDA I/O lines. Within the bus specifications, a
standard mode (100 kHz maximum clock rate) and a fast mode (400 kHz maximum clock
rate) are defined. The DS3231 works in both modes.
21. 15 | P a g e
The following bus protocol has been defined (Figure 2):
● Data transfer may be initiated only when the bus is not busy.
● During data transfer, the data line must remain stable whenever the clock line is high.
Changes in the data line while the clock line is high are interpreted as control signals.
Accordingly, the following bus conditions have been defined:
Bus not busy: Both data and clock lines remain high.
START data transfer: A change in the state of the data line from high to low, while the
clock line is high, defines a START condition.
STOP data transfer: A change in the state of the data line from low to high, while the
clock line is high, defines a STOP condition.
Data valid: The state of the data line represents valid data when, after a START condition,
the data line is stable for the duration of the high period of the clock signal. The data on the
line must be changed during the low period of the clock signal. There is one clock pulse
per bit of data.
Each data transfer is initiated with a START condition and terminated with a STOP
condition. The number of data bytes transferred between the START and the STOP
conditions is not limited, and is determined by the master device. The information is
transferred byte-wise and each receiver acknowledges with a ninth bit.
Acknowledge: Each receiving device, when addressed, is obliged to generate an
acknowledge after the reception of each byte. The master device must generate an extra
clock pulse, which is associated with this acknowledge bit.
A device that acknowledges must pull down the SDA line during the acknowledge clock
pulse in such a way that the SDA line is stable low during the high period of the
acknowledge-related clock pulse. Of course, setup and hold times must be taken into
22. 16 | P a g e
account. A master must signal an end of data to the slave by not generating an
acknowledge bit on the last byte that has been clocked out of the slave. In this case, the
slave must leave the data line high to enable the master to generate the STOP condition.
Figures 5: Data transfer is accomplished on the I2C bus.
Depending upon the state of the R/W bit, two types of data transfer are possible:
Data transfer from a master transmitter to a slave receiver:
The first byte transmitted by the master is the slave address. Next follows a number of
data bytes. The slave returns an acknowledge bit after each received byte. Data is transferred
with the most significant bit (MSB) first.
Data transfer from a slave transmitter to a master receiver:
The first byte (the slave address) is transmitted by the master. The slave then returns
an acknowledge bit. Next follows a number of data bytes transmitted by the slave to the
master. The master returns an acknowledge bit after all received bytes other than the last byte.
At the end of the last received byte, a not acknowledge is returned.
The master device generates all the serial clock pulses and the START and STOP
conditions. A transfer is ended with a STOP condition or with a repeated START condition.
23. 17 | P a g e
Since a repeated START condition is also the beginning of the next serial transfer, the bus
will not be released. Data is transferred with the most significant bit (MSB) first.
Figures 6: Data transfer from slave transmitter to master receiver
The DS3231 can operate in the following two modes:
Slave receiver mode (DS3231 write mode):
Serial data and clock are received through SDA and SCL. After each byte is received, an
acknowledge bit is transmitted. START and STOP conditions are recognized as the beginning
and end of a serial transfer. Address recognition is performed by hardware after reception of
the slave address and direction bit. The slave address byte is the first byte received after the
master generates the START condition. The slave address byte contains the 7-bit DS3231
address, which is 1101000, followed by the direction bit (R/W), which is 0 for a write. After
receiving and decoding the slave address byte, the DS3231 outputs an acknowledge on SDA.
After the DS3231 acknowledges the slave address + write bit, the master transmits a word
address to the DS3231. This sets the register pointer on the DS3231, with the DS3231
acknowledging the transfer. The master may then transmit zero or more bytes of data, with
the DS3231 acknowledging each byte received. The register pointer increments after each
data byte is transferred. The master generates a STOP condition to terminate the data write.
24. 18 | P a g e
Figures 7: Slave receiver mode
Slave transmitter mode (DS3231 read mode):
The first byte is received and handled as in the slave receiver mode. However, in this
mode, the direction bit indicates that the transfer direction is reversed. Serial data is
transmitted on SDA by the DS3231 while the serial clock is input on SCL. START and STOP
conditions are recognized as the beginning and end of a serial transfer. Address recognition is
performed by hardware after reception of the slave address and direction bit. The slave
address byte is the first byte received after the master generates a START condition. The
slave address byte contains the 7-bit DS3231 address, which is 1101000, followed by the
direction bit (R/W), which is 1 for a read. After receiving and decoding the slave address
byte, the DS3231 outputs an acknowledge on SDA. The DS3231 then begins to transmit data
starting with the register address pointed to by the register pointer. If the register pointer is
not written to before the initiation of a read mode, the first address that is read is the last one
stored in the register pointer. The DS3231 must receive a not acknowledge to end a read.
Figures 8: Slave transmitter mode
25. 19 | P a g e
LCD:
The Oriole Display Module (ODM) is a dot matrix liquid crystal display that displays
alphanumeric, Kana (Japanese) characters and symbols.
The built-in controller and driver LSI’s provide convenient connectivity between the dot
matrix LCD and most 4 or 8 bit microprocessors or microcontrollers. All the functions
required for a dot matrix liquid crystal display drive are manually provided. Internal refresh is
provided by ODM. The CMOS technology makes the device ideal for application in hand
held, portable and the other battery powered instruments with low power consumption.
Figures 9: 16x2 character LCD
As the pin resources of Arduino controller is limited, your project may be not able to use
normal LCD shield after connected with a certain quantity of sensors or SD card. However,
26. 20 | P a g e
with this I2C interface LCD module, you will be able to realize data display via only 2 wires.
Figures 10: 16x2 character LCD interfaced with 12c module
FEATURES:
• 5 x 8 dots with cursor
• Built-in controller (KS 0066 or Equivalent)
• + 5V power supply (Also available for + 3V)
• 1/16 duty cycle
• B/L to be driven by pin 1, pin 2 or pin 15, pin 16 or A.K (LED)
• N.V. optional for + 3V power supply
After introducing 1602 I2C module interface.
Interface: I2C
I2C Address: 0x27
Pin Definition : VCC、GND、SDA、SCL
Back lit (Blue with white char colour)
Supply voltage: 5V
Size : 27.7mm×42.6mm
Contrast Adjust : Through Potentiometer
Only employ two I/O interface
27. 21 | P a g e
CONNECTION DIAGRAM:
Here is picture that shows how to connect an Arduino 1602 I2C module. The following is a
table describing which pins on the Arduino should be connected to 1602 I2C LCD module.
Figures 11: 16x2 character LCD interfaced with 12c module and Arduino Mega 2560
CONTRAST ADJUSTMENT
Figures 12: 16x2 character LCD interfaced with 12c module and contrast adjuster
Spinning the potentiometer clockwise is to increase contrast, spinning anti-clockwise is to
decrease it.
28. 22 | P a g e
POWER SUPPLY
The power supply is provided to the whole setup through the Arduino data cable or through a
normal adapter (12V).
LIST OF COMPONENTS
Arduino MEGA 2560
DS3231
16x2 LCD
1602 I2C module
Vero Board
Buzzer
PROGRAM CODE
#include <Wire.h>
#include "ds3231.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define BUFF_MAX 128
int c=0,c1=0,c2=0,t1,d=0,e=0,d2=0,dy=0;
uint8_t time[8];
charrecv[BUFF_MAX];
unsignedintrecv_size = 0;
unsigned long prev, interval = 1000;
void setup()
{
29. 23 | P a g e
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_INTCN);
memset(recv, 0, BUFF_MAX);
Serial.println("GET time");
lcd.begin(16, 2);
lcd.clear();
digitalWrite(13,LOW);
//Serial.println("Setting time");
//parse_cmd("T101921205042016",16);
}
void loop()
{
char in;
chartempF[6];
float temperature;
char buff[BUFF_MAX];
unsigned long now = millis();
structts t;
// show time once in a while
if ((now - prev> interval) && (Serial.available() <= 0))
{
DS3231_get(&t); //Get time
parse_cmd("C",1);
temperature = DS3231_get_treg(); //Get temperature
dtostrf(temperature, 5, 1, tempF);
lcd.clear();
30. 24 | P a g e
lcd.setCursor(1,0);
lcd.print(t.mday);
printMonth(t.mon);
lcd.print(t.year);
dy=dayofweek(t.mday, t.mon, t.year);
printWday(dy);
lcd.setCursor(0,1); //Go to second line of the LCD Screen
if(t.hour>23)
{
lcd.print("00");
Serial.print("00");
}
else
{
lcd.print(t.hour);
Serial.print(t.hour);
}
lcd.print(":");
Serial.print(":");
if(t.hour==13)
{
d=1;
}
if((t.min==t1+1)&&(t.hour==8)&&(d==0))
{
c++;
Serial.print("c=");
31. 25 | P a g e
Serial.println(c);
if(c==50&&c1<6)
{
digitalWrite(13,HIGH);
Serial.print("Alarm");
delay(20000);
c1++;
c=0;
}
}
t1=t.min;
if((t.min==30)&&(t.hour==14))
{
d2=1;
}
if((t.min==t1+1)&&(t.hour>=17)&&(d2==1))
{
e++;
if(e==50&&c2<=3)
{
digitalWrite(13,HIGH);
delay(20000);
c2++;
e=0;
if(c2==3)
d2=0;
}
32. 26 | P a g e
}
if(t.min<10)
{
lcd.print("0");
Serial.print("0");
}
lcd.print(t.min);
Serial.print(t.min);
lcd.print(":");
Serial.print(":");
if(t.sec<10)
{
lcd.print("0");
Serial.print("0");
}
lcd.print(t.sec);
Serial.print(t.sec);
lcd.print(' ');
lcd.print(tempF);
lcd.print((char)223);
lcd.print("C ");
prev = now;
}
if (Serial.available() > 0)
{
in = Serial.read();
if ((in == 10 || in == 13) && (recv_size> 0))
33. 27 | P a g e
{
parse_cmd(recv, recv_size);
recv_size = 0;
recv[0] = 0;
}
else if (in < 48 || in > 122)
{
; // ignore ~[0-9A-Za-z]
}
else if (recv_size> BUFF_MAX - 2)
{ // drop lines that are too long
// drop
recv_size = 0;
recv[0] = 0;
}
else if (recv_size< BUFF_MAX - 2)
{
recv[recv_size] = in;
recv[recv_size + 1] = 0;
recv_size += 1;
}
}
}
voidparse_cmd(char *cmd, intcmdsize)
{
uint8_ti;
uint8_t reg_val;
34. 28 | P a g e
char buff[BUFF_MAX];
structts t;
//snprintf(buff, BUFF_MAX, "cmd was '%s' %dn", cmd, cmdsize);
//Serial.print(buff);
// TssmmhhWDDMMYYYY is set time
if (cmd[0] == 84 &&cmdsize == 16)
{
//T204001502042016
t.sec = inp2toi(cmd, 1);
t.min = inp2toi(cmd, 3);
t.hour = inp2toi(cmd, 5);
t.wday = inp2toi(cmd, 7);
t.mday = inp2toi(cmd, 8);
t.mon = inp2toi(cmd, 10);
t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
DS3231_set(t);
Serial.println("OK");
}
}
int dayofweek(int d, int m, int y)
{
staticint t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7);
}
35. 29 | P a g e
voidprintMonth(int month)
{
switch(month)
{
case 1: lcd.print(" JAN ");break;
case 2: lcd.print(" FEB ");break;
case 3: lcd.print(" MAR ");break;
case 4: lcd.print(" APR ");break;
case 5: lcd.print(" MAY ");break;
case 6: lcd.print(" JUN ");break;
case 7: lcd.print(" JUL ");break;
case 8: lcd.print(" AUG ");break;
case 9: lcd.print(" SEP ");break;
case 10: lcd.print(" OCT ");break;
case 11: lcd.print(" NOV ");break;
case 12: lcd.print(" DEC ");break;
default: lcd.print(" Error ");break;
}
}
voidprintWday(intwd)
{
switch(wd)
{
case 0: lcd.print(" SUN ");break;
case 1: lcd.print(" MON ");break;
case 2: lcd.print(" TUE ");break;
case 3: lcd.print(" WED ");break;
36. 30 | P a g e
case 4: lcd.print(" THU ");break;
case 5: lcd.print(" FRI ");break;
case 6: lcd.print(" SAT ");break;
default: lcd.print(" Error ");break;
}
}
CIRCUIT DIAGRAM:
Figures 13: Circuit Diagram of whole setup
37. 31 | P a g e
RESULTS AND DISCUSSION
The above program was compiled in ‘C’ language and was simulated in the Arduino
software. The required components for the real time clock of required specifications were
connected according to the circuit diagram specified. First the Arduino Board was
soldered to the Vero board with the use of male contact pins. Then it was connected to the
DS3231 IC, this in turn was again connected to the buzzer and LCD. After the program
was fed to the Arduino board by connecting it to the laptop through the provided USB, power
supply was given from the power adapter and the process of simulation started. After
successfully running the program the following things were observed and noted:
DS3231 IC calculated the date and time from the Arduino board according to the
program as it was given. It also sensed the external ambience temperature and
processed it so that it can be fed to the displaying devices and the day of the week was
calculated with the help of the date received from the DS3231 IC in Arduino.
All the information hence were provided to the LCD and it displayed the given
information in the following format :-
dd mm yyyy day
hour minute second temp in OC
The buzzer which was also connected to the chip took its input from the Arduino
which manipulated and compare the required time from the DS3231 IC and it buzzed
after every 50 minutes interval starting from 08:00 A.M. to 01:00 P.M. and again from
02:30 P.M. to 05:00 P.M.
Finally it was given the shape of a clock for protecting it from external force.
38. 32 | P a g e
SUMMARY
In this project for real time application i.e. to show the time, date, day and temp according to
the environment, DS3231 chip was used in which a program was preinstalled using the
Arduino board. The program was compiled and tested using the Arduino software. After
connecting all the components in the Vero board the program was run successfully and LCD
and buzzer showed the required output. In this way our aim of showcasing the day time date
and temp in a single device and to notify the time by using the buzzer was achieved. This can
become handy in case of power failure as it does not require any power supply for its
operation and can be used in areas where power supply is not present like in remote areas and
army camps. It has a great future scope and further work can be done to improve its
application.
39. 33 | P a g e
REFERENCES
1. http://www.instructables.com/id/Arduino-Real-Time-Clock-DS1307/
2. https://www.youtube.com/watch?v=F2wpdbo1EZs
3. http://tronixstuff.com/2014/12/01/tutorial-using-ds1307-and-ds3231-real-time-clock-
modules-with-arduino/
4. DS3231 specification sheet
5. LCD(ODM) specification sheet
6. Arduino Forum: https://www.arduino.cc