As shipped, the Pi has no real-time clock …

As shipped, the Pi has no real-time clock, and depends on the internet to determine the current time. Since my Pi might run without internet, I added a real-time clock to it. It’s an off-the-shelf DS3231 (http://goo.gl/BKyjT9) unit with removable battery that plugs directly into the pi’s headers, tapping the i2c bus and 3.3V power and ground.To enable i2c on the Pi, so that it can read data from the RTC, you need to:

* comment the line “blacklist i2c-bcm2708” from /etc/modprobe.d/raspi-blacklist.conf
* add the lines  “i2c-bcm2708” and “i2c-dev” to /etc/modules
* reboot

The Pi takes a bit longer to boot up with i2c enabled. Once booted, you can see the devices it has detected on the i2cbus with:

sudo i2cdetect –y 0

My RTC showed up is device 0x68. Linux already has support for the DS3231 RTC clock, so to enable it:

echo ds3231 0x68 | sudo tee /sys/class/i2c-adapter/i2c-0/new_device

which logs:

i2c i2c-0: new_device: Instantiated device ds3231 at 0x68

You can then use “hwclock” to query the time. Initially it gave me some time in the year 2000. I plugged an ethernet cable into my Pi, waited for ntpd to sync, verified using the “date” command, and then set the time on the rtc using “hwclock -w” which sets the RTC based on the system clock.

You can make the Pi set the clock automatically on boot from the RTC by adding this to /etc/rc.local:

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

“hwclock -s” sets the system clock from the RTC clock. To test that it works, I unplugged the ethernet and rebooted. The “date” command showed the correct date. All fine!

For my upcoming experiment I need to be able to add more i2c devices, so I decided to move the clock out to a breadboard — my first time using one.

Since the RTC was designed to plug directly into the Pi’s headers, I needed similar header pins on the breadboard to mount the RTC. I had a long strip of breakable headers, so cut off 5 pins. At first, the pins kept popping out of the breadboard — the legs were too short — until I discovered you could actually slide the black plastic up a bit to make the legs longer.

I wired the 3.3V and GND to the power bus on the breadboard, and connected the RTC to the power bus, and also wired the I2C SDA (blue) and SCL (white) lines to the breadboard. Powered up everything and — yay — the clock still works!

#raspberryPi


The RTC has a DS3231 chip and mounts directly onto the Pi’s headers.

i2cdetect shows the clock as device 0x68

Moved the  RTC clock to a breadboard so I can tap other stuff to the i2c bus.

This post was originally published publicly on Google+ at 2015-01-10 03:08:14+0800

You may also like...