DS3231 REAL-TIME CLOCK WITH I2C LCD AND ARDUINO NANO

by OnosTech in Circuits > Microcontrollers

9304 Views, 6 Favorites, 0 Comments

DS3231 REAL-TIME CLOCK WITH I2C LCD AND ARDUINO NANO

Picture of Real-Time Clock.png

In this instructable, you will learn how to build a breadboard circuit of a real-time clock with Arduino Nano that will display the time and date on an LCD. The time and date are adjustable to the current time and date. This will require a DS3231 Real-time Clock module, a PCF8574 I/O expander that connects to 16x2 Liquid Crystal Display (LCD) via the same I2C bus as the DS3231 module. The program for this build will be created with LDmicro, an open-source lightweight application. You can ladder logic for programming PIC, ATMEL and ARM microcontrollers. The Arduino Nano will be programmed directly with LDmicro.

Supplies

1. One Arduino Nano & USB Cable

2. One DS3231 Real-Time Clock Module

3. One 16x2 Liquid Crystal Display

4. One PCF8574 I/O Expander

5. Four 10 Kilo-Ohm Resistors

6. Four Push-Button Switches

7. One Breadboard

8. Jumper Wires

Understanding How the Different Component Work

Arduino Nano.png
DS3231.png
LCD.png
PCF.png

ARDUINO NANO

The Arduino Nano is an open-source programmable small size circuit board. It is a breadboard-friendly microcontroller board built with AVR ATmega328 microcontroller. It comes with a 16 MegaHertz crystal oscillator. It doesn’t require a separate programmer as it can be directly connected to a computer.It can be directly connected to a power source of between 7 to 12V.

The Arduino Nano board also have markers at the top to signifies the start of the pin numbering from one (1) at the top left to thirty (30) at the top right. Most Arduino Nano pins have more than one functions. Pins three (3) and twenty-eight (28) is provided for Reset purpose when the board is in operation. The power supply pins are four (4) and twenty-nine (29) for the negative connection. While, the positive power connections are seventeen (17) for 3.3 volts, twenty-seven (27) for 5 volts and thirty for Vin. Power source ranging from 7 to 12 volts can be connected to Vin. The Digital inputs and output ports are pins five (5) to sixteen (16) with the label of D2 to D13. Analog Input ports are pins eighteen (18) to twenty-six (26) with the label of Aref, ADC0 to ADC7. Pins one (1) and two (2) labelled as TX and RX are for serial data exchange with other devices.

DS32321 REAL-TIME CLOCK MODULE

The DS3231 real-time clock module is an I2C device that provides accurate real-time clock function with a calendar that sustains seconds, minutes, hours, day, date, month, and year data. It supports two time-of-day alarm function with a square-wave output as well as temperature measurement. The module has a battery holder with a 3V lithium battery to maintain the time and date when the supplied power is off. It also has an addressable EEPROM. In addition to the power supply and I2C pins, it has a 33K output for a stable and accurate reference clock and SQW output pin associated with the alarm. The I2C address for the RTC is fixed at hexadecimal 68. The registers address for seconds, minutes, hours, day, date, months, and years are 00, 01, 02, 03, 04, 05 and 06 respectively.

LIQUID CRYSTAL DISPLAY

The liquid crystal display or LCD is a parallel device used for character display. It provides an interface to users on the operation of a device. The16x2 character LCD can display 2 rows of 16 characters and has 16 connecting contacts. The first two contacts, powers the LCD. Contact Vo when connected with a 10 kilo-Ohms a variable resistor is used to adjust the contrast of the display. RS, RW and E control the operation of the LCD. The LCD character information is received via the data bus, from D0 to D7. Contacts A and K connect to the power supply to power up the backlight.

PCF8574 I/O I2C EXPANDER MODULE

The PCF8574 I/O expander module has 16 pins that connect directly to the LCD pins, an I2C data (SDA) and clock (SCL) pins. Power is supplied pins that also power the LCD. The contrast of the LCD can be adjusted with a potentiometer. The module is addressable by solder pads marked as A0, A1 and A2. For most modules, the address default to 27 or 3F hexadecimal when the pads are open.

Circuit Diagram for the Real-Time Clock

I2C LCD and DS3231 Real-Time Clock with LDmicroCircuit.png

Set up your breadboard circuit with this circuit diagram containing the DS3231 module, I2C LCD and four push-button switches with 10 kilo-ohms pull-down resistors, all connected to the Arduino Nano. The Arduino Nano uses pin A4 for SDA and pin A5 for SCL I2C communication. Connect the SCL pin of the module to pin A5 of the Arduino nano. SDA to pin A4 of the nano. VCC to the 5V pin of the nano and GND to GND of the nano.

Ladder Logic for the Real-Time Clock

Slide71.PNG
Slide72.PNG
Slide73.PNG
Slide74.PNG
Slide75.PNG
Slide76.PNG
Slide77.PNG
Slide78.PNG

Create a program in LDmicro to display, the seconds, minutes, hours, dates, months, and years. These can be set with the four push-button switches menu option. Rung 2 has the menu contact that increases a circular counter through 5 options to set the date, month, year, hour, and minute of the real-time clock. The default counter value of zero selects the option to acquire the values of the date and time from the DS3231 module. In rung 3, when Cmenu is zero, the I2C read instruction, reads the second value at register zero from the module at device address 68 hexadecimal and stores it into variable “sec”. The read value is then converted from Binary Coded Decimal to Binary by this instruction. The second value is then separated into the two digits, converted to ASCII format, and stored in variables “stens” and “sunit”. Similar to rung 3 are rungs 5, 7, 9, 11 and 13. Rung 5 acquires the minute value from register address one and stores the separated ASCII digits at “mtens” and “munit”. Down to rung 13 that acquires the year value from register address six and stores the separated ASCII digits at “ytens” and “yunit”.

When the “Xmenu” contact sets “Cmenu” to one, rung 15 is enabled to set the date in the RTC module. These I2C write instructions set the cursor at column 15, row 1 of the LCD to display a less than symbol and character “D” that indicates the setting of the date. And then move the current value of the date to variable “cdate”. “Cdate” variable is increased or decreased by contacts “Xinc” and “Xdec”. This section ensures that the selected date is within 1 and 31. The date is separated into two digits and converted into their ASCII values. When “Xenter" is activated, “cdate” value is moved back to the “date” variable, converted from binary to binary-coded-decimal and sent to the RTC module register address of four to set the date. Rungs 17 and 19 are similar to rung 15 but sets the month and year respectively.

Rung 20 clears the less than symbol and the menu option indicator from columns 15 & 16, row 1 of the LCD. Rungs 22 and 24 are also similar to rung 15, but they set the hour and minute respectively. Then, rung 25 clears the less than symbol and the menu indication from the LCD as before. Rung 27 turns ON the backlight. Rung 28 will display the text “DATE” starting from column 1, row 1 of the LCD. Rung 29 displays the acquired values of the date, month and year separated by a dash, starting from position 86 hexadecimal. Similarly, rung 30 will display the text “TIME” in row two of the LCD. Finally, the Hours, minutes and seconds separated by colons will be displayed on the LCD, starting from position A6 hexadecimal.

Configure the push button switch input pins as the Arduino nano pins of D2 for Cmenu, D3 for Xinc, D4 for Xdec, D5 for Xenter, A5 for I2C SCL and A4 for I2C SDA. Compile the program as before, build C solution and call flashMCU. Observe the display of the calendar and time.

Setting the Current Date and Time

Test.png
Month.png

To adjust the calendar and time, press the menu button once. Observe a less than D at the top right of the LCD. Repeatedly press the increase or decrease button to adjust to today’s date. Then press the enter button to save. Press the menu button again to adjust the month, repeating the previous steps. Press the menu button to set the year, hour, and minute, observing the indication of the parameter to be set on the right side of the LCD, until the current date and time is being displayed on the LCD.

Conclusion

LDmicro 14: I2C LCD & DS3231 Real-Time Clock (Microcontroller PLC Ladder Programming with LDmicro)

This video explains in details the I2C protocol and how to set LDmicro to program with the I2C instruction elements.

The link below contains LDmicrco ladder logic and the generated hex file.

https://drive.google.com/file/d/1yHBI_lBLd2wjYjpuR...

Below is the link to download LDmicro application.

https://cq.cx/ladder.pl