[otw_is sidebar=otw-sidebar-1]In the previous post I have discussed about how to enable the I2C serial communication bus in Raspberry Pi. That was quite easy you need just to type few commands in the terminal window and execute them to enable the I2C serial communication bus. In addition I have also installed the I2C tool that is used for communicating with the I2C device connected to the Raspberry Pi via I2C serial communication bus. With this tool you can talk to the I2C device through the terminal window.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi I2C Interfacing using Python

In this post I will discuss how to interface the I2C device with Raspberry Pi via I2C serial communication bus by using the Python shell in the Raspberry Pi. In order to enable the Python to talk to the I2C device you have to install the module called SMBus (System Management Bus). This module enables the user to write the code in python for communicating with the I2C device.
Follow the following simple steps to be able to communicate to the I2C device.

[otw_is sidebar=otw-sidebar-2]

Installing the SMBus:

Installing the SMBusAs it came out from the above discussion the first step in I2C communication between the Raspberry Pi and I2C device via I2C communication bus through the Python language is to install the SMBus. So enter the following command in the terminal window of the Raspberry Pi and then press enter.

sudo apt-get install python-smBus

After the execution of this command you will be able to write the I2C code in the Python shell.

Getting the Address of the I2C Slave:

[otw_is sidebar=otw-sidebar-3]

As we know the in the I2C communication there can be many master devices and number of slave devices. In our case the master device is definitely Raspberry Pi and a number of slave devices can be connected to the single I2C serial communication bus. Hence as described in the previous post it is important that you know the address of each slave device. It is not difficult as it also needs the execution of the single command in the terminal window. Type the following command in the terminal window and press enter.

i2c detect r 1

A chart will appear in the terminal window with the addresses of your slave devices connected to the I2C serial communication bus of the Raspberry Pi.
After this we are all set up and ready for writing the python module for communicating with the I2C device.

Python IDLE:

Now it is the time to write the code in the Python IDLE. For this go into the Raspberry Pi menu at the top left corner of the screen. Then click on the Python 3.0 IDLE and the window will appear in front of you. Here you can write the code. The whole scheme can be seen in the picture below:

 

For this post I am using the compass module wRaspberry Pi I2C Interfacing using Python,Getting the Address of the I2C Slavehich uses the I2C serial communication to talk to the master device which in our case is Raspberry Pi. The Raspberry Pi will collect the data form the compass module via I2C serial communication bus. You can copy and paste the code which I have made as follows but make sure that you are using the slave address specified for your slave device.

 

import smbus
import time
bus = smbus.SMBus(0)
address = 0 x 20
def bearing255():
bear = bus.rad_byte_data(address,1)
return bear
defbearing3599():
bear1 = bus.read_byte_data(address,2)
bear2 = bus.read_byte_data(address,3)
bear = (bear1<<8) + bear2
bear = bear/10.0
return bear
while true;
bearing = bearing3599()
bear255 = bearing255()
print bearing
print bear 255
time.sleep(1)

Notice here that I have imported the SMBus library here which we have recently installed. So now you might have understood why installing the SMBus module is necessary. Also notice the address I have used in the code which is specified for my slave device. In short this code will automatically collect the reading of the compass module via I2C serial communication through I2C serial communication bus.

Save the Python Module:

Now after you have written the code for Raspberry Pi to communicate through I2C serial communication using Python IDLE it is the time to run the code. You can either run the code through the Python IDLE window by clicking the RUN in the MENU or you can type the name of your file in the terminal window which will automatically run the I2C communication between the Raspberry Pi and the I2C device which in my case is the compass module. When

you are savingRaspberry Pi I2C Interfacing using Python,Raspberry Pi I2C pins Python t

he Python code remember to add the extension of “.py” at the end of the name.
After saving, open the terminal window and type the following command in the terminal window:

sudo python yourfilename.py

[otw_is sidebar=otw-sidebar-3]

Your raspberry pi will automatically start collecting the data from the compass module.
So that’s all for this post. As a summary in this post we have learned how to do the I2C communication between the Raspberry Pi and the I2C slave device using the python console.
There are number of other modules and ICs which uses I2C serial communication one common example is the EEPROM so it becomes important to understand what the I2C serial communication is and have hands-on experience on the this type of communication. You can talk to other I2C devices such as Relay Module with I2C interface by Raspberry Pi via I2C port.

raspberry pi i2c pins Pull-Up Resistors:

It is important here to mention that the SDA and SCA lines of the I2C bus are the open drain lines which means it is necessary pull them up using the pull up resistors before you sned or receive the data over these lines. So simply connect these lines to the resistor and then connect other terminal of the resistor to the power supply.
That is all for this post. I hope this post will be helpful for you. In the next post I will about the SPI interface in Raspberry Pi. So stay connected and enjoy learning.