• sales

    +86-0755-88291180

RPi Tutorial Series: RTC User Guide

Raspberry Pi doesn't have RTC function so it cannot get the correct time without network. This problem can be solved by external RTC module and a DS3231 RTC module may be very helpful. For example, We will introduce how to use the DS3231 module on the Pioneer600 expansion board.

Setup

1. Raspberry Pi reads/writes time information from DS3231 via I2C interface, so the I2C interface should be enabled.

sudo raspi-config

Select Advaced Options -> I2C -> <Yes>

2. Edit the configuration file to add a new device.

sudo vi /boot/config.txt

Add a new RTC device DS3231 to the device tree

dtoverlay=i2c-rtc,ds3231

Reboot to take effect. About Device Tree, see: /boot/overlay/README

3. Read the Hardware Clock.

sudo hwclock –r

Read the system time:

date

4. Set the Hardware Clock to the time given by the --date option.

sudo hwclock --set --date="Aug-22-2016 18:00:00"

Here the date string can accept different date formats.

5. Set the System Time from the Hardware Clock.

sudo hwclock -s

6. Read the RTC and system times.

sudo hwclock -r;date

Programming

We can wirte a I2C program to read/write the RTC. First we should detect the I2C state using this command:

i2cdetect -y 1




This is a command from I2C-Tools. For more details, please read the previous lesson Raspberry Pi Tutorial Series: I2C. If you have read the DS3231 datasheet, you knew this the I2C address of the chip is 0x68. But the "UU" here means probing was skipped, because this address is currently in use by a driver. We cannot control such a I2C device until we uncomment a certain line in the config.txt file.

sudo vi /boot/config.txt

Uncomment this line with a hash (#)

#dtoverlay=i2c-rtc,ds3231

Restart your Pi and check the I2C state again with i2cdetect -y 1. Now the 0x68 is not UU anymore.



Some examples are given in this part to explain how to read/write time data to DS3231 via I2C bus using BCM2835 or Python Libraries.

You should install some specific libraries before using these examples, see: Libraries Installation for RPi

BCM2835

#include <bcm2835.h>
#include <stdio.h>
#include <unistd.h>
 
// regaddr, second, minute, hour, weekday, day, month, year.
// 0x16: year,             2016
// 0x08: month,            August
// 0x12: day of the month, 12
// 0x06: day of the week,  Friday
// 0x18: hour,             18
// 0x19: minute,           19
// 0x20: second,           20
char buf[]={0x00, 0x20, 0x19, 0x18, 0x06, 0x12, 0x08, 0x16};
char *str[]  ={"SUN", "MON", "TUES", "WED", "THUR", "FRI", "SAT"};
void pcf8563SetTime()
{
    bcm2835_i2c_write(buf, 8);
}
 
void pcf8563ReadTime()
{  
    buf[0] = 0x00; 
    bcm2835_i2c_write_read_rs(buf, 1, buf, 7); 
}
 
int main(int argc, char **argv) 
{ 
    if (!bcm2835_init())return 1; 
    bcm2835_i2c_begin(); 
    bcm2835_i2c_setSlaveAddress(0x68); 
    bcm2835_i2c_set_baudrate(10000); 
    printf("start..........\n");
     
    pcf8563SetTime();
    while(1) 
    {  
        pcf8563ReadTime();
        buf[0] = buf[0] & 0x7F; //sec
        buf[1] = buf[1] & 0x7F; //min
        buf[2] = buf[2] & 0x3F; //hour
        buf[3] = buf[3] & 0x07; //day of the week
        buf[4] = buf[4] & 0x3F; //day of the month
        buf[5] = buf[5] & 0x1F; //month
        //year/month/day
        printf("20%02x/%02x/%02x  ", buf[6], buf[5], buf[4]);
        //hour:minute/second
        printf("%02x:%02x:%02x  ", buf[2], buf[1], buf[0]);
        //weekday
        printf("%s\n", str[(unsigned char)buf[3]-1]);
        bcm2835_delay(1000);
    }
 
    bcm2835_i2c_end();
    bcm2835_close();
 
    return 0;
}

Save the file as "ds3231.c". Compile and run with:

gcc -Wall ds3231.c -o ds3231 -lbcm2835
sudo ./ds3231

Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smbus
import time
 
address = 0x68
register = 0x00
#sec min hour week day month year
NowTime = [0x00,0x00,0x18,0x04,0x12,0x08,0x15]
w  = ["SUN","Mon","Tues","Wed","Thur","Fri","Sat"];
#/dev/i2c-1
bus = smbus.SMBus(1)
def ds3231SetTime():
    bus.write_i2c_block_data(address,register,NowTime)
 
def ds3231ReadTime():
    return bus.read_i2c_block_data(address,register,7);
 
ds3231SetTime()
while 1:
    t = ds3231ReadTime()
    t[0] = t[0]&0x7F  #sec
    t[1] = t[1]&0x7F  #min
    t[2] = t[2]&0x3F  #hour
    t[3] = t[3]&0x07  #week
    t[4] = t[4]&0x3F  #day
    t[5] = t[5]&0x1F  #month
    print("20%x/%x/%x %x:%x:%x  %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]-1]))
time.sleep(1)

Save the file as "ds3231.py" and run with:

sudo python ds3231.py
TAG: Raspberry Pi 5 PD Activation Module Raspberry Pi 5/4B easy Adapter Micro HDMI to HDMI 4K All Ports To Pi's USB Side 2.8inch Capacitive TouchScreen LCD Display ST7789 CST328 240x320 For Arduino/Raspberry Pi/ESP32/Pico ESP32 C3 DeepSeek XiaoZhi AI Chat Robot ESP32-S3 1.54 inch LCD N16R8 MUMA Development Board Display Screen Wifi Weather Station TimeClock Tang Nano FPGA Demo Code Pi5 Case Pan-Tilt Kit Raspberry Pi Pico 2 RP2350 bit board Size &amp;amp; Port high Compatible With BBC Micro:bit 2.66inch e-Paper e-link Module 360x184 4-Color Red Yellow Black White For Arduino Raspberry Pi STM32 Jetson Tutorial Information Wiki Program Code User Guide SpotPear ESP32 C6 Development Board 1.47 inch LCD Display Screen ST7789 For Arduino WiFi6/LVGL/HMI Onborad SD-Port/RGB-LED Jetson Nano Development Kit JETSON-NANO-DEV-KIT Replace For Jetson-Nano-Developer-Kit-B01 Luckfox Pico ST7789 SpotPear Luckfox Pico MINI Raspberry Pi Pico Camera 3D Display Transparent Screen Raspberry Pi 11.6 inch LCD HDMI /Type C Display In-Cell Capacitive TouchScreen 1768x828 For Jetson Nano/mini Computer PC STM32 0.85inch LCD IPS