RPi enabling I2C communication

I have considered to use a combination of an Arduino Uno and RPi for my Home Automation Project.
After vigorous research(Googling of cause) on how I am to interface the two, I came to a conclusion that using I2C was the best option as compared to using UART(which required Logic Level Switching as the RPi runs on 3V3 and Arduino on 5V), or as compared to USB communication.

The idea is to have the Arduino doing all Analogue detections and calculations such as a temp sensor, LDR, IR and etc. And also including extra GPIO’s for additional controls.

Now back to the I2C, in this case the RPi will function as the Master and Arduino as the Slave.
Hint: The RPI is running on 3V3 while the Arduino is running at 5V. I read some tutorials where they used a level shifter for the I2c communication. This is NOT needed IF the RPI is running as master and the Arduino is running as slave device. You can use the I2C bus without level shifter as only the master will push the data line high!

Make sure your Raspberry Pi is connected to the internet when installing the drivers.

The new Raspbian distro already have the I2C driver installed but they are disabled by default.
We need to download additional drivers and enable I2C.


sudo apt-get update
sudo apt-get install i2c-tools

Note : The installation could take a few minutes to do, depend on how busy the server is.

Now add a new user to the i2c group:

sudo adduser pi i2c

Install the python-smbus python module:


sudo apt-get install python-smbus 

Now you are ready to use the i2c with python


sudo cp /etc/modprobe.d/raspi-blacklist.conf /etc/modprobe.d/raspi-blacklist.conf.bak

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


# blacklist spi and i2c by default (many users don't need them)

blacklist spi-bcm2708
blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804


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

Remove I2C from the Blacklist:


# blacklist spi and i2c by default (many users don't need them)

#blacklist spi-bcm2708
#blacklist i2c-bcm2708
blacklist snd-soc-pcm512x
blacklist snd-soc-wm8804

Press CTRL X then y to save and exit.

Next edit the modules file by:

sudo nano /etc/modules

Add the following to a new line.

i2c-dev 
i2c-bcm2708

Press CTRL X then y to save and exit.

Run command to activate i2c-dev:

sudo modprobe i2c-dev

Reboot the Pi by:

sudo shutdown -r now

After the reboot test to see any device connected by:

ls -l /dev/i2c*

You should get something like this:

crw-rw---T 1 root i2c 89, 1 Dec 19 09:30 /dev/i2c-1

Now we run a simple test, scan the i2c bus:

sudo i2cdetect -y 0

If your board is the Rev 2 type this:

sudo i2cdetect -y 1

You should see something like this:

sudo i2cdetect -y 1

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

Reboot the Pi by:

sudo shutdown -r now

Hint: if you’re using the first revision of the RPI board, use “-y 0″ as parameter. The i2c bus address changed between those two revisions.

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