arduino with a ds1307 rtc and lcd display free sample

Hi, and welcome to this tutorial, it’s about another RTC (Real Time Clock) module, it’s the DS1307, previously I did a tutorial about the DS1302, and a project where I set it up using a keypad, then an Alarm Clock project based on that module, I also did a tutorial about the DS3132.
But today we’re about the DS1307, and I’m gonna use it with Arduino UNO board and I’ll also use a LCD i²c screen and OLED display, to show time and date in different formats.
“The DS1307 serial real-time clock (RTC) is a lowpower, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially through an I2C, bidirectional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year.”
OLED libraries: – Download here Adafruit OLED SSD1306 library – Download here Adafruit GFX Library
Important !! First time you must wire the module and upload the “SetTime” example, it sets the module to the compiling time of the code which is technically your real time and date.
N.B: If you are testing the module with multiple things it’s better to close the “SetTime” example, open it again and upload to the board, otherwise your module will be programmed with the first time the “SetTime” example was compiled, and you may think that your module doesn’t work well !!

In the Arduino Real Time Clock Tutorial, we will learn about Real Time Clock (RTC) and how Arduino and Real Time Clock IC DS1307 are interfaced as a time keeping device. If you recall, we have already implemented an Arduino Alarm Clock using RTC DS1307 in an earlier project.
But that project didn’t cover the basics of Real Time Clock or RTC, the specifications of DS1307 RTC IC and how to interface a Real Time Clock like DS1307 or DS3231 with Arduino.
An RTC or Real Time Clock is a Timekeeping device, usually in the form of an Integrated Circuit (IC). An RTC is battery powered and keeps track of the current time even when there is no power.
Real Time Clock ICs are present in computers, servers, many embedded systems and in fact they are used wherever it is required to keep an accurate time.
Even though Arduino and almost all microcontrollers have built-in timers and timekeepers (millis () in case of Arduino), they are power dependent i.e. they run as long as there is power supply. Once the power is turned off (manually or due to power outage), all the timers are reset to 0.
While timekeeping using internal timers is acceptable for simple projects, we need an alternative in projects like data loggers, clocks, alarms, etc. where the timer runs independently irrespective of the external power or if the Microcontroller (or Arduino) is reprogrammed.
Here comes the use of Real Time Clock ICs. Almost all RTC ICs are low-current devices that run for years on a single lithium cell (usually CR2032). One of the popular and most commonly used RTC ICs is the DS1307 Real Time Clock.
The DS1307 RTC is a low cost, low power real time clock IC that can maintain full clock and calendar i.e. hours, minutes, seconds as well as year, month and day. Some of the well-known features of the popular DS1307 RTC are mentioned below.
DS1307 RTC is available as modules, which consists of all the necessary components like Battery, connectors, pull-up resistors and crystal. One such module is used in this project and is shown below.
The following image shows the pin diagram of the DS1307 RTC IC. In order to reduce the power consumption, the number of pins on the IC has to be reduced. Hence, DS1307 RTC used I2C Communication.
X1 and X2: These are pins for connecting the crystal of frequency 32.768 KHz to enable the internal oscillator. If an external oscillator is connected to X1, then X2 can be left floating.
Now that we have seen a little bit about the Real Time Clock IC DS1307, we will proceed with the interface of Arduino and Real Time Clock. As mentioned earlier, the DS1307 RTC Module uses I2C Communication.
In the Arduino Real Time Clock I2C interface, the Arduino Microcontroller always acts as Master and the DS1307 acts as Slave. The Master in I2C Communication i.e. Arduino in this case, is responsible for clock signal, bus access, start and stop signals.
The following image shows the circuit diagram of the Arduino Real Time Clock DS1307 Interface. This circuit explains just the basic connections with respect to a DS1307 Module (a board that contains the DS1307 IC along with the crystal, Battery and pullup resistors).
In order to understand better about the DS1307 RTC Module, the following image will help you as it contains the circuit of a typical DS1307 Real Time Clock Module.
The design of the Arduino RTC Interface is quite straight forward. Connect the SDA and SCL pins of the DS1307 RTC to the SDA and SCL pins of Arduino i.e. pins A4 and A5.
A simple project where Arduino UNO is interfaced with DS1307 Real Time Clock is implemented here. In this project, we will be programming the DS1307 RTC with current date and time and see whether it actually keeps that data even if the power supply to Arduino is removed.
A special library called “RTClib” is used in the programming and it can be downloaded from this link. Make sure that it is downloaded first and added to the Arduino library database.
In order to upload the data and time into the DS1307 RTC IC, we have used a feature available in the RTClib library, where the Arduino will upload the date and time from the computer while uploading the code.

Arduino itself has some time-related functions such as millis(), micros(). However, they can not provide the date and time (seconds, minutes, hours, day, date, month, and year). To get date and time, we needs to use a Real-Time Clock (RTC) module such as DS3231, DS1370. DS3231 Module has higher precision than DS1370. See DS3231 vs DS1307

Hey geeks, welcome back to Techatronic. We are sure that you all have seen analog clocks near you all which tells us time but from these clocks, a machine cant read the time.
For reading the time a machine required a clock that generates output data in machine language. In this article, we are going to discuss the working of a DS1307 RTC module with Arduino.
Basically, Arduino is a microcontroller development board that operates using ATmega328p ic. You can check more similar projects on Arduino and also check out our E-book on Arduino at once
which contains 10+ projects with well-label diagrams. We have also made a Digital clock with an LCD display and a Digital clock with a 4-digits display previously,
NOTE: Pleaseupload the code which is given below to the Arduino. Before uploading you have to install
If you are facing any errors during making this please inform us in the comments section below. Also, check more tutorials on Arduino and Raspberry Pi written by us.

This post is about how to use the DS1307 Real Time Clock (RTC) module with the Arduino. You can also follow this guide for other similar modules like the DS3231 RTC.
As you can see in the picture above, the module has a backup battery installed. This allows the module to retain the time, even when it’s not being powered up by the Arduino. This way, every time you turn on and off your module, the time doesn’t reset.
The parameters for the function are highlighted in red: seconds, minutes, hours, day of the week, date, month and year (in this order). Sunday is the day 1 of the week and Saturday is 7. Don’t forget to uncomment that line of code.
This is a very important step to set up the time in your RTC. If you don’t do this, everytime your RTC resets, it will display the time that you’ve set up previously and not the current time.

I hate to say it but PaulS is correct, albeit he can be somewhat brash with his comments but it is a valuable lesson to learn as a new comer to any forum. Have you posted any code that someone else can copy and paste to get their project up and running? Have you spent years developing a library for anyone to use for free (Andrew Rapp)? Do you have a blog with all of your problems, pitfalls, and actively working code that has taken you the last 5 years to create, out there for anyone to copy and paste (draythomp)? If you cannot take 5 minutes to do a simple google search of something, say "sprintf()" to learn the basics of a subject you are having a problem with, then why should an experianced programmer like PaulS, Nick Gammon, grumpy Mike, AWOL, draythomp, rappa, etc spend 5 minutes of their time to help you out. You should know the problem subject well enough to properly communicate the issue you have along with the deficiencies in what you are trying to accomplish.
Also if you are not contributing anything to the community you shouldn"t expect anything from it, this is why I seldom post. I"m trying to change my behavior and become more active. Basically try to give as much as you take, ask for help not for someone that"s better to do the work for you so you can take the credit. And for god"s sake be concise and complete! if you don"t post the code, the whole code and nothing but the code, how is someone going to try to compile it and possibly modify your code to HELP YOU fix the errors!? Most important: all of the best programmers (most helpful, knowledgeable) on this forum keep track of who you are and if you get sh*tty with them right off the bat you"ll never get any help from them EVER!
Look through the forum. All of the "what"s going on with my crazy nonsense!" "help me please ASAP!" "Getting error blah" chaff that you have to sift through is way less helpful than
You can"t possibly have researched sprintf() in the time between my post and your reply. This is not the "do my homework for me" hotline. You are expected to make some attempt to actually learn how to do things with the Arduino beyond borrowing other people"s code.
Just to help you out, I think this quote translates into "look it up yourself it"s pretty self explanatory." Don"t know what %d or %s mean? Google %d C++ and %s C++ maybe take a look at printf() and/or studio.h. Try asking some specific question like. I don"t fully understand the part "...a format specifier in the format string (or a pointer to a storage location, for n)..." or what it exactly means, could someone please give me a simple example or break it down for me? http://www.cplusplus.com/reference/cstdio/sprintf/ I did a little search and it took about 15 minutes and I didn"t know jack about sprintf() before now which is why I"m on this banter filled thread in the first place. BTW I think sprintf() is a way to print out strings reusing a defined buffer saving SRAM from using a normal .print(" ") function and/or also flash from using a .print(F(" ")) function. Seems really useful when using an ATtiny 45 or something with little SRAM or flash.

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

The RTC module is based on the clock chip DS1307, which supports the I2C protocol. It utilizes a Lithium cell battery (CR1225). The clock/calendar provides seconds, minutes, hours, day, date, month, and year. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap years. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. And it is valid up to 2100. In order to gain a robust performance, you must put a 3-Volt CR1225 lithium cell in the battery-holder. If you use the primary power only, the module may not work normally, because the crystal may not oscillate.
The platforms mentioned above as supported is/are an indication of the module"s software or theoritical compatibility. We only provide software library or code examples for Arduino platform in most cases. It is not possible to provide software library / demo code for all possible MCU platforms. Hence, users have to write their own software library.
In order to gain a robust performance, you must put a 3-Volt CR1225 lithium cell in the battery-holder. If you use the primary power only, the module may not work normally, because the crystal may not oscillate.

This example describes how to use the datetime and ds-rtc libraries, format date and time values, and work with real-time clock (RTC) modules for Arduino. Upon the completion of this guide, you will learn how to create a simple digital clock based on a DS1307 I2C RTC module and I2C LCD display.
Both the I2C display and the real-time clock board communicate via I2C. To ease the connection, you can create the I2C bus on the breadboard by wiring the SDL and SDA I2C pins from the Arduino board to the contact lines on the breadboard. The display and RTC have different I2C addresses, they do not interfere with each other, so you can connect them to the same I2C bus.
Plug the I2C display. Wire its SDA and SDL with the I2C bus on the breadboard. Wire its GND and Vo pins with the GND line on the breadboard. Wire Vcc pin with the 5V line on the breadboard.
Plug the I2C RTC breakout board. Wire its SDA and SDL with the I2C bus on the breadboard. Wire its V and GND pins with the corresponding lines on the breadboard.
Put an rtc-device node from the xod-dev/ds-rtc library onto the patch. This node describes an RTC breakout board which communicates via I2C. By default, the I2C device address of the RTC at the ADDR pin is 68h which is correct for the most DS1307 modules. The DEV pin of the node outputs the rtc-device value which contains everything to operate the device.
Add a write node from the xod-dev/ds-rtc library onto the patch. This node’s function is to write time and date to the permanent memory of the RTC board. The DT pin inputs a datetime type value to be written. The UPD pin triggers a new write. The DONE output pin notifies writing complete.
The write node requires the date and time values having a datetime type. The datetime node is used to pack year, month, day, and hours, minutes, seconds into a datetime type value.
For example, we set the current date. Now it is the 17-th of September of the 2018 year, and the time is 14 hours 44 minutes and about 31 seconds. To set this date and time values to the RTC, we fill the datetime pins with values.
The patch to set up the real-time clock is completed. Let’s quickly load it into the Arduino board, to make the difference between the real and written time be only a few seconds.
Once the board is flashed the RTC module will get the correct current time value which automatically ticks and not get lost as long as the module is powered by either its battery or the device itself. However, note that the time gets re-written on every boot to that exact value we have set. So, if you will reset your board after an hour, it will overwrite the correct time with the now-obsolete hour-lagging value. To protect from this effect it is a good idea to remove the uploaded time setting program. Upload an empty patch right after set-current-time to clear.
Create a new patch and name it digital-clock. This is the patch for our digital clock. Strictly speaking, we are going to create a new program. So we need to put an RTC node again. To fetch the data, a node which is named simply rtc might be used. Then we unpack the obtained values for further work.
Add the rtc node onto the patch. This node represents an RTC clock. A pulse at the UPD pin triggers a new reading of a datetime type value. The DONE output pin fires on successful read. By default, the I2C device address at the ADDR pin is 68h.
Add the unpack-datetime node onto the patch and link it with the rtc node. The unpack-datetime node destructs the datetime type value and outputs Number values of the date and time for subsequent processing or formatting.
If the real-time clock takes a small place in your extensive project, and a controller is very loaded, the Continuous pulses on time readings can lead to various unexpected read failures. To solve this problem limit the pulse frequency. For example, you can link the UPD pin with the clock node and set the IVAL value to 1 second. By this, you adjust the real-time clock updates to 1 Hz.
Let’s put this patch aside for a while. Imagine that no time has passed since we written values to the RTC. Thus, if we look at output pins of the unpack-datetime node, the values of date and time are the same format that we entered.
Besides, the unpack-datetime node calculates the weekday and outputs it through the WD pin in the range from 1 to 7. Now, we need to format this numbers to a proper view and add punctuation before showing them at the display screen.
Some time formats are standardized though. For example, you can read about ISO 8601 or Unix time formats. You can check out the xod/datetime library for the format-timestamp node. It formats datetime for use in spreadsheet computer programms. In this example, let’s format date and time as they are shown on the digital clock in the classic American style. Here is the picture of the digital clock we want to make.
We start the formatting process from the date. Create a patch for the first new patch node and name it format-date. We want this node to get numbers for a year, month, and day and output them as a single string. Also, we want format-date to separate the date values with the / character.
Add the format-number node and link it with the YEAR node. The year values that comes to the YEAR pin are of Number type. The Number type values are floating point, and the incoming year value looks like 2018.00. We use the format-number node to get rid of those zeroes. If you put the 0 value to the DIG pin string value of the year becomes 2018.
Add two pad-with-zeroes nodes from the xod/core onto the patch. Link their input pins with MON and DAY. The pad-with-zeroes node transforms a Number value into String adding zeroes before the value. The number of added zeroes depends on the width of the string. The width value is set at the W input pin. This node is very useful for this guide. The month and day value fields on our display have 2 digit capacity. If we show the 12/22/2018 date on the screen, it is ok. However, if we show, for example the 3rd of September directly without adding zeroes below the month and day, the text on the screen looks will be 3/9/2018. Thus cuts the width of the date by two digits, and the total width of the line on the display screen. The pad-with-zeroes solves this problem. We put the 2 width value to the W pin on both nodes, so the 3rd of September looks like 03/09/2018. By this, the total width of the line with the date will always be static.
Add the join node onto the patch. With the join we can separate day, month and year by a character of our choice. As we want to separate date values with /, we link the / string constant with the D delimiter pin. With join we can also set the order of the year, month, and day representation. As we want the MON value to be first, DAY to be second, and YEAR to be third, we make proper links between join input pins and other nodes. After this link the join output to the DATE output-string node.
The unpack-datetime node outputs the number of the weekday depending on the date. To show weekday names instead of numbers we need a node. Create a patch for this node and name it format-weekday.
Add the nth-input node onto the patch. The format-weekday node outputs the number of weekdays in the range from 1 to 7. So, the Monday number is 1, and the Sunday number is 7. The nth-input node is great for this example, it outputs the selected value from X pins based on the IDX index value. Let the input node IN be the index.
Ok, we created nodes that format date and weekday. Time is left to format. Create a patch for the new node and name it format-time. This node will take hours, minutes and seconds from the unpack-datetime node and format them as we desire.
Put three pad-with-zeroes nodes onto the patch and link them with HOUR, MIN, and SEC nodes. To display hours, minutes, and seconds in a two-digit format, put the2value to theWpins as we do it in theformat-date node.
To separate the time values with a colon use the join node. Put the : delimiter into the D pin of the join node. Also, you can use the join to set the order in which the time is displayed. Make proper links between the pad-with-zeroes outputs and the join inputs.
For the American style clock, we decide to use the 12-hour format instead of 24-hour. For the part of the day, we show the AM and PM words on the display screen.
Insert the am-pm node between the HOUR input node and the pad-with-zeroes node. The am-pm changes the hour format from 24 to 12. The AM pin is true if it is “before midday” or false if it is not.
Add the if-else node onto the patch and set the AM string value for T, and PM string value for F. Link the COND pin with the AM output of the am-pm node.
Separate the time from the PM and AM labels with a single space. For this, put another join node onto the patch. Put the spacer character to its delimiter D pin.
When the date, time, and weekday are formatted we can show them on the I2C display screen. Let’s show the date and the weekday at the first line of the I2C display, and time at the second line.
The screen of the display in this example has 16x2 digits capacity. We align both display lines to the center to make the clock looks better. For this, we put two concat nodes for each line. There we added two spaces to indent the text from the edge of the screen and one space to separate the day of the week from the date.
We created a digital clock device using xod/datetime and xod/ds-rtc libraries. Try to make your own clock with different styles and timestamp formats.
If you want to practice, find other usages of these libraries. For example, connect a real-time clock to an SD card, log timestamp values using the format-timestamp node, and have a look how it is easy to process timestamps with the computer applications.

I was trying to use your code to display the same date & time as my monitor on my LCD 16x2. It mostly works, but instead of displaying 14:05, maybe 07:05, it respectively displays 14:5 & 7:5 or 70:5. How to solve this?
Hi Sharon, Thanks for your comment. This is a basic circuit and the Alarm functionality is not available for the moment. I am planning to build a complete one later. For now please see this link [https://goo.gl/9p15MV] I think this link has an example code for you.
Hi I don"t know what I am doing wrong but I get this error when I compile the code in the Arduino IDE: "RTClib.h:5: error: redefinition of "RTC_DS1307 RTC""

Real time clocks are required in applications where we need to keep track of the current time for example in data logging. In this tutorial I will look at the DS1307 RTC which i...

Sometimes it may be necessary to use a display while making a hardware project, but the size and the type of the display may vary according to the application. In a previous project, we used a 0.96″ I2C OLED display, and in this project we will have an I2C 20×4 character display.
This liquid crystal display has 4 lines, 20 character in each line and cannot be used to display graphics. The main feature of this display that it uses I2C interface, which means that you will need only two wires to connect with Arduino. At the back side of the screen there is a small PCB soldered in the display, this circuit is a serial LCD 20 x 4 module and it also has a small trimpot to adjust the contrast of the LCD.
Display’s backlight is blue and the text is white. It is fully compatible with Arduino and has 5V input voltage. Its I2C address could be 0x27 or 0x3F. You can get it for about $7 from Bangood store.
DS3231 is a low-cost, accurate I2C real-time clock (RTC), with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, so that if power is disconnected it maintains accurate time.
RTC maintains seconds, minutes, hours, day, date, month, and year information. Less than 31 days of the month, the end date will be automatically adjusted, including corrections for leap year. The clock operates in either the 24 hours or band / AM / PM indication of the 12-hour format. Provides two configurable alarm clock and a calendar can be set to a square wave output. Address and data are transferred serially through an I2C bidirectional bus.
This RTC module operates at input voltage range between 3.3V and 5.5V, so it can be connected with 3.3V or 5V pins. It is available on Banggood store for about $2.
First we need to download the library of the display, which includes all required functions to configure and write on the display. You can find it here.
Unzip the library and add it to the Arduino libraries folder, then run Arduino IDE and copy the following code. The first two lines are to include both of I2C and LCD libraries.
lcd.setCursor(3,0) will set the cursor of the LCD in the specified location, the first argument for the column and the second for the row starting form 0.
Here we will use a small breadboard to connect the RTC module and display with the Arduino’s I2C pins (A4 and A5). The SCL pins are connected with analog 5 pin and the SDA pins with analog 6 pin. The top rail of the breadboard used as I2C bus and the bottom one is power bus.
In addition to setup and loop function, we will create four other functions to organize the code. As the corners and vertical lines of the frame are special characters, we have to create them manually. So we will use a function to create them and another one to print them on the LCD.
Inside the loop function the time will be read from the real time clock module and the printed to the LCD using a custom function for each of time and date.
At first, we have to include the three libraries, I2C, LCD, and RTC and set the LCD address. Inside the setup function the display is initialized, then we will call createCustomCharacters() function and print them.
Each character can be 5-pixel long in width and 8-pixel in height. So to create a custom character we need to create a new byte. We need 5 characters, the vertical line and the four corners. The yellow pattern shows you how the character will be displayed on the LCD.
Inside createCustomCharacters() function, we called lcd.createChar(#, byte array) function. The LCD supports up to 8 custom characters numbered from 0 to 7. It will assign the index in the first argument to the character given by the byte array. To print this character we can use lcd.write(byte(#)) function.
This function is very simple, it uses lcd.setCursor(#,#) to move the cursor and lcd.print(“”) to print the given string. The function will print the top and bottom horizontal lines, then printing other custom characters.
As we discussed earlier, the loop function will get the current time and date every second and refresh them on the display. First we defined a time element “tm” which has current time data, then if the time is correct and the RTC module working fine the time and date will be printed.
We can add some instructions so, if the DS1307 is stopped or there is a circuit error,we can light a LED to indicate the problem. The loop will wait for 1 second before starting the next iteration.
PrintTime function uses three arguments, the column and line where it will print the time, and the time element. lcd.print(tm.Hour) will print the hour, then if the minutes and seconds are less than 10 we will add 0 to the left. And the same method is used to print the date.
Now everything is ready, upload the code to your Arduino and enjoy watching your new clock. You can find the full Arduino sketches and libraries in the attachment below.

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.
The board contains the DS1307 chip and all the support electronics, including the timing crystal. It also has a battery holder for a coin cell, this sits underneath the board.
If you order your Tiny RTC online from an overseas source it likely won’t have the battery installed, this is due to international shipping regulations regarding lithium batteries. The module uses a widely available coin cell battery.
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.
We start by including the Wire library, which is the built-in library that facilitates communications using I2C. We then include the two libraries we installed earlier.
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.
These frequencies are selected by writing to an internal control register in the DS1307. By default, the device is programmed at the factory for a 32 KHz frequency.
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.
The AM2320 is an I2C temperature and humidity sensor. Physically it looks identical to a DHT11 or DHT22, the difference is that it uses the I2C bus to send data to its host.
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.
You’ll need another couple of libraries to run this sketch to handle the temperature and humidity sensor. Both can be installed using the Library Manager.
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.
Next, we create a couple of variables that count “ticks”. I’m defining a “tick” as a one second period. Theold_tick_valuevariable will be used to check if the tick value has changed since we last used it.
In the Setup routine we attach the interrupt to the interrupt handler, as we did earlier. I kept the same name for the interrupt handler but have changed its function.
Next we initialize the temperature and humidity sensor, delay for two seconds and then read the temperature and humidity values. This is so we already have a first reading before we enter the loop.
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.
In the Loop we first check the number of ticks by reading the tick count. If it is 10 then we read the humidity and temperature, then reset the tick counter to zero.
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.

This Arduino tutorial shows how to build a simple real time clock & calendar (RTCC) using the UNO board and DS1307 RTC integrated circuit where time and date are printed on ST7789 TFT display module.
The ST7789 TFT module contains a display controller with the same name: ST7789. It’s a color display that uses SPI interface protocol and requires 3, 4 or 5 control pins, it’s low cost and easy to use. This display is an IPS display, it comes in different sizes (1.3″, 1.54″ …) but all of them should have the same resolution of 240×240 pixel, this means it has 57600 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant).
The ST7789 display module shown in project circuit diagram has 7 pins: (from right to left): GND (ground), VCC, SCL (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and BLK (back light).
As mentioned above, the ST7789 TFT display controller works with 3.3V only (power supply and control lines). The display module is supplied with 3.3V (between VCC and GND) which comes from the Arduino board.
To connect the Arduino to the display module, I used voltage divider for each line which means there are 4 voltage dividers. Each voltage divider consists of 2.2k and 3.3k resistors, this drops the 5V into 3V which is sufficient.
The DS1307 RTC SDA (serial data) and SCL (serial clock) pins are respectively connected to Arduino A4 and A5 pins (ATmega328P hardware I2C module pins).
The first library is a driver for the ST7789 TFT display which can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries …, in the search box write “st7789” and install the one from Adafruit).
The 3rd library is for the DS1307 RTC, it may be installed using library manager (in the search box write “rtclib” and install the one from Adafruit).
The ST7789 TFT module pins (CS, RST and DC) connections are defined as shown below (even the display module has no CS pin but its definition is required by the Adafruit ST7789 library):
The other display pins (SDA and SCL) are connected to Arduino hardware SPI module pins (digital pin 11 and digital pin 13 respectively for MOSI and SCLK).

In this project I have created a digital clock. we can watch the current date and time on the lcd display this project uses DS1307 based RTC module to to maintain the time and date value.
this module is build around DS1307 chip which is a real time clock that means we can get hour, second, minutes, day, date, month and year value fromitthis module require external power source so that it can power the module during power failure so if we remove the power supply of our clock it would not lost its time for this we uses a 3V li-ion battery this module support I2C (two wire interface) to talk to master device Like Arduino in this case once we store the current time value of second hour minute day date month and year into their corresponding registers and and then ds1307 maintains the time and updates the register values to know more about ds1307 you can have a look at he datasheet ds1307 stores the data in BCD(binary coded decimal) format
to display time and date I have used used 16*2 lcd because it is easy to control it by Arduino in comparison to 7 segment display we can interface with lcd by the help of LiquidCrystal library of arduino
these resistors are necessary for I2C interface because I2C devices have open collector configuration these pullup resistors pull the SDA and SCL line high

A smart relay for the Arduino that allows programming of 10 time based alarms and one voltage based alarm (for overvoltage/ under voltage protection). Also uses DS1307 RTC to keep time, and allows the user to reset the time if the RTC loses time.
Ms.Josey
Ms.Josey