The DS3231 is a hardware RTC module communicating via I2C. This module can be used to keep the current time when the Pi is powered off. When booting, the Pi has to resync the system time by either the network or a hardware clock. If no network is available, the hardware clock acts as a fallback.
DS3231 modules have typically six pins in total. We need just four of them.
DS3231 Pin | Raspberry Pi Pin |
---|---|
GND | GND |
VCC | VCC (3.3 V) |
SDA | SDA (GPIO 2) |
SCL | SCL (GPIO 3) |
SQW | not connected |
32K | not connected |
I2C is a bus system. Other devices can be connected to the same pins, but still work at the same time if they have different addresses. DS3231 are usually using address 0x68.
Install i2c-tools
if not already done:
sudo apt install i2c-tools
Check if the module is recognized correctly:
sudo i2cdetect -y 1
This command should return an entry in the matrix for 0x68 (or a different address if the module has a different one, example):
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
The entry on x57 is apparently the EEPROM found on the module.
Add the following entry to /boot/firmware/config.txt
:
dtoverlay=i2c-rtc,ds3231
Reboot the pi and run the command again:
sudo i2cdetect -y 1
The address should now be marked as UU
, which indicates that the devices is being used by a system driver (example):
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- 57 -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Checking if it really works can be a bit tricky. The simplest way is having the display attached and the script automatically running, because it shows date and time on the display. Make sure it is not connected to the network or internet by removing the Wi-Fi adapter or ethernet cable if you are using one of those, or just by leaving your Wi-Fi range when you are using onboard Wi-Fi.
Shut down the Pi and wait some time before booting it up again. If the time shown is relatively close to a reference clock, the modules was wired and configured correctly.