Adding I²C Peripherals

I²C is a two-wire serial interface commonly used in digital electronics. It allows multiple peripheral chips to be easily connected using only a few shared signal lines. Many useful I²C peripherals exist including digital-to-analog and analog-to-digital converters, servo drivers, LCD display drivers, I/O expanders and numerous sensors.

Raspberry Pi offers an I²C bus for convenient way to interface the Pi to the outside world. The Raspberry Pi Linux distributions support the I²C bus as well as specific drivers for quite a few I²C peripherals.

RPi900 I²C Connector

Raspberry Pi exposes the I²C bus via the GPIO header. RPi900 uses the bus to connect the real-time clock, but also passes the signals through to connector footprint J5. The connector pinout is described here; you can also see labels for the pins on the underside of the RPi900 board.

The pitch of the connector footprint is 0.1” and should fit many types of connectors including standard 0.1” pin headers and sockets. I recommend terminal blocks for easy connection and removal. Connect the two signals SDA and SCL, as well as signal ground and 3.3 V supply if required. (A maximum current of 50 mA may be drawn from the supply.) No pull-up resistors are required as these are already present on the Raspberry Pi board.

Using I²C Devices

On the Raspberry Pi, a simple sanity check can verify that your I²C device is correctly connected to the device. In the following example, an I²C bus scan shows the RPi900 real-time clock chip connected at address 0x68. Any other connected I²C devices would be indicated at their respective addresses.

$su
#pacman -S i2c-tools  # install i2c-tools package
#modprobe i2c-dev     # load i2c-dev module
#sudo i2cdetect -y 1  # detect i2c chips on bus 1
     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 i2c-tools package also provides commands for accessing registers on a given I²C chip. For example, to read and write registers of the PCF8523 real-time clock:

#i2cget -y 1 0x68 0x03          # read the 'Seconds' register
0x08
#i2cset -y -r 1 0x68 0x0E 0x81  # write the 'Offset' register and read back
Value 0x81 written, readback matched

If Bash isn’t your thing, several I²C interface libraries are available for various programming language, as detailed in the Raspberry Pi wiki. WiringPi seems popular, with wrappers for Ruby, Python and Perl as well as the native C/C++ interface. And there is always good old C.