Using the DS1307 RTC with the Raspberry Pi

Using the DS1307 RTC with the Raspberry Pi

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

  1. Raspberry Pi (any model with GPIO and I2C support, e.g., Pi 3, Pi 4)
  2. DS1307 RTC Module
  3. CR2032 Battery (for maintaining time when powered off)
  4. Breadboard and Jumper Wires
  5. 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

  1. Open the Raspberry Pi configuration tool:
    sudo raspi-config
    
  2. Navigate to Interface Options > I2C and enable it.
  3. Reboot the Raspberry Pi:
    sudo reboot
    
  4. Verify that the DS1307 is detected on the I2C bus:
    sudo i2cdetect -y 1
    
    The DS1307 should appear at address 0x68.

Step 3: Install Required Libraries

  1. Update your Raspberry Pi’s package list:
    sudo apt update && sudo apt upgrade -y
    
  2. 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:

  1. Read the DS1307 RTC time and set it as the system time:
    sudo hwclock -r
    sudo hwclock -s
    
  2. Set the DS1307 RTC time from the Raspberry Pi system time:
    sudo hwclock -w
    

Step 6: Applications of the DS1307 RTC Module

  1. Keeping Time on Headless Raspberry Pi Devices – Useful for data logging projects.
  2. Home Automation Systems – Ensure accurate scheduled events even after power loss.
  3. IoT Projects – Use the DS1307 for timestamping data in disconnected environments.
  4. Weather Stations – Store accurate timestamps for temperature and humidity records.

Troubleshooting

  1. DS1307 Not Detected (i2cdetect does not show 0x68)

    • Ensure the SDA/SCL pins are correctly wired.
    • Verify that I2C is enabled using sudo raspi-config.
  2. Incorrect Date/Time Readings

    • Run sudo hwclock -r to check the hardware clock.
    • Reset the RTC clock using sudo hwclock -w.
  3. 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. 🚀

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.