Jump to content

Using a DS3231 RTC With a Raspberry Pi: Difference between revisions

From Squirrel's Lair
Page Creation
 
Ttenbergen (talk | contribs)
mNo edit summary
 
(3 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=No
|featured=Yes
}}
}}
Up until the Raspberry Pi 5, Pi stock 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.


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.
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. It communicates with the Pi over the I2C bus.
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:
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 "Interface Options", then "I2C", and select "Yes" to enable the I2C interface.
* Run <code>sudo raspi-config</code> and select <code>Interface Options -> I2C</code> and select "Yes" to enable the I2C interface.
- Install python3-smbus i2c-tools
* Run <code>sudo apt instsall python3-smbus i2c-tools</code>
- Reboot
* Reboot
- Edit the /etc/modules file, add "rtc-ds1307" to the bottom of the file.
* 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 (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!):
- DS3231 VCC to Pi Pin 1 (3v3)
{| class="wikitable"
- DS3231 SDA to Pi Pin 3 (SDA)
|+ DS3231 to RPi Connection
- DS3231 SCL to Pi Pin 5 (SCL)
|-
- DS3231 GND to Pi Pi 6 (GND)
! DS3231 Pin !! Raspberry Pi Pin
Some DS3231 modules are built to drop directly onto the GPIO pins but check the pinouts first just in case.
|-
- Re-check connections
| VCC/Vin || 1 (3V3)
- Apply power and boot the Pi
|-
- Run i2cdetect -y 1
| SDA || 3 (SDA)
- 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
| SCL || 5 (SCL)
- Reboot the Pi
|-
- Run i2cdetect -y 1
| GND || 6 (GND) or 9 (GND)
- If you see a "UU" in place of where the "68" was, the DS3231 driver is working. If not, check the previous steps.
|}
- Remove the fake-hwclock package so it won't interfere with the RTC (sudo apt remove fake-hwclock).
 
- Clear the fake-hwclock references from the startup scripts with update-rc.d -f fake-hwclock remove
*Re-check connections
- Edit the following file /lib/udev/hwclock-set
*Apply power and boot the Pi
- Comment out the following block of three lines:
*Run <code>sudo i2cdetect -y 1</code>
- if [ -e /run/systemd...
*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.
- exit 0
*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>
- fi
*Reboot the Pi
- Ensure the current time on the Pi is correct and either connect it to a network to sync or set it manually.
*Run <code>sudo i2cdetect -y 1</code>
- Write the current time to the DS3231 with sudo hwclock -w
*If you see a "UU" in place of where the "68" was, the DS3231 driver is working. If not, re-check the previous steps.
- Shut down the Pi and remove power and any network connection.
*Run <code>sudo apt remove fake-hwclock</code> to remove the fake-hwclock package.
- Wait 5 minutes.
*Clear the fake-hwclock references from the startup scripts with <code>sudo update-rc.d -f fake-hwclock remove</code>
- 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.
*Edit the following file <code>/lib/udev/hwclock-set</code>
*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 (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.
*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.

Latest revision as of 16:09, 2024 April 10


  • Cargo:


  • Categories:
  • Default form


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 select Interface Options -> I2C and select "Yes" to enable the I2C interface.
  • Run sudo apt instsall python3-smbus i2c-tools
  • Reboot
  • Edit the /etc/modules file, add rtc-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 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
  • 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.