Read I2C transaction of 2 bytes in Python

4.5k Views Asked by At

I'm trying to implement reading from I2C communication on Python. The problem that I've faced is that the sensor which I'm reading from answers with two bytes in one transaction. Is there a Python library which provides such a reading.

Here is the description of the sensor's answer: Sensor's I2c answer

1

There are 1 best solutions below

1
On

The library you're looking for is the smbus library. If you're looking to read just two bytes as you suggest, use read_word_data(int addr,char cmd). If you're using some sensor that returns more than 2 bytes, you'll have to use read_block_data(int addr,char cmd) and do some byte arithmetic.

Information on the library: http://www.raspberry-projects.com/pi/programming-in-python/i2c-programming-in-python/using-the-i2c-interface-2