Basic i2C Setup on Raspberry Pi
My Furry Radio uses two i2c modules to extend it's i/o capabilities.
Here is how to get the basic i2c connectivity going in 12 easy steps.
Relevant to "2012-12-16-wheezy-raspbian"
1. sudo nano /etc/modprobe.d/raspi-blacklist.conf
This file contains a list of blacklisted device drivers. one of the drivers we want (i2c-bpm2708) is blacklisted, so take it out of the blacklist by commenting out the line with it's name on the file should end up looking something like:
# blacklist spi and i2c by default (many users don't need them)
# modified to allow i2c
blacklist spi-bcm2708
#blacklist i2c-bcm2708
2. sudo nano /etc/modules
# modified to allow i2c
blacklist spi-bcm2708
#blacklist i2c-bcm2708
2. sudo nano /etc/modules
We need make sure the i2c module gets loaded every boot, which is done in this file. add the module to the end of the list, it should end up looking something like:
# /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
3. sudo apt-get update
4. sudo apt-get install i2c-tools
5. sudo apt-get install python-smbus
6. sudo modprobe i2c-bcm2708
7. sudo modprobe i2c-dev
8. sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -
# /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
3. sudo apt-get update
4. sudo apt-get install i2c-tools
5. sudo apt-get install python-smbus
6. sudo modprobe i2c-bcm2708
7. sudo modprobe i2c-dev
8. sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -
Here we have installed all the necessary software that was not in the original image, loaded the correct modules into the kernel (without a reboot) and run the i2c utility as root. the output shows is detected one device at address 20, which is great because that's what's plugged in.
9. sudo adduser pi i2c
10. logout
11. <<log back in>>
12. i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -
10. logout
11. <<log back in>>
12. i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -
Now so you don't need to be root to access the device - a new group "i2c" was created for you when the tools were installed. add your user to this group (int this case the user is "pi", then log out and back in again for this to have effect. You can now access i2C as a normal user.
Errors
If you follow these steps it should all work out smoothly, however here's some errors you may see.
.udevdb or .udev presence implies active udev. Aborting MAKEDEV invocation.
Ignore this one.
Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory
Kernel module is not installed, Need to modprobe or restart (6, 7, 10)
Can't exec "i2cdetect": Permission denied at ...
User has no permission to access the device. add the user to the i2c group and you need to log out and in again to fix group permissions (9, 10)
No comments:
Post a Comment