The DS1307 Real-Time Clock (RTC) module allows the Raspberry Pi to keep track of time even when it is powered off. This guide will walk you through setting up the DS1307 RTC module with the Raspberry Pi using the I2C protocol.
What You Will Need
- Raspberry Pi (any model with GPIO and I2C support, e.g., Pi 3, Pi 4)
- DS1307 RTC Module
- CR2032 Battery (for maintaining time when powered off)
- Breadboard and Jumper Wires
- Python installed on the Raspberry Pi
Step 1: Wiring the DS1307 to the Raspberry Pi
The DS1307 communicates via I2C, so we connect it to the Raspberry Pi’s I2C pins.
Connections (I2C Mode)
DS1307 Pin | Raspberry Pi Pin |
---|---|
VCC | 3.3V or 5V (Pin 1 or 2) |
GND | Ground (Pin 6) |
SDA | SDA (Pin 3, GPIO2) |
SCL | SCL (Pin 5, GPIO3) |
Step 2: Enable I2C on the Raspberry Pi
- Open the Raspberry Pi configuration tool:
sudo raspi-config
- Navigate to Interface Options > I2C and enable it.
- Reboot the Raspberry Pi:
sudo reboot
- Verify that the DS1307 is detected on the I2C bus:
The DS1307 should appear at addresssudo i2cdetect -y 1
0x68
.
Step 3: Install Required Libraries
- Update your Raspberry Pi’s package list:
sudo apt update && sudo apt upgrade -y
- Install the required I2C tools and Python libraries:
sudo apt install -y i2c-tools python3-smbus python3-pip pip3 install adafruit-circuitpython-ds1307
Step 4: Setting and Reading Time on the DS1307
Python Code to Set the Date and Time
Run this script once to set the RTC clock.
import time
import board
import busio
import adafruit_ds1307
# Initialize I2C bus and DS1307
i2c = busio.I2C(board.SCL, board.SDA)
rtc = adafruit_ds1307.DS1307(i2c)
# Set the date and time (Year, Month, Day, Hour, Minute, Second, Weekday, DST)
rtc.datetime = time.struct_time((2025, 1, 1, 12, 0, 0, 0, -1, -1))
print("RTC Date and Time Set!")
Python Code to Read Time from DS1307
After setting the time, use this script to read the current date and time.
import time
import board
import busio
import adafruit_ds1307
# Initialize I2C bus and DS1307
i2c = busio.I2C(board.SCL, board.SDA)
rtc = adafruit_ds1307.DS1307(i2c)
while True:
current_time = rtc.datetime
print(f"Date: {current_time.tm_year}-{current_time.tm_mon:02d}-{current_time.tm_mday:02d}")
print(f"Time: {current_time.tm_hour:02d}:{current_time.tm_min:02d}:{current_time.tm_sec:02d}")
time.sleep(1)
Step 5: Syncing DS1307 with the Raspberry Pi System Clock
To sync the DS1307 RTC time with the Raspberry Pi system time:
-
Read the DS1307 RTC time and set it as the system time:
sudo hwclock -r sudo hwclock -s
-
Set the DS1307 RTC time from the Raspberry Pi system time:
sudo hwclock -w
Step 6: Applications of the DS1307 RTC Module
- Keeping Time on Headless Raspberry Pi Devices – Useful for data logging projects.
- Home Automation Systems – Ensure accurate scheduled events even after power loss.
- IoT Projects – Use the DS1307 for timestamping data in disconnected environments.
- Weather Stations – Store accurate timestamps for temperature and humidity records.
Troubleshooting
-
DS1307 Not Detected (
i2cdetect
does not show0x68
)- Ensure the SDA/SCL pins are correctly wired.
- Verify that I2C is enabled using
sudo raspi-config
.
-
Incorrect Date/Time Readings
- Run
sudo hwclock -r
to check the hardware clock. - Reset the RTC clock using
sudo hwclock -w
.
- Run
-
RTC Time Not Persisting After Power Off
- Ensure the CR2032 battery is installed correctly.
- Replace the battery if it is low or dead.
Conclusion
The DS1307 RTC module provides accurate real-time tracking for Raspberry Pi projects, even when powered off. By following this guide, you can integrate RTC functionality into IoT, automation, and data logging applications. 🚀