Configure I2C

The Raspberry Pi CPU incudes a hardware I2C peripheral, among other things, but this is not activated by default (latest Raspbian version – January 2014). In order to activated type:

sudo nano /etc/modules

and a simple text file will open using the nano editor.

You have to add two lines after the end of the text. Those lines are: i2c-bcm2708 and i2c-dev. The final result will be like the one on the picture below

Press Ctrl-X and then Y to store all the changes. Now, type:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

This is the final file that we must edit in order to activate the I2C module of the Raspberry Pi CPU. By reading the file we can recognise that both the I2C and SPI modules are deactivated, or in blacklist, by default. To undo that, just place one # before those lines like here:

Again, press Ctrl-X and then Y to store all the changes. After editing the files, you will need to reboot for the changes to take effect. If you don’t do that then you are going to face problems with your I2C slaves letter on! To do the reboot type:

sudo reboot

After the reboot finish and you are again on the terminal line type:

sudo apt-get install python-smbus

Press Y when you get promed about that and wait for the installation process to finish. By doing all the previous steps you have full access to the I2C hardware module of the Raspberry Pi CPU and you can use it from a Python program.

 
 

I2C slave discovery tool

In the same I2C bus you can connect multiple slave devices (sensors, IO expanders, micros, GSM modules etc) and address them by using their unique ID. This ID, can often be set by pulling low or high individual pins of the slave I2C devise or by changing jumper settings in case of a module type configuration.

There is a tool to find out which devices are connected to your RPi I2C bus and the address that are carrying. To use this tool first you have to instal it. Type:

sudo apt-get install i2c-tools

Now, you can use the:

sudo i2cdetect -y 1

to scan for connected slave devices. If you are using one of the very first Raspberry Pis, the ones with the 256MB of memory, then you must change the 1 on the end of the command to 0. This is because the Raspberry Pi designers swapped over I2C ports between those models.

PS: The I2C slave addresses numbering on the previous capture is not counting the last bit (the R/W one) of the address. In other words the least significant bit (LSB) of the address is the bit 1 and not the bit 0. This is normal if you follow the I2C general numbering scheme but confusing for a new user. For example the 77 (value in hexadecimal format) is actually 0xEF if you want to read or 0xEE if you want to write by counting all the bits from 0 t0 7 (including the R/W one)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: