Real-Time Clock

Learn how to use a DS3231 Real-Time Clock for accurate time and date in your projects!

Project description

*******

Please visit $ https://proteshea.com/ds3231-real-time-clock-with-arduino-uno/ $ for a complete list of materials for this project.

*******

Introduction

In this project, you’ll be learning how to interface a Real-Time Clock (RTC) to the Arduino Uno to acquire the time, date, day of the week, Unix time, and even temperature. We’ll cover three different examples where we send the data to the Serial Monitor, send the data to a 20×4 LCD, and turn on an LED on a certain day of the week. Time is running out, so let’s get started!

20×4 Character LCD

The 20×4 LCD adds two extra rows and four extra columns per row compared to the 16×2 LCD. Similar to the 16×2 that we’ve used in previous projects, the 20×4 LCD uses the Hitachi controller so the commands and interfaces are the same. It also has the same 16-pin header, allowing you to unplug the 16×2 LCD and plug in the 20×4 without changing any wiring. The only thing we have to change is one line of code, lcd.begin(20, 4) , which specifies the columns (first argument) and rows (second argument) of the LCD. An image of the 20×4 LCD is shown below.

20×4 LCD Mounted to Modulus

DS3231 RTC

The DS3231 RTC is used to accurately provide the day of the week, date (DD/MM/YYYY), time, Unix time, and even temperature. There is a 32kHz crystal and a temperature sensor internally. The temperature sensor is used to provide feedback to the crystal since temperature effects the frequency (i.e., clock drift) of the crystal. We are using Adafruit’s DS3231 RTC breakout board which breaks out 8 pins: Vin, GND, SCL, SDA, BAT, 32k, SQW, and an active-low RST. We will only be using pins Vin, GND, SCL, and SDA. A table of specifications for the module is shown below.

DS3231 RTC Specifications

There is also a battery slot to insert a CR1220 coin-cell battery which keeps track of the time and date when main power is turned off. If you don’t have this battery, you will lose this data and will have to reprogram the time and date on the DS3231 when main power is restored.

To calculate how long the battery will last, we first have to know the mAh (milliamp hours) which is the capacity of the battery. The particular cell we are using doesn’t list this, but after a quick search, the 1220 series range from about 35 – 40 mAh. The datasheet does not list how much current draw the DS3231 pulls when it is being powered from the battery, so we’ll just use its standby current draw of about 140μA. Using dimensional analysis, we can calculate the lifespan of the battery, as shown below.

Calculating Lifespan of Battery

The DS3231 uses the I2C protocol for communication which consists of two wires, SCL and SDA. We will be using a library created by Henning Karlsen which is available at his website: $ http://www.rinkydinkelectronics.com/library.php?id=73 $ 

With this library, we are able to program the initial time, and then retrieve the day of the week, data, Unix time, and temperature.

Solderless Breadboard

If you are using a solderless breadboard, use the schematic below to make the necessary connections to the Uno.

Tip : The schematic shows a 16×2 character LCD, but the 20×4 LCD is pin compatible. The only change you have to make is in the software.

Breadboard Circuit Schematic

FuelCan Wiring

If you haven’t mounted the Uno onto the prototyping area of the FuelCan, go ahead and do that. If you are using a breadboard instead of Modulus, place the breadboard in the bottom storage compartment to limit the length of the jumper wires. You’ll need to supply +5V and GND to the power and ground rails on the breadboard by using the provided banana jack to test-lead clip cables. You will need two male header pins to mount the test-lead clips on the breadboard side. Plug the Type A side of the USB cable into USB1 receptacle and the Type B side into the Uno’s receptacle. Power up the FuelCan with the AC-DC power adapter.

For additional information about the Fuelcan-910, click $ here $ $ . $ 

Software

Once the wiring is complete and the FuelCan is powered up, we can now load the sketch onto the Uno. The first sketch is below. To initialize the DS3231 with the date and time, we have to uncomment rtc.setDOW() , rtc.setTime() , and rtc.setDate() . Input the current time and date as arguments to their respective functions, and upload the sketch to the Uno.

Once you have initialized the original day, time, and date, comment out those three functions, and upload the new sketch. The Serial Monitor will display the current day, date (DD.MM.YYYY), time (in a 24-hour format), and Unix time (number of seconds that have elapsed since 1 January 1970), as shown in the image below. Make sure that the baud rate is set to 115200 .

Displaying Data on Serial Monitor

//Interface the DS3231 Real-Time Clock (RTC) to an Arduino Uno //Displaying the day, date, time, and Unix time on the Serial Monitor //DS3231 SDA pin -> Arduino Uno A4 //DS3231 SCL pin -> Arduino Uno A5 //DS3231 has internal pull-up resistors on SDA & SCL signals //Using DS3231 Library created by Henning Karlsen //Can be downloaded from his website at www.rinkydinkelectronics.com #include

For the second sketch, we are writing the date, time, Unix time, and temperature to the 20×4 LCD instead of the Serial Monitor. The sketch is below as well as an image of what is displayed on the LCD.

//Interface the DS3231 Real-Time Clock (RTC) to an Arduino Uno //Display date, time, Unix time, and temperature on a 20x4 LCD //DS3231 SDA pin -> Arduino Uno A4 //DS3231 SCL pin -> Arduino Uno A5 //DS3231 has internal pull-up resistors on SDA & SCL signals #include

In the third sketch below, we turn on the Uno’s LED (routed to pin 13) on Monday of each week. This could be useful if you wanted to build your own controller for a lawn sprinkler system.

//Interface the DS3231 Real-Time Clock (RTC) to an Arduino Uno //Turn on LED if day is Monday //DS3231 SDA pin -> Arduino Uno A4 //DS3231 SCL pin -> Arduino Uno A5 //DS3231 Breakout board has pull-up resistors on SDA & SCL signals //Using DS3231 Library created by Henning Karlsen //Can be downloaded from his website at www.rinkydinkelectronics.com #include

Similar Posts

Leave a Reply