Configuring I2C

Configuring I2C

http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c

I2C is a very commonly used standard designed to allow one chip to talk to another. So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable chips and modules.

Here are some of the Adafruit projects that make use of I2C devices and modules:

 

If you are using Occidentalis, then your Pi is ready to go with I2C as far as enabling the hardware goes. However, if you are using Raspbian, you will need to open LXTerminal and enter the following command:

Copy Code
sudo nano /etc/modules

and add these two lines to the end of the file:

Copy Code
i2c-bcm2708 
i2c-dev
editing_modules_file.png

After editing the file, you will need to reboot for the changes to take effect.

If you have problems with I2C on Raspbian, then it is well worth updating to the latest version. These instructions were tested with the release dated 2012-10-28.

The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with a unique address, that can often be set by changing jumper settings on the module. It is very useful to be able to see which devices are connected to your Pi as a way of making sure everything is working.

To do this, it is worth running the following commands in the Terminal to install the i2c-tools utility.

Copy Code
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Depending on your distribution, you may also have a file called /etc/modprobe.d/raspi-blacklist.conf

If you do not have this file then there is nothing to do, however, if you do have this file, you need to edit it and comment out the lines below:

Copy Code
blacklist spi-bcm2708
blacklist i2c-bcm2708
.. by putting a # in front of them.

Open an editor on the file by typing:

Copy Code
sudo nano /etc/modprobe.d/raspi-blacklist.conf
.. then edit the file so that it appears as below, and then save and exit the file using CTRL-x and Y.
blacklist.png

Once this is all done, you can type the following command to see all the connected devices (if you are running a 512MB Raspberry Pi Model B)

Copy Code
sudo i2cdetect -y 1
i2c-detect.png

This shows that two I2C addresses are in use – 0×40 and 0×70.

Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi Model B) then you will need to change the command to:

Copy Code
sudo i2cdetect -y 0

The Raspberry Pi designers swapped over I2C ports between board releases. Just remember: 512M Pi’s use i2c port 1, 256M ones use i2c port 0!