arduino real time clock ds3231 with lcd display manufacturer
In this Arduino Tutorial we will learn how to use the DS3231 Real Time Clock Module. You can watch the following video or read the written tutorial below.
The first question that comes here is why we actually need a separate RTC for our Arduino Project when the Arduino itself has built-in timekeeper. Well the point is that the RTC module runs on a battery and can keep track of the time even if we reprogram the microcontroller or disconnect the main power.
The DS3231 is a low-cost, highly accurate Real Time Clock which can maintain hours, minutes and seconds, as well as, day, month and year information. Also, it has automatic compensation for leap-years and for months with fewer than 31 days.
Once we connect the module we need to program the Arduino Board to work with the Real Time Clock. However, when it comes to programing a communication between Arduino and an I2C module the code isn’t that small and easy. Luckily, there are already several libraries for the DS3231 RTC which can be found on the internet.
So once we download and install the library we can use its first demo example to initially activate the clock of the RTC module. In the setup section of the demo example code we can notice that there are three line that we need to uncomment in order to initially set the day of the week, the time and the data.
The first line is for setting the day of the week, the second line is for setting the time in hours, minutes and seconds, and the third line is for setting the date in days, months and years.
Now even if we disconnect the Arduino power and then reconnect it and run the Serial Monitor again we can notice that the time keeps going without being reset.
So now we have our Real Time Clock up and running and we can use in any Arduino Project. As a second example I connected an LCD to the Arduino and printed the time and the date on it.
In this project, I will discuss about DS3231 RTC Module, important components and features of this module and finally show you how to Interface a DS3231 Real Time Clock (RTC) Module with Arduino.
Real Time Clock or RTC is a timekeeping device in the form of an Integrated Circuit or IC. RTC is an integral component of many time critical applications and devices like Servers, GPS, Data Loggers etc.
With 8051, I have used DS1307 RTC Module in a project called RFID BASED CAR PARKING SYSTEM. Coming to Arduino, I have used the same DS1307 RTC in ARDUINO ALARM CLOCK and ARDUINO REAL TIME CLOCK TUTORIAL USING DS1307. If you want a quick reference, you can go through the provided links.
Also, in the Arduino Real Time Clock Tutorial using DS1307 project, I have talked about the need for an RTC. So, I won’t go into that aspect again. I will directly jump into the IC of interest: the DS3231 RTC IC.
The DS3231 is an RTC IC developed by Maxim Integrated. It is a low cost, extremely accurate RTC IC with communication over I2C Interface. An interesting feature of DS3231 RTC IC is that it has integrated crystal oscillator and temperature sensor and hence you don’t have to connect an external crystal.
Using DS3231 IC as the main component, several manufacturers developed DS3231 RTC Modules with all the necessary components. Almost all the modules available today consists of an additional IC, 24C32N (or something similar). This secondary IC is an EEPROM IC of 32Kb size.
Since RTC is all about maintaining time irrespective of the power supply, you can connect a 3V CR2032 Lithium Battery to the RTC IC to keep the clock ticking. In the DS3231 Module, there is a provision for you to connect a battery using the battery holder provided on the back.
As mentioned earlier, DS3231 IC and 24C32 EEPROM IC are the main components on a typical DS3231 RTC Module board. Apart from that, there are a few other components like Power ON LED, few resistors, capacitors, a battery holder and pins for connecting with microcontroller.
First, let me begin the connections between Arduino and DS3231. Since the interface between them is I2C, identify the I2C Pins on your Arduino Board (if you are using any other board than UNO).
In Arduino UNO, A4 and A5 are SDA and SCL pins. Connect these pins with corresponding SDA and SCL pins of the DS3231 Module. Also, connect the VCC and GND of the RTC Module to +5V and GND of Arduino.
I have used a special library called “RTClib” from Adafruit (which is a forked version of JeeLab’s RTC Library). Download the library from this link and place the extracted folder in the libraries directory of Arduino.
The working of the Arduino DS3231 RTC Module Interface is very easy. Arduino first initializes the RTC Module with its slave address (0x68 for DS3231 IC).
Arduino then updates the internal registers of the RTC IC with the date and time at which the code is compiled and uploaded to Arduino. The uploaded date and time can be viewed on the LCD display.
Z:\Arduino\Sketch Kühlschrank\Set_DS3231\RTCTFT160\RTCTFT160.ino:124:7: note: ‘void printText(char*, uint16_t, int, int, int)’ previously defined here
Real time clock module with the DS3231 chip. This module ensures that the Arduino knows the exact time at all times. By connecting the Arduino to the ´qw´ pin, it is possible to generate an interrupt every second with which sensor values or a display can be addressed.
This module uses the DS3231, which is the big brother of DS1307. This chip has an integrated timing crystal and temperature sensor, which provides more stability. With this module you have just that little extra precision and reliability that the predecessor is missing. The chip is fully compatible with the predecessor and therefore requires no modification in the source code.
Edit 20220301 - All units STOPPED working at the end of 2021. Battery still good. The current advertisement still says, "until 2100". Maybe it is planned obsolesence? Any how, at this point I will have to locate another vendor"s RTC to get my RPi"s back to automated time without an internet connection. Or at each power-on the temporary solution is to enter: sudo date -s "month day year hh:mm:ss" where month would be like Feb. The day would be 1 to {28|29|30|31} and the year like 2022. ( e.g. sudo date -s "Feb 28 2022 23:59:59" )
Edit 20200608 -- Just noticed: Complete clock calendar function includes second, minute, time, week, date, month, and year, and provide leap year?compensation until 2100;? This AMBIGIOUS 2100 is curious. What does it mean? Lowered my rating one star.
There were no setup instructions. I did find some with a search. Once I had the setup instructions the clock installed and worked flawlessly. The only caution is to make sure it is put on the J8 end of the pin-outs. Installed on a B, B+, 0, and RPi 4 B. The welded power cell will be a point of contention if it dies. Oh well, the price and the simplicity of installation makes it a good purchase. Locating setup instructions could be an issue as could be the software step needed to get it working. If I get more RPi and the vendor is still providing these devices, I would probably buy it again!
If you receive a product with a manufacturing defect, please notify us within the 3 days of you receive the product, supported by proper pictures and description. Once our support team accept the return, we will provide a replacement or a complete refund including the return shipping cost. Please note that if your item is already soldered or modified in manner we will not be able to take it under return.
We don"t accept the returns for the products damanged by improper use of the product. Moreover we don"t accept the return, if the ordered product is not fit for any specific application. Please read the product specifications and datasheet before selecting and ordering a product. Returns are accepted only with 3 days from the date of delivery.
We ship to all over India with free shipping on all prepaid orders. For Cash on Delivery orders INR 70 will be charged for orders below INR 599 and INR 20 will be charged for the orders above 599. Please contact to our support team at support@quartzcomponents.com for any question related to shipping.
…includes a very accurate temperature-compensated crystal oscillator and crystal. An optional backup battery (not included) can maintain the correct time even when power is interrupted.
We offer an impressive array of Real Time Clocks, in which Automatic Siren operates without manual intervention and is used to avoid manual errors and aid the security personnel.
Real time clock from Mabara manufacturing company has unique features designed especially for lift applications. It has dual mode operating voltage, which could be operated either in 12v or 24v application. High bright 7-segment LED’s in this clock are giving more light with less power consumption. This clock offers you toread more...
We try our best to reach each and every corner of India using a few of the best courier services running in the Country such as Delhivery, DTDC, BlueDart, XpressBees, Ecom Express, etc. as per the feedback for the courier partner at the customer"s location. A few of the interior parts of India which are not covered by these courier services are covered by India-Post by us. We apply our best effort on daily basis to dispatch the order the same day it is ordered or within the next 24 hours of the order placed. Most of the orders that are placed before 1 PM are dispatched and shipped the same day. The orders placed post that is scheduled for next day shipment. The same effort is applied throughout the week including weekdays and sometimes weekends and public holidays as well. We facilitate local pickup (self-pickup for the local customers) on the weekdays and partially on weekends also.
rtc module clock arduino rtc arduino clock clock real time arduino clock rtc ds1307 arduino clock rtc 3231 clock raspberry rtc ds3231 ds3231 rtc real time clock rtc raspberry nano shield real time clock arduino clock rtc rtc module rtc module rtc module rtc arduino adduino arduous raspberry afruit ino ds3231 raspberry clock arduino time arduino rtc module rtc module ds3231 module ds3231 module ds1307 time arduino rtc ds1307 arduino adafruit arduino i2c arduino real time modul horlog arduino horlog camera ds 3231 rtc modul ds. 3231 arduino arduino real time clock raspberry rtc arduino ds1307 azdelivery rtc clock rtc arduino arduino rtc ds3231 arduino time azdelivery real time clock ds 1307 clock ds3231 i2c clock real time clock module real time raspberry rtc ds 1307 rtc i2c module clock arduino ds1 307 module rtc ds1307 pi rtc rtc 1307 ds3231 at24c32 clock rtc raspberry module time arduino arduino rtc clock ds3231 and battery horlog arduino azdelivery arduino rtc module arduino ds1307 arduino arduino arduino clock module ds1307 rtc rtc clock DS3231 rtc module
The Arduino is an amazing device. It’s useful for prototyping and can also be used to construct a complete project. It has an analog to digital converters (ADC), digital I/O pins, it handles interrupts and it can communicate via a serial port, SPI, and I2C.
A “real time clock” is essentially a digital clock whose output can be read by a computer or microcontroller. It has its own oscillator that it uses to count time and it has registers that allow you to set the current time and date.
The device you are using to read this article likely has a real time clock and if you are attached to the Internet you probably are synchronizing this to a network time server (if your device is a phone it may be time synchronized to your telephone companies carrier signal instead).
Unix time is the number of seconds that have elapsed since midnight January 1, 1970 (which was a Thursday if you’re interested). If you are on Linux (or at the terminal on a Mac, which uses Unix as its underlying operating system) you can type “date +%s” to get the current Unix time.
Unix time is useful when you are writing a program that needs to calculate the time difference between two dates and times, as it involves simple addition and subtraction, without having to worry about days, months and years.
The issue with leap seconds is generally taken care of by synchronizing your real time clock with a local or network time source, which itself will have compensated for leap seconds.
The problem with 2038 is likely to be resolved within the next 19 years, I suspect just shifting to a 64-bit standard would resolve this for several millennia!
One chip is theDS3231, a highly accurate real time clock that uses an I2C interface. This chip has its own internal crystal oscillator, so no external crystal is required.
Another very popular chip is theDS1307. Like the DS3231 it also communicates using the I2C bus, however, this chip requires an external timing crystal.
As the Tiny RTC uses the I2C bus it is very easy to hook it up to an Arduino. The I2C bus provides power, clock and data signals, so only four connections are required.
Before you hook up your real time clock module it would be a good idea to install the coin cell battery, if you haven’t already. That way you will ensure that the device will keep time even after you power down the Arduino.
Some Arduino clones also have separateSDAandSCLpins, usually located on the same side as the digital I/O pins above theAREFpin. You can use these connections instead of the analog pins if you wish, they are actually just duplicates.
There are several libraries available that will work with the Tiny RTC. I’m going to use two libraries that were contributed by Paul Stoffregen, a well-known figure in the Arduino community.
The Arduino Library Manager can be also used to install two updated versions of these libraries, these were updated by Michael Margolis. You can find them as follows:
Once you have the libraries installed open your Arduino IDE and select theFilemenu item and selectExamples. A sub-menu will appear, scroll down until you get to theExamples from Custom Librariessection.
As you might have guessed from its name theSetTimesketch sets the time on the Tiny RTC module. You’ll need to run this, or something similar, before you can use the clock.
SetTime uses two functions, getTime and getDate, to retrieve the time and date respectively from your computer clock. As most Internet-connected computers synchronize to a network time protocol (NTP) server this will probably be very accurate.
Without using a data structure the time will be reported in Unix time, which I described earlier. ThetmElements_tdata structure breaks this down into elements like seconds, minutes, hours, days, months and years.
Otherwise, the sketch is fairly straightforward. It gets the current system time from the computer running the IDE and writes it to the DS1307 chip on the Tiny RTC module. It does all of this in the Setup section so there is no code in the loop.
Al that is left is to read those time values and print them to the serial monitor. A function calledprint2digitsis used for the hours, minutes and seconds to format the display nicely, with leading zeros for numbers below 10.
The two sketches we have just looked at illustrate how to set and read the time from the Tiny RTC module, and they accomplish this very well. But the module has an additional function, the ability to output a square wave.
You can use this square wave as a timing source for another circuit. It could be used to drive a stepper motor to create an analog clock. And, as I will show you here, it can be used to generate an interrupt for your Arduino.
To use the square wave output as an interrupt for your Arduino you will need to connect the SQ output on the Tiny RTC module to one of the interrupt pins on the Arduino.
An LED is also connected to the Arduino to indicate when an interrupt is being serviced. This is actually optional as it is connected to pin 13 and the Uno also has a built-in LED connected to that pin. If you wish you can leave it out and just monitor the LED on your Uno board.
I found a great sketch that shows how to use the square wave output, it was originally published by Robert Ulbricht on theArduino Slovakia website. Fortunately, there is an English translation available.
The functionsetSQWis really where the “action” is. This function writes to the control register in the DS1307 and sets the square wave frequency to 1Hz.
ThehandleIntfunction is the interrupt handler. Every time a pulse is received on the D2 pin it will be called. This is set up in the Setup function using theArduino attachInterrupt function.
The previous sketch illustrated how to use the SQ square wave output from the Tiny RTC module as an interrupt. And while it does a great job of displaying interrupt operation it really doesn’t have many practical uses.
After all, there are many simpler methods of blinking an LED. In fact, if you really wanted to use the Tiny RTC to blink an LED you could just attach it directly to the SQ output, eliminating the Arduino entirely (although you’d need the Arduino to set the SQ output to 1Hz first).
Take another look at theReadTestsketch from the DS1307RTC examples. You’ll notice that it reads the time and then adds two delays that total exactly one second. It then does it all over again.
If you’re just building a clock this will work well, as every second you’ll read the time and it will have advanced one second. But what if you want to do something else in the Loop after you read and display the time?
If the ”something else” takes less than a second then you’ll be displaying the same time more than once. On a display like an LCD or OLED this is not such a bad thing as you might never notice it, but on the serial monitor it will stand out.
If the “something else” takes more than one second you’ll miss reading the clock and it wil skip one or more seconds. You can alleviate this problem somewhat by simply not displaying seconds but still, your minutes may not change at precisely the right time.
To illustrate how to take advantage of interrupts to solve the timing problem I’m going to build a temperature and humidity meter that can also tell time. I’m going to stick to the serial monitor for my display, but you could easily modify the code to use an OLED or LCD display.
If you wish you could modify the sketch to use the DHT22 or DHT11, I used the AM2320 because I was already using I2C for the real time clock and because I had one handy!
No matter which sensor you choose for your design you’ll encounter the same dilemma – these temperature and humidity sensors require at least two seconds between readings to stabilize. And so if you read it in the Loop you’ll get an erratic display.
The latter library is not called directly in the sketch, instead, it is used by the AM2320 Library. Without it installed your sketch will fail to compile.
TheprintCurrentTimefunction prints the time, date, temperature and humidity to the serial monitor. It takes the temperature and humidity as an input and reads the real time clock, it then writes everything to the serial monitor.
By doing this we only read the temperature and humidity sensor every 10 seconds, which gives it plenty of time to stabilize. You can reduce this number if you wish, but don’t go below two seconds.
We then check to see if the tick count is the same as it was before. If it is then we don’t do anything. If it isn’t then it means a second has elapsed, so we callprintCurrentTimeto read the time and write everything to the serial monitor.
Load the sketch and give it a test. Note that since the temperature and humidity are only updated every 10 seconds it won’t immediately respond to a change in these values. In normal situations this shouldn’t really be an issue.
Adding a real time clock to an Arduino makes it possible to build devices that are aware of the current time and date. This can allow you to create fancy timers and delay circuits, or just build a really cool and unique digital clock.
A Real Time Clock can be added to your Arduino project in order to tell the time. Today I will show you how to use the Tiny RTC, a real time clock based upon the popular DS1307 chip.
DS3231 RTC is a Precise Real-Time Clock Module with a 32Kbit EEPROM and a built-in 10-bit temperature sensor having a resolution of 0.25C. The DS3231 RTC module Precise Real-Time Clock Module is a low-cost, extremely accurate I²C 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 the 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 Arduino is available in commercial and industrial temperature ranges and is offered in a 16-pin, 300-mil SO package.
The datasheet for the DS3231 explains that this part is an “Extremely Accurate I²C-Integrated RTC/TCXO/Crystal”. And, hey, it does exactly what it says on the tin! This Real Time Clock (RTC) is the most precise you can get in a small, low power package.
Most RTC’s use an external 32kHz timing crystal that is used to keep time with low current draw. And that’s all well and good, but those crystals have slight drift, particularly when the temperature changes (the temperature changes the oscillation frequency very very very slightly but it does add up!) This RTC is in a beefy package because the crystal is inside the chip! And right next to the integrated crystal is a temperature sensor. That sensor compensates for the frequency changes by adding or removing clock ticks so that the timekeeping stays on schedule.
This is the finest RTC you can get, and now we have it in a compact, breadboard-friendly breakout. With a coin cell plugged into the back, you can get years of precision timekeeping, even when main power is lost. Great for data logging and clocks, or anything where you need to really know the time.
Vcc – this is the power pin. Since the RTC can be powered from 2.3V to 5.5V power, you do not need a regulator or level shifter for 3.3V or 5V logic/power. To power the board, give it the same power as the logic level of your microcontroller – e.g. for a 5V micro like Arduino, use 5V.
Along with keeping track of the time and date, this modules also have a small EEPROM, an alarm function and the ability to generate a square-wave of various frequencies.
This modules use the I2C bus, which makes connection very easy. First you will need to identify which pins on your Arduino or compatible boards are used for the I2C bus – these will be knows as SDA (or data) and SCL (or clock). On Arduino Uno or compatible boards, these pins are A4 and A5 for data and clock:
Connecting this module is easy as header pins are installed on the board at the factory. You can simply run jumper wires again from SCL and SDA to the Arduino and again from the module’s Vcc and GND pins to your board’s 5V or 3.3.V and GND. However these are duplicated on the other side for soldering your own wires.
The function setDS3231time() is used to set the clock. Using it is very easy, simple insert the values from year down to second, and the RTC will start from that time. For example if you want to set the following date and time – Wednesday November 26, 2014 and 9:42 pm and 30 seconds – you would use:
Note that the time is set using 24-hour time, and the fourth parameter is the “day of week”. This falls between 1 and 7 which is Sunday to Saturday respectively. These parameters are byte values if you are substituting your own variables.
Once you have run the function once it’s wise to prefix it with // and upload your code again, so it will not reset the time once the power has been cycled or micrcontroller reset.
Reading the time form your RTC Is just as simple, in fact the process can be followed neatly inside the function displayTime(). You will need to define seven byte variables to store the data from the RTC, and these are then inserted in the function readDS3231time().
Then you can use the variables as you see fit, from sending the time and date to the serial monitor as the example sketch does – to converting the data into a suitable form for all sorts of output devices.
Just to check everything is working, enter the appropriate time and date into the demonstration sketch, upload it, comment out the setDS3231time() function and upload it again. Then open the serial monitor, and you should be provided with a running display of the current time and date, for example:
From this point you now have the software tools to set data to and retrieve it from your real-time clock module, and we hope you have an understanding of how to use these inexpensive and highly accurate timing modules.
About: Maker 101; Beginner and intermediate level Maker projects! You can find projects such as "How to" and "DIY" on programmable boards such as Arduino, ESP8266, ESP32 and Raspberry Pi on this channel. The projects…