Electronics

Real Time Clock RTC Module DS3231 For Raspberry Pi

AED 19.50

Low stock
1

Description

The Real Time Clock (RTC) Module DS3231 for Raspberry Pi is a specially designed module that enables accurate timekeeping on the Raspberry Pi. Since the Raspberry Pi lacks an onboard RTC, this module serves as an external timekeeping solution. It uses the I2C bus to communicate with the Raspberry Pi.

 

Package Includes:

  • 1 x Real Time Clock (RTC) Module DS3231 for Raspberry Pi

 

Features:

  • Accurate Timekeeping: The module utilizes the Maxim DS3231 chip, which offers highly accurate timekeeping capabilities. It provides real-time counting for seconds, minutes, hours, day, date, month, and year.
  • Temperature Compensation: The DS3231 chip features a temperature-compensated crystal oscillator (TCXO), ensuring accurate timekeeping even in varying temperature conditions.
  • Long Battery Life: The module includes a small button battery that powers the RTC even when the Raspberry Pi is turned off. It has a low battery consumption rate of less than 500nA in Battery-Backup Mode, ensuring long-lasting battery life.
  • Easy Integration: The module is designed to be compatible with the Raspberry Pi and can be connected to the Raspberry Pi's 40-pin GPIO header. It requires no extra modification for usage, allowing for convenient plug-and-use operation.
  • I2C Communication: The module communicates with the Raspberry Pi via the I2C bus, allowing for efficient and reliable data transfer between the RTC module and the Raspberry Pi.
  • Wide Temperature Range: The module can operate within a temperature range of -40 °C to +85 °C, making it suitable for various environmental conditions.
  • High Timing Accuracy: The DS3231 chip maintains timing accuracy of ± 5ppm (± 0.432 sec/day), ensuring precise and reliable timekeeping over extended periods.
  • Leap Year Compensation: The RTC module accounts for leap years, providing accurate date and time information until the year 2100.
  • Compatibility with Arduino: In addition to the Raspberry Pi, the RTC module can also be used with Arduino, making it versatile for different microcontroller projects.

 

Description:

The module uses the DS3231 chip, which offers improved performance and power efficiency compared to the DS1307. The DS3231 incorporates a temperature-compensated crystal, resulting in more accurate timekeeping across varying temperatures and extended periods. It also includes a small button battery that allows the RTC to continue running even when the Raspberry Pi is powered off. The DS3231 RTC module is compatible with both 3.3V and 5V systems, making it suitable for use with both Raspberry Pi and Arduino boards. It can be easily connected to the Raspberry Pi's 40-pin GPIO header without requiring any additional modifications. The module provides precise time information, including seconds, minutes, hours, days, dates, months, and years. It can accurately count the time until the year 2100, compensating for leap years. The timing accuracy is maintained within ±5ppm (±0.432 sec/day) in a temperature range of -40 °C to +85 °C. Additionally, the DS3231 RTC module consumes less than 500nA of power in Battery-Backup Mode, ensuring long-lasting operation while using minimal energy. It can be controlled and operated using shell commands.

 

Principle of Work:

  1. Crystal Oscillator: The DS3231 chip inside the module utilizes a crystal oscillator to generate highly stable and accurate clock signals. The crystal oscillator acts as a time reference for the RTC.
  2. Clock Calibration: The DS3231 chip is factory calibrated to ensure accurate timekeeping. It compensates for any frequency deviations in the crystal oscillator to maintain precise time counting.
  3. Temperature Compensation: The DS3231 chip includes a built-in temperature sensor. It continuously measures the ambient temperature and adjusts the clock frequency to compensate for temperature variations. This temperature compensation feature helps improve the accuracy of timekeeping over different temperature ranges.
  4. Power Management: The DS3231 module is designed to operate with a main power source (e.g., the Raspberry Pi) and a backup power source, typically a small button battery. When the main power source is available, the DS3231 chip uses it to operate and keep track of time. When the main power is disconnected or turned off, the module seamlessly switches to the backup power source. The button battery provides power to the DS3231 chip, allowing it to maintain accurate timekeeping even when the primary power is not available.
  5. I2C Communication: The DS3231 module communicates with the host device (e.g., Raspberry Pi) using the I2C bus protocol. It establishes a bidirectional communication channel, enabling the host device to read the current time and date from the DS3231 chip or set new time values.
  6. Register Configuration: The DS3231 chip contains registers that store various time and date information. The host device can access these registers through I2C commands to retrieve or update the time and date values.

 

Pinout of the Module:

  1. VCC: Power supply pin (3.3V or 5V).
  2. GND: Ground pin.
  3. SDA: I2C data line, used for communication with the Raspberry Pi (connect to the Raspberry Pi's SDA pin).
  4. SCL: I2C clock line, used for communication with the Raspberry Pi (connect to the Raspberry Pi's SCL pin).

 

 

Applications:

  1. Timekeeping: The RTC module is primarily used for accurate timekeeping in projects that require precise scheduling or logging of events. It ensures that the system maintains accurate time even when the Raspberry Pi is powered off or experiences power interruptions.
  2. Data Logging: The module can be utilized in data logging applications where time-stamped data is collected and stored. It enables synchronized recording of events, measurements, or sensor data with accurate timestamps.
  3. Alarm Systems: The RTC module can be integrated into alarm systems to trigger specific actions at predetermined times. It allows for scheduled events, alarms, or reminders to be executed based on accurate timekeeping.
  4. Home Automation: In home automation projects, the RTC module can be used to schedule and control various tasks. It enables automation systems to perform specific actions at specific times, such as turning lights on or off, adjusting temperature settings, or activating/deactivating devices.
  5. IoT Devices: The RTC module is valuable for Internet of Things (IoT) projects that require accurate time synchronization across multiple devices or networked systems. It ensures that data sent or received by IoT devices is timestamped correctly, facilitating data analysis and synchronization.
  6. Data Encryption: The RTC module can play a role in cryptographic applications, where precise timekeeping is necessary for secure key generation, authentication protocols, or digital signatures.
  7. Industrial Automation: In industrial settings, the RTC module can be used for time-sensitive processes, scheduling tasks, or coordinating operations across different systems or machines.
  8. Robotics: The module can be employed in robotics projects to synchronize actions, schedule tasks, or maintain accurate time-based control of robot movements.

 

Circuit:

 

The Real-Time Clock (RTC) Module DS3231 is connected to Raspberry Pi 3 on GPIO pins 1, 3, 5, and 9 as you see in the picture.

 

Library: 

To install the smbus2 the library you need to execute the following command in your terminal:

pip install smbus2

If the pip command is not available, it means that the pip package manager is not installed on your system. To install pip, you can follow these steps:

sudo apt install python3-pip

 

Code:

an example Python code for accessing the Real-Time Clock (RTC) Module DS3231 connected to Raspberry Pi 3 on GPIO pins 1, 3, 5, and 9:

import smbus2
import time

# Define the I2C bus number (0 or 1) and the DS3231 address
bus_number = 1
rtc_address = 0x68

# Open the I2C bus
bus = smbus2.SMBus(bus_number)

# Function to read the current time from the RTC
def get_time():
    # Read the time registers from the RTC
    time_data = bus.read_i2c_block_data(rtc_address, 0x00, 7)
    # Extract the individual time components
    seconds = ((time_data[0] & 0b01111111) + (time_data[0] >> 7) * 10)
    minutes = ((time_data[1] & 0b01111111) + (time_data[1] >> 7) * 10)
    hours = ((time_data[2] & 0b00111111) + (time_data[2] >> 7) * 10)
    day = time_data[3]
    date = ((time_data[4] & 0b00111111) + (time_data[4] >> 7) * 10)
    month = ((time_data[5] & 0b00011111) + (time_data[5] >> 7) * 10)
    year = ((time_data[6] & 0b1111111) + (time_data[6] >> 7) * 10 + 2000)
    
    # Return the time as a formatted string
    return f"{hours:02d}:{minutes:02d}:{seconds:02d} {day:02d}/{month:02d}/{year}"

# Main program loop
while True:
    # Get and print the current time from the RTC
    current_time = get_time()
    print(f"Current Time: {current_time}")
    
    # Wait for 1 second
    time.sleep(1)

 

  1. The smbus2 and time libraries are imported. smbus2 provides functions for I2C communication, and time is used for the delay between time readings.
  2. The I2C bus number (bus_number) and the address of the DS3231 module (rtc_address) are defined. In this code, it assumes the I2C bus number is 1, and the DS3231 address is 0x68.
  3. The I2C bus is opened using the smbus2.SMBus() function, and the bus object is created for further communication with the DS3231 module.
  4. The get_time() function is defined to read the current time from the RTC module. Inside the function:
  • The bus.read_i2c_block_data() function is used to read the time registers from the RTC module. It reads 7 bytes starting from register 0x00.
  • The individual time components, such as seconds, minutes, hours, day, date, month, and year, are extracted from the read data. The values are obtained by manipulating the bits of the data using bitwise operations.
  • The extracted time components are formatted into a string with leading zeros, and the string is returned.
  1. The main program loop starts with an infinite loop using while True. Inside the loop:

    • The current_time variable is assigned the value returned by the get_time() function, which gives the current time from the RTC module.
    • The current time is printed using print(f"Current Time: {current_time}").
    • The program waits for 1 second using time.sleep(1) to provide a delay before reading the time again.

By running this code, the Raspberry Pi will continuously read the current time from the DS3231 RTC module and print it every second. The get_time() function handles the communication with the module and extracts the time components from the received data.

 

Technical Details: 

  • Chip: Maxim DS3231
  • Power Supply Voltage: 3.3V and 5V compatible
  • Communication Interface: I2C
  • Temperature Range: -40 °C to +85 °C
  • Timing Accuracy: ± 5ppm (± 0.432 sec/day)
  • Battery Backup Mode Current Consumption: Less than 500nA
  • Real-Time Counting: Provides seconds, minutes, hours, day, date, month, and year counting, valid until the year 2100 with leap-year compensation
  • Battery: Soldered small button battery included
  • Compatibility: Can be used with Raspberry Pi (connects to 40-pin GPIO) and Arduino

 

Resources:

 

Comparisons:

The DS3231 and DS1307 are both popular Real-Time Clock (RTC) modules used for timekeeping in electronic projects. Here are some comparisons between the two:

  1. Performance: The DS3231 generally outperforms the DS1307 in terms of performance and accuracy. The DS3231 has a temperature-compensated crystal oscillator, which provides better timekeeping accuracy over temperature variations. The DS1307, on the other hand, does not have this feature and may experience larger deviations in timekeeping accuracy.
  2. Power Consumption: The DS3231 module has lower power consumption compared to the DS1307. The DS3231 consumes less than 500nA in battery-backup mode, making it more suitable for low-power applications or scenarios where power efficiency is important.
  3. Year Range: The DS3231 and DS1307 both provide timekeeping until the year 2100 with leap year compensation. They can handle dates and times within this range.
  4. I2C Communication: Both modules use the I2C bus for communication with the host device (e.g., Raspberry Pi). This allows easy integration with microcontrollers and single-board computers.
  5. Compatibility: The DS3231 module is designed specifically for the Raspberry Pi, but it can also be used with other devices, including Arduino. The DS1307 is widely used and compatible with various platforms, including Raspberry Pi and Arduino.