Using a DS3231 RTC With a Raspberry Pi: Difference between revisions
No edit summary |
Ttenbergen (talk | contribs) mNo edit summary |
||
(2 intermediate revisions by one other user not shown) | |||
Line 2: | Line 2: | ||
|shortDescription=How to add a real-time clock to the Raspberry Pi (Useful for devices without a network connection) | |shortDescription=How to add a real-time clock to the Raspberry Pi (Useful for devices without a network connection) | ||
|skillSet=Electronics; Raspberry Pi | |skillSet=Electronics; Raspberry Pi | ||
|featured= | |featured=Yes | ||
}} | }} | ||
Up until the Raspberry Pi 5, stock Pi boards didn't come with built-in RTC hardware (and the Pi 5 needs an external battery for its RTC). Normally this isn't a problem because they will attempt to sync with an NTP server periodically, but that's not possible if they don't have a network connection or the connection is particularly locked down. | Up until the Raspberry Pi 5, stock Pi boards didn't come with built-in RTC hardware (and the Pi 5 needs an external battery for its RTC). Normally this isn't a problem because they will attempt to sync with an NTP server periodically, but that's not possible if they don't have a network connection or the connection is particularly locked down. | ||
Line 12: | Line 12: | ||
To set up the Pi so the DS3231 is used: | To set up the Pi so the DS3231 is used: | ||
* Ensure the Pi is up to date | * Ensure the Pi is up to date | ||
* Run raspi-config and select | * Run <code>sudo raspi-config</code> and select <code>Interface Options -> I2C</code> and select "Yes" to enable the I2C interface. | ||
* | * Run <code>sudo apt instsall python3-smbus i2c-tools</code> | ||
* Reboot | * Reboot | ||
* Edit the /etc/modules file, add | * Edit the <code>/etc/modules</code> file, add <code>rtc-ds1307</code> to the bottom of the file. | ||
* Shut down the Pi and remove power. | * Shut down the Pi and remove power. | ||
* Connect the DS3231 to the Pi. Some DS3231 modules are built to drop directly onto the GPIO pins but check the pinouts first just in case. (NOTE: These are PIN numbers, not GPIO numbers!): | * Connect the DS3231 to the Pi. Some DS3231 modules are built to drop directly onto the GPIO pins but check the pinouts first just in case. (NOTE: These are PIN numbers, not GPIO numbers!): | ||
{| class="wikitable" | |||
|+ DS3231 to RPi Connection | |||
|- | |||
! DS3231 Pin !! Raspberry Pi Pin | |||
|- | |||
| VCC/Vin || 1 (3V3) | |||
|- | |||
| SDA || 3 (SDA) | |||
|- | |||
| SCL || 5 (SCL) | |||
|- | |||
| GND || 6 (GND) or 9 (GND) | |||
|} | |||
*Re-check connections | *Re-check connections | ||
*Apply power and boot the Pi | *Apply power and boot the Pi | ||
*Run i2cdetect -y 1 | *Run <code>sudo i2cdetect -y 1</code> | ||
*If you see a "68" appear in the chart, the DS3231 and Pi are communicating. If not, make sure the I2C bus is enabled and the pins are connected properly. | *If you see a "68" appear in the chart, the DS3231 and Pi are communicating. If not, make sure the I2C bus is enabled and the pins are connected properly. | ||
*Edit the /boot/firmware/config.txt file and add the following line to the end of the file: dtoverlay=i2c-rtc,ds3231 | *Edit the <code>/boot/firmware/config.txt</code> file and add the following line to the end of the file: <code>dtoverlay=i2c-rtc,ds3231</code> | ||
*Reboot the Pi | *Reboot the Pi | ||
*Run i2cdetect -y 1 | *Run <code>sudo i2cdetect -y 1</code> | ||
*If you see a "UU" in place of where the "68" was, the DS3231 driver is working. If not, check the previous steps. | *If you see a "UU" in place of where the "68" was, the DS3231 driver is working. If not, re-check the previous steps. | ||
* | *Run <code>sudo apt remove fake-hwclock</code> to remove the fake-hwclock package. | ||
*Clear the fake-hwclock references from the startup scripts with update-rc.d -f fake-hwclock remove | *Clear the fake-hwclock references from the startup scripts with <code>sudo update-rc.d -f fake-hwclock remove</code> | ||
*Edit the following file /lib/udev/hwclock-set | *Edit the following file <code>/lib/udev/hwclock-set</code> | ||
*Comment out the following block of three lines: | *Comment out the following block of three lines: | ||
<nowiki>if [ -e /run/systemd/system ] ; then | |||
exit 0 | |||
fi</nowiki> | |||
*Ensure the current time on the Pi is correct | |||
*Write the current time to the DS3231 with sudo hwclock -w | *Ensure the current time on the Pi is correct (either connect it to a network to sync or set it manually). | ||
*Write the current time to the DS3231 with <code>sudo hwclock -w</code> | |||
*Shut down the Pi and remove power and any network connection. | *Shut down the Pi and remove power and any network connection. | ||
*Wait 5 minutes. | *Wait 5 minutes. | ||
*Power up the Pi and once it boots check the time. If everything is working properly, the time should be correct without a network connection. | *Power up the Pi and once it boots check the time. If everything is working properly, the time should be correct without a network connection. |
Latest revision as of 16:09, 2024 April 10
Up until the Raspberry Pi 5, stock Pi boards didn't come with built-in RTC hardware (and the Pi 5 needs an external battery for its RTC). Normally this isn't a problem because they will attempt to sync with an NTP server periodically, but that's not possible if they don't have a network connection or the connection is particularly locked down.
Adding an external RTC to the Pi is relatively simple, there are many different models out there, some of which are in HAT format, some connect directly to and cover just a few pins, and some need to be connected by wires to the appropriate pins.
The DS3231 is a relatively accurate device that is quite popular and not very expensive. It communicates with the Pi over the I2C bus.
To set up the Pi so the DS3231 is used:
- Ensure the Pi is up to date
- Run
sudo raspi-config
and selectInterface Options -> I2C
and select "Yes" to enable the I2C interface. - Run
sudo apt instsall python3-smbus i2c-tools
- Reboot
- Edit the
/etc/modules
file, addrtc-ds1307
to the bottom of the file. - Shut down the Pi and remove power.
- Connect the DS3231 to the Pi. Some DS3231 modules are built to drop directly onto the GPIO pins but check the pinouts first just in case. (NOTE: These are PIN numbers, not GPIO numbers!):
DS3231 Pin | Raspberry Pi Pin |
---|---|
VCC/Vin | 1 (3V3) |
SDA | 3 (SDA) |
SCL | 5 (SCL) |
GND | 6 (GND) or 9 (GND) |
- Re-check connections
- Apply power and boot the Pi
- Run
sudo i2cdetect -y 1
- If you see a "68" appear in the chart, the DS3231 and Pi are communicating. If not, make sure the I2C bus is enabled and the pins are connected properly.
- Edit the
/boot/firmware/config.txt
file and add the following line to the end of the file:dtoverlay=i2c-rtc,ds3231
- Reboot the Pi
- Run
sudo i2cdetect -y 1
- If you see a "UU" in place of where the "68" was, the DS3231 driver is working. If not, re-check the previous steps.
- Run
sudo apt remove fake-hwclock
to remove the fake-hwclock package. - Clear the fake-hwclock references from the startup scripts with
sudo update-rc.d -f fake-hwclock remove
- Edit the following file
/lib/udev/hwclock-set
- Comment out the following block of three lines:
if [ -e /run/systemd/system ] ; then exit 0 fi
- Ensure the current time on the Pi is correct (either connect it to a network to sync or set it manually).
- Write the current time to the DS3231 with
sudo hwclock -w
- Shut down the Pi and remove power and any network connection.
- Wait 5 minutes.
- Power up the Pi and once it boots check the time. If everything is working properly, the time should be correct without a network connection.