Raspberry Pi Real Time Clock Module DS1338

Raspberry Pi Real Time Clock

The Raspberry Pi does not have a real time clock. This is ok if your Raspberry Pi is connected to the internet as it will automatically sync the clock to one of the NTP servers. If you use the RPi standalone and require a real time clock then you can add the functionality toe have the RPi real the date and time from an RTC module.

The RTC module we are going to use is our own RTC module based on the DS1338 real time clock chip. It runs at 3.3V so can be connected directly to the RPi I2C port pins and 3.3V power

DS1338 Real Time Clock Module (RTC)

Raspbian Wheezy kernel version 3.2 and above already include the required drivers for the DS1338/DS1307 RTC chip

Enable I2C and Install I2C Tools

If you don't already have I2C enabled and i2c-tools installed, then we must install i2c-tools. If you already do, you can skip this stage.

By default the Raspberry Pi Raspbian config doesn’t load the I2C modules at boot. To change this you need to comment out or delete the I2C entry from the following
file: /etc/modprobe.d/raspi-blacklist.conf

Add a # character to the beginning of each line and save the file

  #blacklist spi-bcm2708
  #blacklist i2c-bcm2708

Next, install ic2-tools

  sudo apt-get install i2c-tools

Then add the I2C functionality into the kernel with

  sudo modprobe i2c-dev
  sudo modprobe i2c-bcm2708

Enable RTC Driver

This specific RTC is a DS1338, which is supported by the DS1307 driver. We add support with:

  sudo modprobe rtc-ds1307

Next we can check to make sure the RTC module can be seen. Run i2cdetect with port 0 for RPi V1 or port 1 for RPi V2

  sudo i2cdetect -y 0

       0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
  00:          -- -- -- -- -- -- -- -- -- -- -- -- --
  10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
  70: -- -- -- -- -- -- -- --

The RTC module should appear at address 68

Add RTC Device at boot

To make the RTC module load at boot time we need to add a new device

  sudo bash
  echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
  exit	

Test the RTC module

We should now be in a position to test the RTC module. Run

  sudo hwclock -r

  hwclock: Warning: unrecognized third line in adjtime file
  (Expected: `UTC' or `LOCAL' or nothing.)
  Fri 11 Apr 2014 16:22:23 UTC  -0.686251 seconds

Ok, we have a date and time returned, but you may see a Warning error (you may not).

If you get the error it is due to a bug with the creation of the adjtime file. There should be a carriage return after the UTC line. Edit the file and add a carriage return.

  sudo vi /etc/adjtime

  0.0 0 0.0
  0
  UTC

Load the RTC module at boot

Finally, once we get everything working, we must ensure that all the modules are loaded at boot. We edit /etc/modules:

  sudo vi /etc/modules

we add the following lines:

  i2c-bcm2708
  rtc-ds1307
	

And to ensure that the device is created at boot and the time is set from the RTC, we edit /etc/rc.local :

  sudo vi /etc/rc.local

and add the following lines before ‘exit 0’ (change /i2c-0/new_device to i2c-1/new_device if you’re using rev2):

  echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
  hwclock -s
	

The next time you boot, it should have the correct time.

Setting the Date and Time

We need to set the right time set on the RPi. This is done automaticallyif you connect it up to Ethernet or Wifi - it will automatically set the time from the network.

But you can set it manually using the 'date' command, e.g.

  sudo date -s "11 APR 2014 16:51:00" 

Check that the date is set correctly

  date
  Fri Apr 11 16:51:02 UTC 2014

Once the date and time is correct you can write it to the RTC module with

  sudo hwclock -w

You can then verify it with

  sudo hwclock -r
 
  
Shopping Cart
0 items
 
DELIVERY/PAYMENT

Free Delivery Orders over £50

UK - £2.40 orders under £50
Airmail from £2.95 (+VAT)
More Details...

 
Brands
 
 Check out our videos