Introduction: Raspberry Pi I2C (Python)
In this instructable, I will explain how to use I2C on the Pi, with the examples of the CMPS03 compass module and SRF08 Ultrasonic range, using python. I will explain right through installing the OS, to ensure that the dependencies and everything is installed.
I2C is a communication bus designed by Philips, for chips to communicate with each other on a PCB. It is commonly used, however, for connecting sensors, such as the two examples later in this instructable and port expanders, because you can have multiple devices on the same two pins.
I2C is a communication bus designed by Philips, for chips to communicate with each other on a PCB. It is commonly used, however, for connecting sensors, such as the two examples later in this instructable and port expanders, because you can have multiple devices on the same two pins.
Step 1: Install R-Pi Image
Go to the Raspberry Pi website, and download the latest Raspbian image and follow the instructions burn it to the SD card.
http://www.raspberrypi.org/downloads
There is an easy setup guide on the wiki, just follow it through.
When you have got it installed, run the config tool, and get everything going nicely. In my case, I am running it headless via SSH, which is enabled as default, at pi@192.168.0.X (check on your router to find the IP).
http://www.raspberrypi.org/downloads
There is an easy setup guide on the wiki, just follow it through.
When you have got it installed, run the config tool, and get everything going nicely. In my case, I am running it headless via SSH, which is enabled as default, at pi@192.168.0.X (check on your router to find the IP).
Step 2: Enable I2C
On the Pi, I2C is disabled by default. The first thing to do, is run the command sudo nano /etc/modprobe.d/raspi-blacklist.conf . In this file, there is a comment, and two lines. Add a hash before the I2C line, to comment it out.
Original:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
blacklist i2c-bcm2708
Convert to this:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
#blacklist i2c-bcm2708
Original:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
blacklist i2c-bcm2708
Convert to this:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
#blacklist i2c-bcm2708
Step 3: Enable Kernel I2C Module
The next thing to do is add the I2C module to the kernel. Run the command sudo nano /etc/modules .You should see the following file:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
This should have the line i2c-devadded to the end.
Final file:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
This should have the line i2c-devadded to the end.
Final file:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev
Step 4: Install Necessary Packages
There are a few packages that will need installing to use I2C. The first command to run is sudo apt-get install i2c-tools. If this fails, try running sudo apt-get update and try again, else run crying to your nearest nerd. The other package needed can be installed by running sudo apt-get install python-smbus.
To configure the software, we will add the Pi user to the I2C access group, by running the command sudo adduser pi i2c.
Now run sudo reboot to reboot, and test the new software.
To test the software, run the command i2cdetect -y 0 to see if there is anything connected. On my setup, it returned this output, because there was nothing connected:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
To configure the software, we will add the Pi user to the I2C access group, by running the command sudo adduser pi i2c.
Now run sudo reboot to reboot, and test the new software.
To test the software, run the command i2cdetect -y 0 to see if there is anything connected. On my setup, it returned this output, because there was nothing connected:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Step 5: Example 1: CMPS03 Compass Module
We now have everything ready to start using I2C!
To use the CMPS03 compass module, connect the power to V+ and 0V, from the Pi. I used the 5V line, which they recommend not doing because it might damage your pi, It worked for me, and has caused now damage, but I am not responsible if your's fries.
Then, connect the SDA and SCL lines to the Pi SDA and SCL, and you are ready to roll. The wiring diagram is shown at http://www.robot-electronics.co.uk/htm/cmps3tech.htm.
When you have connected it, run the command "i2cdetect -y 0". In my case, this returned:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
This shows that the module is on address 0x60. You then need the following python file:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x60
def bearing255():
bear = bus.read_byte_data(address, 1)
return bear
def bearing3599():
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() #this returns the value to 1 decimal place in degrees.
bear255 = bearing255() #this returns the value as a byte between 0 and 255.
print bearing
print bear255
time.sleep(1)
This program should be saved as anything, but add ".py" on the end. Then, run the command with sudo python whateveryoucalledit.p and you should get values written to your screen in a long list.
To use the CMPS03 compass module, connect the power to V+ and 0V, from the Pi. I used the 5V line, which they recommend not doing because it might damage your pi, It worked for me, and has caused now damage, but I am not responsible if your's fries.
Then, connect the SDA and SCL lines to the Pi SDA and SCL, and you are ready to roll. The wiring diagram is shown at http://www.robot-electronics.co.uk/htm/cmps3tech.htm.
When you have connected it, run the command "i2cdetect -y 0". In my case, this returned:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
This shows that the module is on address 0x60. You then need the following python file:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x60
def bearing255():
bear = bus.read_byte_data(address, 1)
return bear
def bearing3599():
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() #this returns the value to 1 decimal place in degrees.
bear255 = bearing255() #this returns the value as a byte between 0 and 255.
print bearing
print bear255
time.sleep(1)
This program should be saved as anything, but add ".py" on the end. Then, run the command with sudo python whateveryoucalledit.p and you should get values written to your screen in a long list.
Step 6: SRF08 Range Sensor
The second example is the SRF08 range sensor, with built in light sensor.
Wire it in in exactly the same way as before, with power, SDA and SCL connected to the Pi. I found that this sensor would not work off 3.3V, but again, I bear no responsibility for you putting 5V through your Pi pins. You can even leave the compass module in as well, because I2C can handle multiple devices on one line. The wiring diagram can be seen here: http://www.robot-electronics.co.uk/htm/srf08tech.shtml .
Run i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
Note that I have left the compass module connected.
You will then need the following python file. It is more complex, becuase you have to write a command to the sensor to get it to begin reading.
import smbus
import time
bus = smbus.SMBus(0)
address = 0x70
#SRF08 REQUIRES 5V
def write(value):
bus.write_byte_data(address, 0, value)
return -1
def lightlevel():
light = bus.read_byte_data(address, 1)
return light
def range():
range1 = bus.read_byte_data(address, 2)
range2 = bus.read_byte_data(address, 3)
range3 = (range1 << 8) + range2
return range3
while True:
write(0x51)
time.sleep(0.7)
lightlvl = lightlevel()
rng = range()
print lightlvl
print rng
This will print the light level on the built in light sensor and the current range, in cm.
Wire it in in exactly the same way as before, with power, SDA and SCL connected to the Pi. I found that this sensor would not work off 3.3V, but again, I bear no responsibility for you putting 5V through your Pi pins. You can even leave the compass module in as well, because I2C can handle multiple devices on one line. The wiring diagram can be seen here: http://www.robot-electronics.co.uk/htm/srf08tech.shtml .
Run i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
Note that I have left the compass module connected.
You will then need the following python file. It is more complex, becuase you have to write a command to the sensor to get it to begin reading.
import smbus
import time
bus = smbus.SMBus(0)
address = 0x70
#SRF08 REQUIRES 5V
def write(value):
bus.write_byte_data(address, 0, value)
return -1
def lightlevel():
light = bus.read_byte_data(address, 1)
return light
def range():
range1 = bus.read_byte_data(address, 2)
range2 = bus.read_byte_data(address, 3)
range3 = (range1 << 8) + range2
return range3
while True:
write(0x51)
time.sleep(0.7)
lightlvl = lightlevel()
rng = range()
print lightlvl
print rng
This will print the light level on the built in light sensor and the current range, in cm.
Step 7: Conclusion
I hope you have found this instructable useful, as it should provide you with the code you need to get I2C working nicely. I spent a long time trying to fathom the Adafruit I2C Library out, before realising that these simple commands are all that I need. The basic read and write commands are functions in my provided code, so that should see you through.