RTC Pi on a Raspberry Pi Raspbian Stretch
A tutorial showing how to use the RTC Pi on a Raspberry Pi under Raspbian Stretch.
Created: 12/11/2015 | Last Updated: 09/06/2019
This page details how to setup the RTC Pi on the Raspbian Jessie or Stretch image from https://www.raspberrypi.org/downloads/raspbian/
Step 1: With your Raspberry Pi switched off install the RTC Pi on the Raspberry Pi GPIO port and insert a CR2032 coin battery into the battery holder. Using the RTC Pi without a battery installed may damage the RTC Pi and will stop it from appearing on the I2C bus.
Step 2: Follow the instructions on how to install and configure I2C on Raspbian Linux.
Step 3: Install i2c-tools using:
sudo apt-get install i2c-tools
Step 4: Once you have installed I2C check that the RTC Pi has been detected using:
sudo i2cdetect -y 0 # (if using v1 Raspberry Pi or)
sudo i2cdetect -y 1 # (if using v2 Raspberry Pi)
The RTC Pi should appear on channel 68 as shown in the screen shot below. If the RTC Pi does not appear check that the battery is installed correctly and is fully charged:
Step 5: Edit config.txt to add the following: dtoverlay=i2c-rtc,ds1307
sudo nano /boot/config.txt
At the end of the file add
Step 6: Add the module to /etc/modules:
sudo nano /etc/modules
Add at the end of the file
Save your changes
Step 7: Next edit edit /lib/udev/hwclock-set
sudo nano /lib/udev/hwclock-set
Commend out the following lines with #
#if [ -e /run/systemd/system ] ; then
Reboot the Raspberry Pi.
If this is the first time you have run the RTC Pi it will display a date of January 1st 2000.
Step 8: If the Raspberry Pi is connected to the internet the correct date and time should be set automatically otherwise you can set the current date and time using:
sudo date -s "2 OCT 2015 18:00:00"
You can check the current linux date with the command (date). To save the date onto the RTC Pi use the following command:
sudo hwclock -w
Verify the date has been saved onto the RTC Pi with:
sudo hwclock -r
If everything worked correctly the RTC Pi should be initialised on boot and the current date and time will be loaded into Linux.
Step 9: If for some reason the date is not loaded from the RTC Pi on boot, you can get around this problem by creating a script which runs when the Raspberry Pi boots.
Create a script called hwclock in the /home/pi folder.
sudo nano /home/pi/hwclock
Add the lines below:
Save the file and set the permissions using chmod to make the script executable.
sudo chmod 755 /home/pi/hwclock
Next we will use crontab to run the hwclock script on boot.
sudo crontab -e
If this is the first time you have used crontab it may display the following message asking you to select an editor.
Select an editor. To change later, run 'select-editor'.
2. /bin/nano <---- easiest
Choose 2 to open the file in nano.
At the end of the file add the following line.
@reboot /home/pi/hwclock &
Save and reboot your Raspberry Pi.
The hwclock script should run when your Raspberry Pi boots loading the date from the RTC into Linux. You can check that the date is correct using the date command.