Enabling The I2C Port

The I2C port needs to be enabled in Rasbian before it can be used. See here.

Checking For Connected Devices

At the command prompt type one of these depending on whether you are using the I2C0 or I2C1 port:


sudo i2cdetect -y 0
//or
sudo i2cdetect -y 1

The 7 bit I2C address of all found devices will be shown (ignoring the R/W bit, so I2C address 0000 0110 is displayed as hex 03).

 

SMBus (System Management Bus) Functions

SMBus (System Management Bus) is a subset from the I2C protocol
When writing a driver for an I2C device try to use the SMBus commands if possible (if the device uses only that subset of the I2C protocol) as it makes it possible to use the device driver on both SMBus adapters and I2C adapters.

Note address is the 7 bit address excluding the read / write bit (it will be shifted left 1 bit when added to the read/write bit)


long write_quick(int addr)

Send only the read / write bit


long read_byte(int addr)

Read a single byte from a device, without specifying a device register.


long write_byte(int addr,char val)

Send a single byte to a device


long read_byte_data(int addr,char cmd)

Read Byte Data transaction.


long write_byte_data(int addr,char cmd,char val)

Write Byte Data transaction.


long read_word_data(int addr,char cmd)

Read Word Data transaction.


long write_word_data(int addr,char cmd,int val)

Write Word Data transaction.


long process_call(int addr,char cmd,int val)

Process Call transaction.


long[] read_block_data(int addr,char cmd)

Read Block Data transaction.     


write_block_data(int addr,char cmd,long vals[])

Write up to 32 bytes to a device.  This fucntion adds an initial byte indicating the length of the vals array before the valls array.  Use write_i2c_block_data instead!


long[] block_process_call(int addr,char cmd,long vals[])

Block Process Call transaction.     

I2C Access Functions


long[] read_i2c_block_data(int addr,char cmd)

Block Read transaction.


write_i2c_block_data(int addr,char cmd,long vals[])

Block Write transaction.

Code Example


#!/usr/bin/python

import smbus

bus = smbus.SMBus(1)    # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

DEVICE_ADDRESS = 0x15      #7 bit address (will be left shifted to add the read write bit)
DEVICE_REG_MODE1 = 0x00
DEVICE_REG_LEDOUT0 = 0x1d

#Write a single register
bus.write_byte_data(DEVICE_ADDRESS, DEVICE_REG_MODE1, 0x80)

#Write an array of registers
ledout_values = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
bus.write_i2c_block_data(DEVICE_ADDRESS, DEVICE_REG_LEDOUT0, ledout_values)

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. dpjanes

    9 months ago

    Is there any way to get a notification or to poll / select for when data is available?

  2. Mike Rawlings

    9 months ago

    Very good but idle 3 cant’t find smbus when importing but idle 2.6 can. Upgrade as of 18 May 2014. Any ideas?

  3. forhelp

    7 years ago

    Did you install the library?

    Try:

    sudo apt-get install python-smbus

  4. Gary

    7 years ago

    i get 2 lines with uu on them when i run i2cdetect im new to this and learning as i go (very slowly) could any ont tell me what they represent

    1. John

      9 months ago

      It means that address is currently being used, which in the docs for i2cdetect means it’s “highly likely” that a chip exists at that address. you can run >man i2cdetect to read about it

  5. KeithB

    7 years ago

    Hi, an other newbee here, I have the i2c installed as above but it only works from the command line. When I try the python it say’s ‘ImportError: No module named smbus’.
    Is this a permission problem? How do I fix it?

    1. Parag

      7 years ago

      apt-get install i2c-tools python-smbus.

    2. KeithB

      7 years ago

      Thanks Parag, the system replied that i2c-tools is already the newest version. i2c-tools set to manually installed. python-smbus is already the newest version. 0 updated, 0 newly installed, 0 to remove and 179 not updated.
      Found an other site which says that i2c smbus does not work on python3 so, I tried it on python 2.7 and it worked!

    3. Rudi Ananta Kurniawan

      7 years ago

      sure!
      let me check the link?
      I have problem import smbus on python 2.7..
      and i try
      sudo i2cdetect -y 1
      just new line nothing info

      any suggestions?

  6. Static

    7 years ago

    I’m using write_i2c_block_data and I’m getting an error:
    NameError: name ‘ledout_values’ is not defined
    Has anyone actually tried the example code as written with success?

  7. Sandy

    6 years ago

    Is there a way to read 4 bytes from a device, without specifying a device register?

    Thanks!

  8. bigbuilder

    6 years ago

    I think the problem lies with the path to the library within the IDE you are using. With python, if something works on the command line, and fails in an IDE, generally it’s an IDE related issue. I personally gave up on IDEs after suffering for years. I use an editor and then run python at the command line.

  9. sam

    6 years ago

    Is there a way to read a block of data, lets say 8 byte, without specifying a register or command?

    1. paul

      5 years ago

      sam did you find a way ?

  10. dealwizit

    5 years ago

    no shit sherlock!!1 go fk iurself

  11. Christophe Nabil Nicolas El-Kh

    5 years ago

    So the methods you’re using are predefined within the smbus class we imported?
    also, what are the DEVICE_REG_MODE1 and DEVICE_REG_LEDOUT0 used for?
    I’m trying to use the Pi as a Master to communicate with a 18F4550 MicroChip being the slave to display data via an LCD connected to the MicroChip, so could you kindly explain based on that? Thank you!

  12. Anthony

    4 years ago

    As it stands, smbus is only compatible with up to python 2.7

  13. polis256

    9 months ago

    Is it posible to set Raspberry pi 3 B as I2C slave? I find some information how to set it but i do’t understend how it works ((

  14. Mathias

    8 months ago

    long[] read_i2c_block_data(int addr,char cmd)

    should be:
    long[] read_i2c_block_data(int addr, int length)

Comments

Your email address will not be published.