Introduction
In this tutorial, we will work with the RTC (Real Time Clock) Module DS1307 RTC Module With Arduino. we will be able to know the exact time, day month year, and even the day of the week, in turn, thanks to an EEPROM that the module brings we can save the data without clear when you turn off the Arduino.
Thank You NextPCB:
This project is successfully finished because of the service and support from NextPCB. NextPCB is one of the most professional PCB manufacturers in Global and has specialized in the PCB and assembly industry for over 16 years. Not only could NextPCB deliver the most creative printed circuit boards and assembly technologies in the most elevated quality standards, but the fastest delivery turnaround was also as fast as 24 hours.
Guys if you have a PCB project, please visit their website and get exciting discounts and coupons. if you sign-up using this link you will get free coupons worth $100. Checkout out NextPCB now and get amazing discounts on your first order.
- Only 0$ for 5-10pcs PCB Prototypes:https://www.nextpcb.com/PCBPrototypes
- 4-layer PCB price reduction up to 40%: nextpcb.com/40%off
- Register and get $100 from NextPCB: Nextpcb.com/coupon
What is a DS1307 module?
The DS1307 from Dallas Semiconductor (Maxim) is a device known as a “Real Time Clock” ( RTC ) that operates through the I2C bus and that, in addition to giving us the time with minutes and seconds, has a calendar that includes leap years until the end of the century, that is, until the year 2100.
The module we will be using is the low-cost DS1307 RTC module with the AT24C32 I2C EEPROM from ATMEL. Both integrated circuits share the same communication bus with the I2C Protocol. AT24C32 EEPROM memory allows you to store 32Kbits (4K Bytes) of data permanently.
On the back, it has a socket to insert a 3V battery, which allows the RTC to keep the time and date in operation when the Arduino is turned off or the main power is removed. This battery, depending on the brand and the time it is off, can last from 3 to 10 years approximately.
DS1307 RTC functions:
- Time: Seconds, minutes, hours
- Date: day number, day of the week, month, and year
- Leap year compensation
- Output pin at 1 Hz to carry timing on other components
DS1307 RTC Module Spec
- Supply voltage: 3.3V – 5V DC
- Main chips: DS1307 and AT24C32
- Digital communication interface: I2C
- DS1307 I2C address: Read(11010001) Write(11010000)
- AT24C32 EEPROM memory (4K * 8bit = 32Kbit = 4KByte)
- Can be used in cascade with another I2C device, the address of AT24C32 can be modified (default is 0x57)
- The battery can keep the RTC working for 10 years.
- Dimensions: 28*27mm
- Weight: 5 grams
DS1307 RTC Pinout
This module communicates with the Arduino through the I2C bus and a bus that it shares with the DS1307, in this way we only need 2 Arduino pins to control the module. RTC and EEPROM of the Module.
The communication between the Arduino and the RTC Module is through I2C (SDA and SCL pins), It also has a pin (SQ) that sends a pulse every second (1 Hz), which can be used to synchronize other components or to make a counter of seconds.
Connections between DS1307 RTC Module With Arduino
As you can see, the connection is like any I2C connection. the TinyRTC module requires the following connections to the Arduino board. Note that the “DS” pin of the TinyRTC module is not used, it corresponds to the 1-wire temperature sensor that is not factory installed. Only the SDA, SCL lines, and the 5-volt power supply are necessary:
- SCL -A5
- SDA -A4
- VDC – 5V
- GND – GND
Programming And Library
For this example we will use the RTClib library from Adafruit, a library that can be downloaded from here:
https://github.com/adafruit/RTClib
After downloading the library import this library into the Arduino IDE.
Correcting the time and date of DS1307 RTC
When we receive a DS1307 module, without a battery, it does not have the updated time, every time we remove the battery the DS1307 loses the time, establishing itself as time 0:0:0 and date 00/00/00,
This example is only used to set our DS1307 to the time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include < Wire . h > #include "RTClib.h" RTC_DS1307 rtc ; void setup ( ) { Serial . begin ( 9600 ) ; rtc . begin ( ) ; //Initialize the RTC Serial . println ( "Setting time and date..." ) ; rtc . adjust ( DateTime ( F ( __DATE__ ) , F ( __TIME__ ) ) ) ; Serial . println ("DS1307 updated with the time and date this program was compiled:" ) ; Serial . print ( "Date = " ) ; Serial . print ( __DATE__ ) ; Serial . print ( " Time = " ) ; Serial . println ( __TIME__ ) ; } void loop ( ) { } |
If you want to update at an exact time and date, you must change the following line of code:
1 |
rtc . adjust ( DateTime ( 2022, 5, 19, 12, 10, 0 ) ) ; //2022,May,19,12h,10min,0sec |
Reading Time and Date from DS1307 RTC Module
In this sample, we will read the seconds, minutes, and hours of the DS1307 RTC module, and in turn, we will also obtain the date.
To perform this sample we use the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include < Wire . h > #include "RTClib.h" RTC_DS1307 rtc ; int second , minute , hour , day , month ; long year ; //variable year DateTime HourDate ; void setup ( ) { Serial . begin ( 9600 ) ; rtc . begin ( ) ; //Initialize the RTC } void loop ( ) { TimeDate = rtc . now ( ) ; // get the current time and date second = TimeDate . second ( ) ; minute = TimeDate . minute ( ) ; time = TimeDate . hour ( ) ; day = TimeDate . day ( ) ; month = TimeDate . month ( ) ; year = TimeDate . year ( ) ; //We send the time and date through the serial port. Serial . print ( "time = " ) ; Serial . print ( time ) ; Serial . print ( ":" ) ; Serial . print ( minute ) ; Serial . print ( ":" ) ; Serial . print ( second ) ; Serial . print ( " Date = " ) ; Serial . print ( day ) ; Serial . print ( "/" ) ; Serial . print ( month ) ; Serial . print ( "/" ) ; Serial . print ( year ) ; Serial . println ( ) ; delay ( 1000 ) ; } |
As you can see, the code is easy to understand.
In the void Setup, we only initialize the RTC and in the void loop() we read the DS1307
1 |
TimeDate = rtc . now ( ) ; // get the current time and date |
Here is the serial monitor for this example:
Automatic Relay on and off
In this sample, we will show the code to turn on a light or any electronics appliance. etc, at a time decided by our program.
For example, to turn on the light every day from 6:00 p.m. to 10:00 p.m., or to turn on a pump to select a watering schedule in a garden, etc.
The example shown below activates relay from Monday to Friday from 6:30 pm to 11:30 pm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include < Wire . h > #include "RTClib.h" RTC_DS1307 rtc ; int second , minute , hour , day , month , dayOfWeek ; DateTime TimeDate ; void setup ( ) { Serial . begin ( 9600 ) ; rtc . begin ( ) ; //We initialize the RTC pinMode ( 2 , OUTPUT ) ; } void loop ( ) { TimeDate = rtc . now ( ) ; // get the current time and date second = TimeDate . second ( ) ; minute = TimeDate . minute ( ) ; time = TimeDate . hour ( ) ; dayOfWeek = HourDate . dayOfTheWeek ( ) ; //0:Sunday, 1:Monday, 2:Tuesday ... float hours = hour + minute / 60.0 ; //condition: >Sunday and <Saturday and >6.30pm and <11.30pm if ( dayOfWeek > 0 && dayOfWeek < 7 && hours > 18.5 && hours < 23.5 ) { digitalWrite ( 2 , HIGH ) ; Serial . print ( "Digital output 13 = ON" ) ; } else { digitalWrite ( 2 , LOW ) ; Serial . print ("Digital output 13 = OFF" ) ; } //We send the time and date through the serial port. Serial . print ( " time = " ) ; Serial . print ( time ) ; Serial . print ( ":" ) ; Serial . print ( minute ) ; Serial . print ( ":" ) ; Serial . print ( second ) ; Serial . print ( " day of the week = " ) ; Serial . print ( TimeDate . dayOfTheWeek ( ) ) ; Serial . println ( ) ; delay ( 1000 ) ; } |
To modify the schedule, you only have to change the condition, note that we convert the minutes to hours to work the condition in a shorter way.
The println() serials are only used for demonstration purposes because it is only to monitor the status: