0

members

Easy Basics: Project 072a DS1307 Real Time Clock (RTC) module

of Acoptex.com in UNO

Basics: Project 072a

Project name: DS1307 Real Time Clock (RTC) module

Tags: Arduino Uno, DS1307 RTC Module, AT24C32 Real Time Clock, RTC, DS1307 Real Time Clock (RTC) module, I2C

Attachments: sketch1, library1; sketch2; sketch3

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1.Arduino Uno R3 (you can also use the other version of Arduino)

2. DS1307 Real Time Clock (RTC) module  1pc

3.Arduino IDE ( you can download it from here  )

4.Jumper cables M-M, F-M

5. Lithium Ion battery LIR2032 3.6 VDC or CR12203VDC or CR2032 3VDC (depends on battery holder and module) 1pc

General

We will learn how to connect DS1307 Real Time Clock (RTC) module to Arduino board and use it.

Understanding the DS1307 Real Time Clock (RTC) module

This is a great battery-backed real time clock (RTC) that allows your microcontroller project to keep track of time even if it is reprogrammed, or if the power is lost. Perfect for datalogging, clock-building, time stamping, timers and alarms, etc. The DS1307 is the most popular RTC, and works best with 5V-based chips such as the Arduino.

What is an RTC? (by Tyler Cooper)

A real time clock is basically just like a watch - it runs on a battery and keeps time for you even when there is a power outage! Using an RTC, you can keep track of long timelines, even if you reprogram your microcontroller or disconnect it from USB or a power plug. Most microcontrollers, including the Arduino, have a built-in timekeeper called millis() and there are also timers built into the chip that can keep track of longer time periods like minutes or days. So why would you want to have a separate RTC chip? Well, the biggest reason is that millis() only keeps track of time since the Arduino was last powered - . That means that when the power is turned on, the millisecond timer is set back to 0. The Arduino doesn't know that it's 'Tuesday' or 'March 8th', all it can tell is 'It's been 14,000 milliseconds since I was last turned on'. OK so what if you wanted to set the time on the Arduino? You'd have to program in the date and time and you could have it count from that point on. But if it lost power, you'd have to reset the time. Much like very cheap alarm clocks: every time they lose power they blink 12:00. While this sort of basic timekeeping is OK for some projects, some projects such as data-loggers, clocks, etc will need to have consistent timekeeping that doesn't reset when the Arduino battery dies or is reprogrammed. Thus, we include a separate RTC! The RTC chip is a specialized chip that just keeps track of time. It can count leap-years and knows how many days are in a month, but it doesn't take care of Daylight Savings Time (because it changes from place to place).

Whenever the RTC chip loses all power (including the backup battery) it will report the time as 0:0:0 and it won't count seconds (its stopped). Whenever you set the time, this will kick start the clock ticking. So basically the upshot here is that you should never ever remove the battery once you've set the time. You shouldn't have to and the battery holder is very snug so unless the board is crushed, the battery wont 'fall out'.

Let's check different versions of modules with DS1307 IC.

I2C RTC DS1307 AT24C32 Real Time Clock Module

 

  • Two wire I2C interface
  • Hour : Minutes : Seconds AM/PM
  • Day Month, Date – Year
  • Leap year compensation
  • Accurate calendar up to year 2100
  • 1Hz output pin
  • 56 Bytes of Non-volatile memory available to user
  • Size: 28x27x8.4mm
  • Lithium Ion battery LIR2032 3.6 VDC
  • Supported with Atmel, PIC, AVR, Arduino, ARM

OPEN SMART DS1307 RTC v1.1 module I2C. The module is based on DS1307 high precision real time clock module; Through I2C interface to communicate with singlechip, can read year, month, day, week, time, minute, second; Car read until 2100 year; Control interface is I2C; Recommended power: DC 5V; The address of the module is 0x68; Control interface level is 5V or 3.3V; 4-M2 screw hole, easy to install.

Specification:

  • Power voltage VCC: 4.5~5.5V;
  • Battery voltage: 2.0~3.5V;
  • High level input: 2.1~VCC + 0.3V;
  • Low level input: -0.3~+0.8V;
  • Real time IC: DS1307Z;
  • Independent timing: through I2C interface communicate with MCU;
  • Battery: CR1220;
  • Installed hole: 4-M2 screw hole, diameter hole: 2.2mm, easy to install
  • Dimensions 1.81 in x 0.94 in x 0.31 in (4.6 cm x 2.4 cm x 0.8 cm)
  • Weight 0.28 oz (8 g)

MY012V1.0 DS1307 Real Time Clock Module (RTC) I2C

This is the Real Time Clock (RTC) Module, this small breakout board that uses the most popular DS1307 to keep track of the current year, month, day as well as the current time. The module comes fully assembled and includes a small  Lithium coin cell battery that will run the RTC for a minimum of 9 years without an external 5V  Power supply. The DS1307 RTC is Accessed via I2C Protocol.

Specification:

  • Two wire I2C interface
  • (hh:mm:ss) Hour : Minutes : Seconds AM/PM (DD/MM/YYYY) Day Month, Date – Year Leap year Compensation
  • Accurate Calendar up to year 2100
  • Battery Backup Included
  • 3 Volt Li(Lithium) Coin Battery CR12203VDC or CR2032 3VDC
  • 1Hz output pin
  • 5 Volt Power Supply
  • 56 Bytes of Non-volatile Memory Available to User
  • Plugs into any breadboard, or You can use wires
  • On board power indication RED LED
  • Four mounting holes 3.0 mm for easy mounting
  • Dimensions: 43 mm x 34.5 mm

Meeeno DS1307 Real-time Clock Brick Module I2C

It is based on the clock chip DS1307; 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; This is low power consumption and with 56 byte NVRAM all BCD code clock / calendar real-time clock chip, site and data is transmitted by two lines two-way serial bus; The chip is built-in power supply induction circuit, with power failure detection and battery switchover function

Specification Real-time clock (RTC) counts seconds, minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100; 56-byte, battery-backed, nonvolatile (NV) RAM for data storage; I2C serial interface (IIC communication); DC 5V supply; Programmable; Square-wave output signal; Automatic power-fail detect and switch circuitry; Consumes Less than 500mA in battery-backup mode with oscillator running; Working temperature: -40'C~80'C; Power supply needs: 5V and 3.3V; Interface type: Digital; Pin Definition: 1-GND 2-VCC 3-SCK 4-SDA 5-INT
Application Arduino teaching, DIY project, singlechip learning and experiment
English Manual/Spec Yes (electronic format)
Other A product for Arduino that works with official Arduino boards
Packing List 1 x Module 1 x CR1220 battery
Dimensions: 1.57 in x 0.94 in x 0.24 in (4.0 cm x 2.4 cm x 0.6 cm)
Weight: 0.18 oz (5 g)

It is based on the clock chip DS1307; 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; This is low power consumption and with 56 byte NVRAM all BCD code clock / calendar real-time clock chip, site and data is transmitted by two lines two-way serial bus; The chip is built-in power supply induction circuit, with power failure detection and battery switchover function.

Specification:

  • Real-time clock (RTC) counts seconds, minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100;
  • 56-byte, battery-backed, nonvolatile (NV) RAM for data storage;
  • I2C serial interface (IIC communication);
  • Programmable; 
  • Square-wave output signal; 
  • Automatic power-fail detect and switch circuitry;
  • Consumes Less than 500mA in battery-backup mode with oscillator running; 
  • Working temperature: -40'C~80'C; 
  • Power supply needs: 5V and 3.3V;
  • Interface type: Digital; 
  • CR1220 battery
  • Dimensions: 1.57 in x 0.94 in x 0.24 in (4.0 cm x 2.4 cm x 0.6 cm)
  • Weight: 0.18 oz (5 g)

Anykits DS1307 Real Time Clock Module I2C

RTC Add-On Board offers an easy way to interface a standard I2C RTC IC type DS1307, into your project.

Specification: 

  • 5 VDC supply sourced through the interfacing Box Header connector.
  • Battery backup for the RTC board available on the PCB
  • Four mounting holes of 3.2 mm each
  • PCB dimensions 50 mm x 33 mm
  • 3V coin battery CR12203VDC or CR2032 3VDC
  • 10 pin box header connector

DS1307 Real Time Clock Mini Board I2C

Need to add a Real Time Clock to your project, then our DS1307 Real Time Clock Mini Board is the ideal solution. This compact, easy to install mini-board uses the standard I2C bus for control and data transfer. 

The time is maintained and updated within the DS1307 IC. Included on-board is also battery back-up, enabling the time to be maintained even with power disconnected from the board. The DS1307 IC stores time values in separate registers, for years, month, day, hour, minutes and seconds. A scratchpad data bank is also available for storing of temporary or alarm values. Refer to the DS1307 datasheet below for more information. 

The new DS1307 Real Time Clock Mini Board is a compact, easy to control Real Time Clock, ideal for use when setting up a real-time control system or for use when date stamping is required for various monitoring applications. 

Features:

  • Includes Dallas Semiconductor DS1307 Real Time Clock
  • On-Board Battery Back-Up
  • Standard I2C Bus Control Connection
  • Easily Add a Real Time Clock to your Project
  • Compact and Easy To Mount Board
  • Ideal for use with our Development and Control Boards
  • Board Dimensions: 45 x 55 mm
  • 3V coin battery CR12203VDC or CR2032 3VDC

Real Time Clock Module DS1307 I2C

This custom designed module uses the DS1307 Real Time Clock to keep track of the current year, month, day as well as the current time. The module comes fully assembled and pre-programmed with the current time (OK, so it's MST current time). The included Lithium coin cell battery (CR1225 41mAh) will run the module for a minimum of 9 years (17 years typical) without external 5V power. The DS1307 is accessed via the I2C protocol.

Features:

  • Two wire I2C interface
  • Hour : Minutes : Seconds AM/PM
  • Day Month, Date - Year
  • Leap year compensation
  • Accurate calendar up to year 2100
  • Battery backup included
  • 1Hz output pin
  • 56 Bytes of Non-volatile memory available to user
  • Dimensions: 0.75x0.75" (20x20mm)

DS1307 Real Time Clock breakout board kit

This is a great battery-backed real time clock (RTC) that allows your microcontroller project to keep track of time even if it is reprogrammed, or if the power is lost. Perfect for datalogging, clock-building, time stamping, timers and alarms, etc. The DS1307 is the most popular RTC - but it requires 5V power to work (although we've used it with 5V power and 3.3V logic successfully)

  • PCB & header are included
  • Quick to assemble and use
  • Plugs into any breadboard, or you can use wires
  • Length: 24.21mm/0.95in
  • Width: 30.44mm/1.2in
  • Height: 5mm/0.2in
  • Weight: 4g/0.14oz
  • Mounting holes are 3.3mm(0.13in) diameter, 30mm(1.1in) apart
  • This board/chip uses I2C 7-bit address 0x68
  • Two mounting holes
  • Will keep time for 5 years or more

Note: As of October 10th, 2015, this product no longer comes with a CR1220 coin cell battery.

This breakout board is a kit and requires some light soldering which should only take about 15 minutes.

The DS1307 is simple and inexpensive but not a high precision device. It may lose or gain up to 2 seconds a day.

Adafruit DS1307 Real Time Clock Assembled Breakout Board

This is a great battery-backed real time clock (RTC) that allows your microcontroller project to keep track of time even if it is reprogrammed, or if the power is lost. Perfect for datalogging, clock-building, time stamping, timers and alarms, etc. The DS1307 is the most popular RTC - but it requires 5V power to work (although we've used it with 5V power and 3.3V logic successfully)

Works great with an Arduino using our RTC library or with a Raspberry Pi (or similar single board linux computer)

  • PCB & header are included
  • Plugs into any breadboard, or you can use wires
  • Two mounting holes
  • Will keep time for 5 years or more
  • Note: This product does not come with a CR1220 coin cell battery. 
  • Length: 25.8mm/1.02in
  • Width: 21.7mm/0.85in
  • Height: 5mm/0.2in
  • Weight: 2.3g/0.09oz
  • Mounting holes are 2.2mm(0.086in) diameter, 25mm(0.98in) apart
  • This board/chip uses I2C 7-bit address 0x68

The DS1307 is simple and inexpensive but not a high precision device. It may lose or gain up to 2 seconds a day. 

DS1307 specification you can find here, schematic here.

Signals and connections of the DS1307 Real Time Clock (RTC) module

I2C RTC DS1307 AT24C32 Real Time Clock Module

Control interface is 4-pin :

GND - ground pin.

VCC - power supply pin 5V or 3.3V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

DS - not in use.

SQ - not in use.

BAT - not in use.

DS1307 RTC v1.1 module. Control interface is 4-pin :

GND - ground pin.

VCC - power supply pin 5V or 3.3V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

MY012V1.0 DS1307 Real Time Clock Module (RTC) I2C Control interface is 4-pin :

GND - ground pin.

VCC - power supply pin 5V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

SQW - optional square-wave output you can get from the RTC if you have configured it to do so. not in use

Meeeno DS1307 Real-time Clock Brick Module I2C. Control interface is 4-pin :

G - ground pin.

V - power supply pin 5V or 3.3V.

SCK - I2C interface clock pin.

SDA - I2C interface data pin.

INT - optional square-wave output you can get from the RTC if you have configured it to do so. not in use

Anykits DS1307 Real Time Clock Module I2C. Control interface is 4-pin :

GND - ground pin.

VCC - power supply pin 5V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

SQW - optional square-wave output you can get from the RTC if you have configured it to do so. not in use

DS1307 Real Time Clock Mini Board I2CControl interface is 4-pin :

GND - ground pin.

+VCC - power supply pin 5V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

INT - not in use.

Real Time Clock Module DS1307 I2C. Control interface is 4-pin :

GND - ground pin.

5V - power supply pin 5V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

SQW - optional square-wave output you can get from the RTC if you have configured it to do so. not in use

DS1307 Real Time Clock breakout board kit. Control interface is 4-pin :

GND - ground pin.

5V - power supply pin 5V.

SDA - I2C interface data pin.

SCL - I2C interface clock pin.

SQW - optional square-wave output you can get from the RTC if you have configured it to do so. not in use

Wiring

This module uses I2C communication. This means that it communicates with the Arduino using just 2 pins. For Arduino Uno - Serial Data Line (SDA) (Arduino Uno SDA or Analog pin A4) and Serial Clock Line (SCL) (Arduino Uno SCL or Analog pin A5).

If you’re using other Arduino board rather than the Uno, check out what are their SCL and SDA pins.

  • Nano: SDA (A4); SCL(A5)
  • MEGA: SDA (20); SCL(21)
  • Leonardo: SDA (20); SCL(21)

The following picture shows the needed connections with the Arduino Uno 

Step by Step instruction

  1. Solder some header pins if your DS1307 Real Time Clock (RTC) module doesn't have them.
  2. Install backup battery. It will be Lithium Ion battery LIR2032 3.6 VDC or CR12203VDC or CR2032 3VDC (depends on battery holder and module). 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.
  3. Do wiring.
  4. Open Arduino IDE.
  5. Plug your Adruino Uno board into your PC and select the correct board and com port
  6. Verify and upload the the sketch to your Adruino Uno
  7. Open serial monitor at 9600 bps.
  8. You will see that date and time will be displayed in the serial monitor.

Code

Sketch1

An example from RTClib library.

Setting the Time

With the same sketch loaded, uncomment the line that starts with RTC.adjust like so:

if (! rtc.initialized()) {

    Serial.println("RTC is NOT running!");

    // following line sets the RTC to the date & time this sketch was compiled

    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

This line is very cute, what it does is take the Date and Time according the computer you're using (right when you compile the code) and uses that to program the RTC. If your computer time is not set right you should fix that first. Then you must press the Upload button to compile and then immediately upload. If you compile and then upload later, the clock will be off by that amount of time. From now on, you wont have to ever set the time again: the battery will last 5 or more years.

Reading the Time

There's pretty much only one way to get the time using the RTClib, which is to call now(), a function that returns a DateTime object that describes the year, month, day, hour, minute and second when you callednow().

There are some RTC libraries that instead have you call something like RTC.year() and RTC.hour() to get the current year and hour. However, there's one problem where if you happen to ask for the minute right at3:14:59 just before the next minute rolls over, and then the second right after the minute rolls over (so at3:15:00) you'll see the time as 3:14:00 which is a minute off. If you did it the other way around you could get 3:15:59 - so one minute off in the other direction.

Because this is not an especially unlikely occurrence - particularly if you're querying the time pretty often - we take a 'snapshot' of the time from the RTC all at once and then we can pull it apart into day() or second()as seen above. Its a tiny bit more effort but we think its worth it to avoid mistakes!

We can also get a 'timestamp' out of the DateTime object by calling unixtime which counts the number of seconds (not counting leapseconds) since midnight, January 1st 1970.

Since there are 60*60*24 = 86400 seconds in a day, we can easily count days since then as well. This might be useful when you want to keep track of how much time has passed since the last query, making some math a lot easier (like checking if its been 5 minutes later, just see if unixtime() has increased by 300, you dont have to worry about hour changes).

Sketch2

Working with the RTC requires two important steps:

  1. setting the current time, so that the RTC knows what time is it
  2. retaining the time, so that the RTC always gives the correct time, even when it is turned off

Set the current time in the Real Time Clock

For setting the current time you need to change the code provided. Set your current time in the function setDS3231time()


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. After setting the current time, you can upload the provided code with the required modifications.

Retain the time in the Real Time Clock

If you don’t want to reset the time everytime the RTC is turned off, after setting up the time, you should comment the function that sets the time and upload the code again.

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.

Sketch 3

With this code you can set the date/time, retreive the date/time and use the extra memory of an RTC DS1307 chip. The program also sets all the extra memory space to 0xff. Serial Communication method with the Arduino that utilizes a leading CHAR for each command described below.

Commands: T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip. Example to set the time for 26-May-18 at 13:43:00 for the 6 day of the week, use this command - T0043136260518 Q(1-2) - (Q1) Memory initialization (Q2) RTC - Memory Dump

Summary

We learnt how to connect DS1307 Real Time Clock (RTC) module to Arduino board and use it.

Libraries:

  • All libraries attached on the begining of the project description.
  • Wire library included in your Arduino IDE.
  • RTClib library. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read about it here.

Sketch:

  • See attachment on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 26-05-2018
Viewed: 4345 times