|
UPDATE: Adafruit shows how to interace with the DS1307 RTC without a level converter in their Adding a Real Time Clock to Raspberry Pi tutorial
Yesterday, I described how to access the Raspberry Pi's serial console via the GPIO header. Another capability that can accessed from that header is the I2C bus. I2C stands for Inter-Integrated Circuit and is common serial bus for chips to talk to each other. Although not written for the Pi, Tronixstuff's Arduino and the I2C bus is a good introduction to how the bus works. Here's a short video from NXP that the tutorial highlights:
Unlike most computers, the Pi lacks a real-time clock (or RTC). This means the Pi doesn't know what time it is when powered on. If connected to a network, then the Pi could retrieve the current time from a time server (referred to as NTP). Another option is to connect an external real-time clock to the Pi via the I2C bus. I already had a DS1307 Real Time Clock breakout board kit from Adafruit:
However, the DS1307 operates at 5V and requires a logic level converter to safely connect to the Pi's 3.3V I2C pins. Furthermore, since I2C is bi-directional, a logic level converter should be choosen which explicitly states it will work with I2C. I choose Adafruit's 4-channel I2C-safe Bi-directional Logic Level Converter:
From the eLinux wiki, the GPIO header pinout show the I2C lines are on GPIO 0 (SDA) and GPIO 1 (SCL):
SDA is the Serial Data line, and SCL is the Serial Clock line. Here is a photo of the RTC board connected via the logic level shifter to the Pi:
(From Left to Right: Adafruit 4-channel I2C-safe logic level converter, Adafruit DS1307 RTC board, Adafruit 8-channel logic level converter [unused], SparkFun logic level converter board [connected to FTDI cable for serial console])
Raspberry Pi Pin | Jumper Wire | Logic Level Converter: LV side (3.3V) |
Logic Level Converter: HV side (5V) | Jumper Wire | DS1307 RTC |
---|---|---|---|---|---|
3V3 Power | Red | LV | HV | Red | 5V* |
Ground | Green | GND | GND | Black | GND* |
GPIO 0 (SDA) | Yellow | A3 | B3 | Yellow | SDA |
GPIO 1 (SCL) | Blue | A4 | B4 | Blue | SCL |
*NOTE: The 5V FTDI cable provides 5V power to the DS1307. The DS1307 "5V" pin is connected to "VCC" on the 5V FTDI cable, and the DS1307 "GND" pin is connected to "GND" on the 5V FTDI cable. Alternatively, the DS1307 could instead be powered by the 5V pin on the Pi's GPIO header.
Here's a close up of the wiring:
(From Left to Right: Adafruit 4-channel I2C-safe logic level converter, Adafruit DS1307 RTC board, TMP102 temperature senor [more info in a future post], SparkFun logic level converter board [connected to FTDI cable for serial console])
I'll now turn my attention to the software side now that the wiring is done. bootc.net a terrific resource for I2C on Pi. First, download the bootc.net Debian Wheezy image which includes v3.2 of the Linux kernel and I2C drviers. Boot the Pi from that image and execute these simple commands from I2C and the Raspberry Pi (as root):
Register the DS1307 RTC chip with the I2C address of 0x68:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
View the current date & time stored in the RTC:
hwclock -r
Set the Linux system time to the value in the RTC:
hwclock -s
Here's screenshots of the process:
These commands could be added to a startup script so that the Pi retrieves the current date/time from the RTC while booting up.
Finally, the DS3231 is a higher precision I2C RTC than the DS1307. The ChronoDot by Macetech features this chip. It also has the advantage of being able to operate at 3.3V, so a logic level converter isn't needed.
Cheers,
Drew
Product Name* | Description | Supplier | ||
Raspberry Pi Model B | Raspberry Pi model B | Raspberry Pi |
| |
Optical mouse | Basic USB optical mouse | IONE / Pro Signal |
| |
Raspberry Pi power supply | 120-240v to 5V power supply with micro USB connector | Raspberry Pi |
| |
Keyboard | Basic USB keyboard | Gear Head / A4 Tech |
| |
Pre-programmed 4GB SD card | 4GB Class 4 SD card preloaded with Debian 6 Linux | Samsung |
| |
DS1307 Real Time Clock Breakout | DS1307 I2C real time clock on breakout board | Adafruit |
| |
4ch Logic Level Converter | 4 channel 5V to 3.3V bidirectional logic level converter | Adafruit |
| |
Adafruit Pi Cobbler | GPIO breakout kit for Raspberry Pi | Adafruit |
| |
Breadboard | Basic breadboard with 830 connection points | Twin Industries |
| |
Jumper wire bundle | Jumper wires for use on breadboard | Bud Industries |
|
*Products and resources listed are listed to help members build their own Pi Projects. They are suggestions and listed for educational purposes. For substitutions of any parts, please post a question asking the original author.
Top Comments