Author Archives: Philip

rtc (ds3231) on a raspberry pi

Sometimes you raspberry pi (or other boards: arduino, olimex) has no Internet access but you want to keep time between reboots.  A rtc (real time clock) will do the job.

  1. First, get one!  They can be found on popular sites and ebay.  The DS3231 is a bit more accurate than the other rtc’s but they all will do the job.
    https://www.adafruit.com/products/264
    http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=ds1307&rt=nc&LH_BIN=1
    http://www.ebay.com/sch/i.html?_odkw=ds1307&LH_BIN=1&_osacat=0&_from=R40&_trksid=p2045573.m570.l1313.TR11.TRC2.A0.H0.Xds3231.TRS0&_nkw=ds3231&_sacat=0
    https://www.olimex.com/Products/Modules/Time/MOD-RTC/
  2. when you have an rtc module you have to connect with you raspberry.  You can find a picture of the raspberry pi pinout here http://pinout.xyz/pinout/i2c
    – rtc pin GND > raspberry pin 9 (GND)
    – rtc pin VCC > raspberry pin 1 (VCC)
    – rtc pin SDA > raspberry pin 3 (SDA)
    – rtc pin SCL > raspberry pin 5 (SCL)
  3. now that everything is connected, you still need to enable the rtc.  To do so , open /boot/config.txt :
sudo nano /boot/config.txt

now you’ve opened the file, put this text at the bottom and save the file.

dtoverlay=i2c-rtc,ds3231
dtoverlay=i2c-rtc,ds1307

this will load the correct modules for the rtc at boot time.
The next  step will read the clock at boottime, you do this by editing the /etc/rc.local file

sudo nano /etc/rc.local

Add this text before exit 0:

hwclock -s

it will look like this:

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi 
hwclock -s
exit 0

now save the file and reboot.

Login and check if the rtc is working, type:

sudo hwclock -D

if everything goes well, the output will be something like this:

hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 1451215751 seconds after 1969
Last calibration done at 1451215751 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2015/12/27 13:39:28
Hw clock time : 2015/12/27 13:39:28 = 1451223568 seconds since 1969
Sun 27 Dec 2015 14:39:28 CET  -0.856325 seconds

If this is the first time you use your rtc, you need to save the time to it.
Check if your system time is correct by typing:

date

the time will show up (this is mine when I was writing the article):
Sun, 27 Dec 2015 14:34:11 +0100
If this the correct time show’s up, you are ready to save the time to your rtc.

this can be done by typing:

hwclock -w -D

the following will show up (time&date may differ)

hwclock from util-linux 2.25.2
Using the /dev interface to the clock.
Last drift adjustment done at 1451215751 seconds after 1969
Last calibration done at 1451215751 seconds after 1969
Hardware clock is on UTC time
Assuming hardware clock is kept in UTC time.
Waiting for clock tick...
/dev/rtc does not have interrupt functions. Waiting in loop for time from /dev/rtc to change
...got clock tick
Time read from Hardware Clock: 2015/12/27 13:44:53
Hw clock time : 2015/12/27 13:44:53 = 1451223893 seconds since 1969
missed it - 1451223892.505637 is too far past 1451223892.500000 (0.005637 > 0.001000)
1451223893.500000 is close enough to 1451223893.500000 (0.000000 < 0.002000)
Set RTC to 1451223893 (1451223892 + 1; refsystime = 1451223892.000000)
Setting Hardware Clock to 13:44:53 = 1451223893 seconds since 1969
ioctl(RTC_SET_TIME) was successful.
Not adjusting drift factor because it has been less than a day since the last calibration.

this should be it! Your clock is set en ready for use!
If you should have question’s, leave a comment bellow of email me philip -at- vanmontfort.be