How to Enable i2c on the Raspberry Pi

i2c is a communication protocol that runs over a two wire bus. The two wires are called SDA (Serial Data) and SCL (Serial Clock). The i2c bus has one or more masters (the Raspberry Pi) and one or more slave devices, like the LSM9DS0 on the BerryIMU. As the same data and clock lines are shared between multiple slaves, we need some way to choose which device to communicate with. With i2c, every device has an address that each communication must be prefaced with.

How to enable i2c on the Raspberry Pi

pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get install i2c-tools libi2c-dev python-smbus

You will then need to comment out the driver from the blacklist. Currently the I2C module isn’t being loaded.

pi@raspberrypi ~ $ sudo nano /etc/modprobe.d/raspi-blacklist.conf

Place a hash ‘#’ in front of blacklist i2c-bcm2708
If the above file is blank or doesn’t exist, then skip the above step

You now need to edit the modules conf file.

pi@raspberrypi ~ $ sudo nano /etc/modules

Add these two lines;

i2c-dev
i2c-bcm2708

Update /boot/config.txt

pi@raspberrypi ~ $ sudo nano /boot/config.txt

Add to the bottom;

dtparam=i2c_arm=on
dtparam=i2c1=on

 

Now reboot.

Once your Raspberry Pi reboots, you can check for any components connected to the i2c bus by using i2cdetect;

pi@raspberrypi ~ $ sudo i2cdetect -y 1

Or port 0 on the older Raspberry Pi

pi@raspberrypi ~ $ sudo i2cdetect -y 0

A table like the table below will be shown and if any divices are connected, thier address will be shown. Below you can see that a device is connected to the i2c bus which is using the address of 0x6b.

pi@raspberrypi ~ $ sudo /usr/sbin/i2cdetect -y 1

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

I2C and Python

If you plan on using I2C in python, then you will need to install the Python bindings;

pi@raspberrypi ~ $ sudo apt-get install python-smbus

Blip, blop, bloop…