Blog
A Practical Guide to DS1302 and DS1307 RTC Module
Hey makers! Ever built an Arduino data logger or smart clock only to have the time reset after power-off? You need a DS1307 real time clock! This beginner-friendly DS1307 RTC module is an I2C lifesaver. But is the DS1302 real time clock module a better fit for your ESP32 or 3.3V project? Let’s dive into this quick guide on the DS1302 and DS1307 RTC module to help you decide.
What is DS1302 and DS1307 RTC Module?
The DS1302 RTC module is a low-current charging clock chip that incorporates a real-time clock/calendar and 31-byte static RAM. It communicates with the microcontroller via a simple serial interface. The real-time clock/calendar circuit provides information on seconds, minutes, hours, day, week, month, and year. The number of days in each month and the number of days in leap years can be automatically adjusted. The clock operation can be determined by the AM/PM indication to adopt either 24-hour or 12-hour format. The DS1302 real time clock can communicate with the microcontroller simply in a synchronous serial manner, using only three pins: RST reset, I/O data line, and SCLK serial clock. The read/write data of the clock/RAM is transmitted in a byte or up to 31-byte character group format. When the DS1302 module is operating, its power consumption is very low, with the power consumption when storing data and time information being less than 1mw.
The DS1307 RTC Module (24C32 32K I2C EEPROM RAM) uses a rechargeable LIR2032 lithium battery and comes with a charging circuit to address the issue of the DS1307 not being readable or writable when equipped with a backup battery. Once fully charged, it can power the DS1307 RTC module for one year. The clock pin of the DS1307 is brought out to provide a clock signal for the microcontroller. It can be cascaded with other I2C devices. The RTC DS1307 module is a low-power, full BCD code clock calendar real-time clock chip with 56 bytes of non-volatile RAM. Address and data are transmitted through a two-wire bidirectional serial bus. The chip can provide information such as seconds, minutes, and hours, and automatically adjusts the number of days in each month. It also has a leap year compensation function. The AM/PM flag bit determines whether the clock operates in 24-hour or 12-hour mode. The chip has an internal power supply detection circuit with power failure detection and battery switching functions.
Working Principle
DS1302 Working Principle
- DS1302 Schematic Diagram
- DS1302 Functional Block Diagram
The main components of the serial timer are shown in the figure: shift register, control logic, oscillator, real time clock and RAM.
- DS1302 Signal Description
(1) Vcc1: In single power supply and battery-powered systems, Vcc1 provides low power consumption operation and low power consumption battery backup. In systems using a trickle charger, the rechargeable energy is connected to this pin.
(2) Vcc2: Vcc2 is the main power pin in a dual power supply configuration. Vcc1 is connected to the backup power supply and maintains time and date in the absence of the main power supply. The DS1302 will be powered by the larger of Vcc1 or Vcc2. When Vcc2 is greater than Vcc1 + 0.2V, Vcc2 will supply power to the DS1302. When Vcc2 is less than Vcc1, Vcc1 will supply power to the DS1302.
(3) SCLK (Serial Clock Input): SCLK is used to synchronize the data movement on the serial interface.
(4) I/O (Data Input/Output): The I/O pins are bidirectional data pins for a three-wire interface.
(5) RST (Reset): During read or write operations, the reset signal must be set to a high level.
(6) X1, X2: Connection pins for standard 32.768 kHz quartz crystals.
(7) Internal Oscillator: Designed to work with a crystal with a specified load capacitance of 6 pF.
The DS1302 RTC module can also be driven by an external 32.768 kHz oscillator. In this configuration, the X1 pin is connected to the external oscillator signal, and the X2 pin is left floating.
DS1307 Working Principle
- DS1307 Schematic Diagram
- DS1307 Functional Block Diagram
- DS1307 Real Time Clock Chip
This module mainly employs a low-cost and extremely precise real-time clock chip — the DS1307 chip. It manages all the timing functions and has a simple two-wire I2C interface, which can be easily connected to any microcontroller of your choice.
The DS1307 chip maintains information on seconds, minutes, hours, day of the week, date, month and year. For months with fewer than 31 days, the end-of-month date will be automatically adjusted, including corrections for leap years (valid until 2100). The clock operates in 24-hour or 12-hour format with an AM/PM indicator. It also provides two programmable time alarms.
Another cool feature of this module is the SQW pin, which can output a beautiful square wave of 1Hz, 4kHz, 8kHz or 32kHz and can be processed through programming.
The DS1307 RTC module comes with an external 32kHz crystal for timing. However, the problem with these crystals is that external temperature can affect their oscillation frequency. This frequency change is negligible but will definitely increase. This may sound like a problem, but it is not. In fact, the result is that the clock is reduced by approximately five minutes every month.
- Backup battery
The DS1307 includes a battery input and maintains accurate timekeeping when the main power supply to the device is interrupted.
The built-in power detection circuit continuously monitors the VCC status to detect power failures and automatically switch to the backup power supply. Therefore, you don’t have to worry about power outages, your MCU can still keep track of time.
On the back of the circuit board, there is a battery holder designed for a 20mm 3V button cell lithium battery. Any CR2032 battery can be installed well.
Assuming a 47mAh CR2032 battery is used, fully charged, and the chip consumes its minimum 300nA current, this battery can keep the RTC running for at least 17.87 years without an external 5V power supply.
47mAh/300nA = 156666.67 hours = 6527.78 days = 17.87 years
- Built-in 24C32 EEPROM
The DS1307 RTC module is also equipped with a 32-byte 24C32 EEPROM chip from Atmel, which has a certain read-write cycle. It can be used to store settings or any other information.
The 24C32 EEPROM communicates via the I2C interface and shares the same I2C bus with the DS1307. The onboard 24C32 EEPROM has a hardwired I2C address and is set to 0x50.
- Hidden function of the module – DS18B20
There is a frequently overlooked feature on the DS1307 real time clock module, which enables us to install a DS18B20 temperature sensor.
The three through-holes on the right upper corner next to the battery holder (marked as U1) are the installation positions for the DS18B20.
After installing the DS18B20, you will be able to obtain temperature readings from the DS pin. These readings can also be used to compensate for the temperature-based time drift in the code. Solder the DS18B20 according to the silk-screen markings. A 4.7K resistor may also be needed between VCC and DS.
Pin Functions
DS1302 Pin Functions
Pin | Definition |
X1,X2 | 32.768 kHz Crystal Pins |
GND | Ground |
/RST | Reset |
I/O | Data Input/Output |
SCLK | Serial Clock |
Vcc1,Vcc2 | Power Supply Pins |
DS1307 Pin Functions
Q: Someone asked: What are the purposes of SQ, DS, and the three holes above?
A: The three holes are for soldering the DS18B20 temperature sensor. As the name suggests, DS is the pin for reading the temperature. The designer probably intended for the user to create a weather calendar module. SQ is a square wave output, short for “square”, and is used for testing and debugging purposes.
Parameters
Real Time Clock DS1302 Parameters
- The backup battery is CR2032, with a voltage of 3V and a current of 260mAh. It is a non-rechargeable battery. The theoretical data indicates a retention time of more than 10 years!
- The crystal oscillator is 32.768KHz, with a matching capacitor of 6pF, and the size is 2*6mm.
- DS1302 is an 8-pin surface-mount chip. There is an IC socket below the chip, which is convenient for replacing and removing the chip.
- The module’s operating voltage is compatible with 3.3V/5V, and it can be connected to 5V and 3.3V microcontrollers.
- Operating temperature: 0℃~70℃
Real Time Clock DS1307 Parameters
- DS1307 I2C Real Time Clock Chip
- 24C32 32K I2C EEPROM RAM
- Connect the clock pins of DS1307 to provide clock signals for the microcontroller. It can be cascaded with other I2C devices.
Arduino Tutorial
DS1307 Arduino
DS1307 Pinout
- DS1307 Arduino
VCC ——> 5V
GND ——> GND
SCL ——> SCL
SDA ——> SDA
- OLED Arduino
VCC ——> 5V
GND ——> GND
SCL ——> A5
SDA ——> A4
DS1307 Arduino code
#include
#include
#include
#include
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
RTC_DS1307 rtc;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
// Initialize OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED allocation failed"));
for(;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Initialize RTC
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
}
// Sync with computer time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Show brief initialization
display.setTextSize(1);
display.setCursor(40, 30);
display.println(F("Ready"));
display.display();
delay(800);
}
void loop() {
DateTime now = rtc.now();
display.clearDisplay();
// Date display (slightly smaller font - size 1.5 equivalent)
display.setTextSize(1); // Base size
display.setCursor(15, 12);
display.print(now.year());
display.print(F("-"));
if(now.month() < 10) display.print(F("0"));
display.print(now.month());
display.print(F("-"));
if(now.day() < 10) display.print(F("0"));
display.print(now.day());
// Time display (unchanged size 2)
display.setTextSize(2);
display.setCursor(15, 35);
if(now.hour() < 10) display.print(F("0"));
display.print(now.hour());
display.print(F(":"));
if(now.minute() < 10) display.print(F("0"));
display.print(now.minute());
display.print(F(":"));
if(now.second() < 10) display.print(F("0"));
display.print(now.second());
display.display();
delay(1000);
}
DS1307 Effect Demonstration
DS1302 and DS1307 Application Scenarios
The DS1307 RTC module is widely used for time-keeping in Arduino projects. When you need to add a real-time clock, the DS1307 module provides excellent accuracy with battery backup. For simpler applications, the DS1302 moduleis a cost-effective alternative. Although the communication protocol differs, the DS1302 RTC module is also very capable of handling basic date and time functions.
Relative Information
DS1302 and DS1307 RTC Module Purchase Link
FAQ
Q: What is the RAM in the DS1302 used for? What kind of data can be stored?
A: The DS1302 has 31 bytes of general-purpose, battery-backed RAM. This means the data stored in it will be preserved even when the main power is off (as long as the backup battery is connected).
Typical uses include:
- Storing system configuration settings or parameters.
- Storing event counters (e.g., how many times a device was powered on).
- Holding small amounts of non-volatile data without needing a separate EEPROM chip.
- It is not used for clock/calendar functions; those are stored in separate registers.
Q: What is the use of the SQW/OUT pin on the DS1307? How to output a square wave?
A:The SQW/OUT pin is a multi-function pin that can be programmed to output a square wave signal or to simply go high/low.