The DS1307 is a real-time clock (RTC) chip designed to keep track of the current time and date, including seconds, minutes, hours, day, date, month, and year. It communicates with microcontrollers using the I2C (Inter-Integrated Circuit) protocol, making it easy to integrate into a wide range of electronic projects. One of its key features is the ability to maintain timekeeping even during power outages, thanks to its built-in battery backup functionality.
The DS1307 RTC chip has the following key technical specifications:
Parameter | Value |
---|---|
Operating Voltage | 4.5V to 5.5V |
Backup Battery Voltage | 2.0V to 3.5V |
Communication Protocol | I2C (2-wire) |
Timekeeping Accuracy | ±2 seconds/day (at 25°C) |
Operating Temperature | -40°C to +85°C |
Current Consumption | 1.5µA (typical in battery backup mode) |
Clock Format | 12-hour or 24-hour |
Memory | 56 bytes of non-volatile RAM |
The DS1307 has 8 pins, as described in the table below:
Pin Number | Pin Name | Description |
---|---|---|
1 | X1 | Connect to the external 32.768 kHz crystal oscillator (input). |
2 | X2 | Connect to the external 32.768 kHz crystal oscillator (output). |
3 | VBAT | Backup battery input (2.0V to 3.5V). Maintains timekeeping during power loss. |
4 | GND | Ground (0V reference). |
5 | SDA | Serial Data Line for I2C communication. |
6 | SCL | Serial Clock Line for I2C communication. |
7 | NC | No connection (leave unconnected). |
8 | VCC | Primary power supply (4.5V to 5.5V). |
0x68
.Below is an example of how to use the DS1307 with an Arduino UNO. This code uses the popular RTClib
library.
#include <Wire.h>
#include <RTClib.h>
// Create an RTC_DS1307 object to interact with the DS1307 RTC
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
Wire.begin(); // Initialize I2C communication
if (!rtc.begin()) {
Serial.println("Couldn't find RTC. Check connections!");
while (1); // Halt execution if RTC is not found
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, setting the time...");
// Set the RTC to the current date and time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// Get the current date and time from the RTC
DateTime now = rtc.now();
// Print the current date and time to the Serial Monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
delay(1000); // Wait for 1 second before updating
}
RTC Not Detected
Time Resets After Power Loss
Inaccurate Timekeeping
I2C Communication Fails
0x68
) and ensure no address conflicts.Q: Can the DS1307 operate without a backup battery?
A: Yes, but it will lose the current time and date when the primary power supply is disconnected.
Q: What is the maximum length for I2C communication lines?
A: The maximum length depends on the pull-up resistor values and the capacitance of the lines, but it is typically limited to a few meters.
Q: Can I use the DS1307 with a 3.3V microcontroller?
A: The DS1307 requires a 5V power supply. However, you can use level shifters to interface it with a 3.3V microcontroller.
Q: How do I switch between 12-hour and 24-hour modes?
A: The hour register in the DS1307 contains a bit to toggle between 12-hour and 24-hour modes. Refer to the datasheet for details on modifying this bit.