Vous êtes sur la page 1sur 4

WE USE ARDUINO AND A RTC DS3231 MODULE TO

DISPLAY THE TIME AND DATE


ON AN I2C 16X02 LCD DISPLAY
In this project we will show a simple example of clock and calendar using an Arduino UNO
board and a DS3231 RTC module, where the time and date are displayed on the i2C 1602 LCD
screen.
The beauty of this project is that we can modify the time and date by acting directly on two
buttons placed on a breadboard.

The DS3231 RTC module shown in the figure is equipped with a 3V CR2032 battery and is able
to provide information on date, time and ambient temperature.
Thanks to the battery it is equipped with, once the time and date have been memorized, it keeps
on counting the time, even when it is not in use or removed from the circuit.
The module supplies the information to the Arduino through the I2C communication protocol or
SDA / SCL bus.
In Arduino Uno this protocol is managed by the A4 and A5 ports in Arduino Mega 2560 on ports
D20 and D21.
In this exercise we will not only use the RTC DS3231 module, but we will also use a 16x02 LCD
display managed by an I2C driver and then we will have two components that use the SDA /
SCL bus at the same time.

COMPONENTS:
Arduino ONE
RTC DS3231
16x02 I2C LCD Display
2x Microswitch buttons
2x 10KOhm resistors
Breadboard
Jumper cables MM / MF

CONNECTION DIAGRAM:

In the circuit there are 2 buttons connected to pins 8 and 9 respectively. The two buttons are used
to set the date and time parameters (minutes, hours, date, month and year).
The first button selects the parameter and the second one increases the selected parameter.

LIBRARIES:
First we need to install a library on the Arduino IDE.
We will use the library Newliquidcrystal_1.3.5 for operating the I2C LCD display.

To install the libraries open Arduino IDE and click Sketch> #includi Library> Add to Library ..
.ZIP file and select the library you just downloaded.

The following Arduino sketch does not use any library for the DS3231 RTC module, the Wire
library is for communication between the Arduino and the DS3231 for the I2C protocol.

Download file
SKETCH:

Download file

The DS3231 module only works with the BCD format ( Binary-coded decimal on wikipedia )
and to convert the BCD to decimal and vice versa I used the following 2 lines (example per
minute):

// Convert BCD to decimal


second = ( second >> 4 ) * 10 + ( second & 0x0F ) ;
minute = ( minute >> 4 ) * 10 + ( minute & 0x0F ) ;
hour = ( hour >> 4 ) * 10 + ( hour & 0x0F ) ;
date = ( date >> 4 ) * 10 + ( date & 0x0F ) ;
month = ( month >> 4 ) * 10 + ( month & 0x0F ) ;
year = ( year >> 4 ) * 10 + ( year & 0x0F ) ;

// Convert from decimal to BCD


minute = (( minute / 10 ) << 4 ) + ( minute % 10 ) ;
hour = (( hour / 10 ) << 4 ) + ( hour % 10 ) ;
date = (( date / 10 ) << 4 ) + ( date % 10 ) ;
month = (( month / 10 ) << 4 ) + ( month % 10 ) ;
year = (( year / 10 ) << 4 ) + ( year % 10 ) ;
Displays the time and calendar, before displaying the time and calendar data are converted from
the BCD format to the decimal format.

void DS3231_display ( ) { }
Inside this small function at the bottom is the pause of the blinking and is interrupted by the
buttons (connected to pins 8 and 9).
With this function we can see the blinking of the selected parameter with a frequency of 2Hz.
So a delay of 250 ms arrives after printing the selected parameter and after this delay 2 spaces
are printed to make the parameter disappear on the LCD display, then another 250 ms delay
arrives after printing the 2 spaces.

void blink_parameter ( ) {
byte j = 0 ;
while ( j < 10 && digitalRead ( 8 ) && digitalRead ( 9 )) {
j ++ ;
delay ( 25 ) ;
}
}

Vous aimerez peut-être aussi