Thursday, 20 September 2012

Enabling I2C on a Raspberry Pi

Using an SD card with Raspbian “wheezy”...

Enable I2C in modprobe:
Have a look at the /etc/modprobe.d/raspi-blacklist.conf file, comment out the two lines:
blacklist spi-bcm2708
blacklist i2c-bcm2708

Load the i2c kernel drivers:
sudo modprobe i2c-dev

Now check that the i2c devices have appeared in /dev:
ls -l /dev/i2c*
crw-rw---T 1 root i2c 89, 0 Sep 20 19:43 /dev/i2c-0
crw-rw---T 1 root i2c 89, 1 Sep 20 19:43 /dev/i2c-1

Install the i2c tool chain:
sudo apt-get install i2c-tools

Add your user to the i2c usergroup
This is so you don't need to use sudo to interact with the i2c device.
sudo usermod -aG i2c yourusername

We can now use the i2cdetect command to determine what devices are on one of the  I2C buses
pi@raspberrypi ~ $ i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --
We can see that I have a device at address 0x48 on bus 0 (see my tutorial here for further info).

2 comments:

  1. On the later "Made in the UK" versions of the pi you need to use "i2cdetect -y 1" rather than "i2cdetect -y 0" to use /dev/i2c-1. Hope this helps someone!

    ReplyDelete