I2C Installation for Raspberry Pi – Step by Step Guide

07/08/2012

This is a step by step guide on installation I2C driver for the Raspberry Pi. It is for the Raspbian image.

Raspberry Pi Starter Kits

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. To enable it all you need to do is comment out a line by putting # in front. At the prompt type.

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

then add a # on the 3rd line.

Press CTRL X then y to save and exit.

Next edit the modules file by:

sudo nano /etc/modules

Add i2c-dev to a new line.

Press CTRL X then y to save and exit.

Now install the i2c-tools package by:

sudo apt-get install i2c-tools

If you get a 404 error do an update first:

sudo apt-get update

then run the install the i2c-tools again.

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

Reboot the machine by:

sudo shutdown -r now

After the reboot test to see any device connected by:

sudo i2cdetect -y 0

If your board is the Rev 2 type this:

sudo i2cdetect -y 1

You should see something like this:

Next install the  python-smbus python module:

sudo apt-get install python-smbus 

Now you are ready to use the i2c with python.

Example

http://www.skpang.co.uk/blog/archives/454

Download the led chaser python file by:

wget http://www.skpang.co.uk/blog/wp-content/uploads/2012/06/ledchaser.py

Change the file permission to allow execute:

chmod 777 ledchaser.py

Run the example by:

./ledchaser.py

 

Short URL:

Tags: , ,

25 Responses to I2C Installation for Raspberry Pi – Step by Step Guide

  1. pine on 08/08/2012 at 09:13

    Excellent. I had to work my way through this the hard way (with considerable help from Nathan C – thanks!).

    • pt on 22/08/2012 at 09:05

      How did you do it? I seem to have downloaded the software ok, but when I run the program I get: IOError: [Errno 5] Input/output error.
      I’m not sure that my pins are in the right place, as I find the difference between the photos/schematic/datasheet for MCP23017 confusing.
      I asked for clarification on the ledchaser page, but no reply.
      I have had the ladder game and tux crossing working ok.

      • pine on 01/09/2012 at 14:53

        Hi pt, Looking at the drogon.net pages, I see neither the ladder game nor the tux crossing game use the MCP23017, so could be the problem is the way you’ve wired it up. I assume you can ‘see’ the MCP23017 if you run i2cdetect -y 0 from the RPi terminal prompt (as above on this page)? You should do this before trying to run the ledchaser.py program. I sometimes get the error when I forget to re-connect the 5v lead back to the breadboard (having disconnected it when not using the breadboard).

        If you cannot see the MCP23017 with i2cdetect, check your connections. I think he schematic is now pretty clear, if you use it with the pin-out diagram on the MCP23017 data sheet. Perhaps simplifying things by only connecting one or two leds at first might help. Once these flash, you can add the rest to get the chasing.

        Hope this helps, Pine

        • Rich Kavanagh on 13/10/2012 at 13:45

          This doesn’t work on a Revision 2 Pi – any ideas why that might be?

          I have this working perfectly on my Revision 1 Pi, but if I unplug it and hook it up to my Rev2 (using the same SD card), it doesn’t work at all.

          Running “i2cdetect -y 0″ shows no device, where usually (on the Rev1 Pi) we’d see the grid showing “20″, but the results are all empty on the Rev2 Pi.

          If I try and run ledchaser.py, I get the following error:

          File “./ledchaser.py”, line 16, in
          bus.write_byte_data(0×20,0×00,0×00) # Set all of bank A to outputs
          IOError: [Errno 5] Input/output error

          Any ideas why the “i2cdetect -y 0″ command can’t find the MCP23017 on a Rev2 board but it can on a Revision 1?

          Or more importantly, anyone know how I can make this work on a Revision 2 Pi?

          Thanks :)

          • Rich Kavanagh on 13/10/2012 at 13:57

            Ok, I found out why this doesn’t work on a Rev2 board and more importantly, how to fix it :)

            Edit your ledchaser.py file by typing:

            sudo nano ./ledchaser.py

            Find this line:

            bus = smbus.SMBus(0) to bus = smbus.SMBus(0)

            Replace it with this line:

            bus = smbus.SMBus(0) to bus = smbus.SMBus(1)

            CTRL+x to exit and save your changes.

            What you’re essentially doing is altering the bus location of the MCP23017.

            Thanks to Nathan Chantrell for the blog post at http://nathan.chantrell.net/20120602/raspberry-pi-io-expander-board/

          • Jeff Henning on 25/02/2013 at 14:11

            Had the same problem myself, since most of the examples out there were for RPi r1.

            Revision 2 of RPi moved the i2c-0 pins to the P5 bank. The old i2c pins on P1 are now the i2c-1 pins (P1-1 = 3.3V; P1-3 = SDA; P1-5 = SCL; P1-6 = GRND). See article

            http://elinux.org/RPi_Low-level_peripherals

            This is why using these pins, your i2c device will show up using:

            i2cdetect -y 1 {the last parameter indicates i2c-0 or i2c-1)

            This is why rich is using:

            bus = smbus.SMBus(1)

  2. Thomas Macpherson-Pope on 24/10/2012 at 10:51

    For the LED chaser, could you not have connected the MCP23017 to 3v3 and not bothered with all those resistors?

  3. admin on 24/10/2012 at 10:59

    The Raspberry Pi have limited available current on the 3.3v supply.

  4. Ray on 24/12/2012 at 17:25

    Hi
    I know this is not the same chip but I have been trying to connect a TMP102 to the i2c bus
    i2cdectect finds the device at 0×48 but i2cget -y 0 0×48 0×00 b gives a read error
    Also tried using Python but I get a read error
    I have tried another TMP102 – new SD card with new load of Wheezy
    Has anybody get any ideas
    Thanks
    Ray

    • helmut505 on 18/02/2013 at 18:47

      Have you tried
      i2cget -y 1 0×48 0×00 b
      instead?
      That worked for me.

  5. Paul on 26/01/2013 at 01:37

    What if i2cdetect returns all zeros and doesn’t show any port being active? Everyting else seems to run fine on my raspberry, but I cannot seem to get i2c runn ing. I using the occidentalis distro.

    Thanks

    • Name on 19/02/2013 at 19:31

      Have you switched the SDA and SDC connection? if the table shows up softwarewise eerything should be “good to go”. so maybe it isn’t properly wired. check the datasheet for further reference but my best guess its the SDA and SDC cable.

      • Eric Bell on 13/02/2014 at 19:39

        I was having the same issue (all zeros showing up with i2cdetect -y 1). I wired and rewired the chip several times. No luck. But, after a while, instead of grounding the 3 address pins, I put A2 to 3.3v When I did that and ran i2cdetect -y 1, it showed up on my i2c list.

  6. Chris on 07/04/2013 at 22:24

    Hi all, im impressed i eventually managed to get this to work, a couple of things i was hoping somebody here could help me with, i have three MCP23017′s and a MPR121 capacitive touch sensor and was wondering how i would connect all of these, i have a basic understanding of electronics. when i was trying to get this to work i tried a 5v and gnd connection of the MCP23017 to a voltage reg i made and this didnt seem to work could anybody tell me why? i also tried originally connecting the MCP23017 to Rpi pin 2 for 5v and Rpi pin 25 for gnd, this didnt work, however when i used Rpi pin 2 for 5v and pin 9 for gnd it did. can anybody tell me why this happened? pin 9 worked but pin 25 didnt even though they should both be gnd pins?
    thank-you in advance
    Chris

  7. mpolaczyk on 16/06/2013 at 18:25

    Hi,
    how to make smbus package visible to python3 ?
    I get this exception when I try to execute my code:

    pi@raspberrypi ~/test/mpolaczyk/Sensor $ sudo python3 main.py

    ImportError: No module named smbus

    Marcin

  8. jaymie on 21/06/2013 at 11:38

    High,

    I get the error
    Import error:no module named smbus
    as well.

  9. Latik on 06/12/2013 at 07:19

    hi… while doing above steps I have on installing i2c tools it is showing error as something about ‘lock’ and after command ‘sudo i2cdetect -y 0 or 1′ it is showing command error as ‘there is such code’..
    please suggest me

    • admin on 06/12/2013 at 08:32

      Can you please post exactly the error here.

  10. Dsolle on 25/03/2014 at 23:53

    Hello,

    I am also getting import error: no module named smbus. Same as mpolaczyk and Jammie. Please help us!

    Thanks

    • Dave Bolt on 26/05/2014 at 15:11

      Did you install the python-smbus library first? Something like:
      sudo apt-get install python-smbus

      • Hendrik on 21/09/2014 at 01:06

        Getting the same error. python-smbus is installed and on latest level.
        Any idea where python is looking for the modules?
        The error is shown as:
        Traceback (most recent call last):
        File “./light.sh”, line 4, in
        import smbus
        ImportError: No module named smbus

        In addition python complains about each line with the print statement. Syntax error at the end of the line.
        File “./light.sh”, line 14
        print “Power ON”
        ^
        SyntaxError: invalid syntax

        thanks, Hendrik

        • Hendrik on 21/09/2014 at 09:26

          Two comments:
          The print statements are working if the quoted string is set in brackets.
          Print seems now a function.

          The module smbus that gets installed is for python 2.7 but not for python3x. This module seems incompatible with python3.

          See this tread: http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=22348

          But this is from 2012. The status of this module may have changed.

          Thanks, Hendrik

  11. Mario sandoval on 18/01/2015 at 14:42

    hi. i have this problem with /etc/modules
    I saved on editor Press CTRL X then y to save and exit.

    Next edit the modules file by:

    sudo nano /etc/modules

    the screen displayed “error writing etc/modules: no such file or directory”
    I don’t know fix the problem
    (Rasperry no me deja crear el directorio modules

  12. Hibi on 29/05/2015 at 10:19

    Hello,
    I have a project that requires to install this library and then use it to publish the values of a sensor. Does anybody know what is the next step after installing the library? I am using a raspberry pi and a seed studio sensor.
    Thanks

  13. khaled khnissi on 03/06/2015 at 15:00

    I execute the program and I got this error help me please

    Traceback (most recent call last):
    File “./ledchaser.py”, line 16, in
    bus.write_byte_data(0×20,0×00,0×00) # Set all of bank A to outputs
    IOError: [Errno 5] Input/output error

Leave a Reply

Your email address will not be published. Required fields are marked *

*