I2C is a popular device protocol in communicating with microcontrollers. However, this protocol is not limited to Arduino, PIC and STM32; the Raspberr

How to Use I2C Devices with Raspberry Pi

submited by
Style Pass
2020-07-07 16:55:47

I2C is a popular device protocol in communicating with microcontrollers. However, this protocol is not limited to Arduino, PIC and STM32; the Raspberry Pi can also use I2C.

I am using Raspberry 4 Model B in this tutorial running Raspian 10 Buster. Some parts of this tutorial may not apply to your version of Raspberry Pi. To know your Raspbian version, run this on the shell:

This is I2C port 1 or simply I2C-1. There is another set of I2C pins (I2C-0) at 27 and 28 but they are reserved for communicating with EEPROMs found in Raspberry Pi HATs.

The main library for I2C programming with Python is SMBUS. However prior to programming, you must check if the device is recognized by the Raspberry Pi. One thing you can do is use:

This will show you a list of connected devices and their address. This is an example output when an MPU6050 attaches to the Raspberry Pi’s I2C bus:

roland@raspberrypi:~ $ sudo i2cdetect -y 1 [sudo] password for roland: 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: — — — — — — — — — — — — — — — — 10: — — — — — — — — — — — — — — — — 20: — — — — — — — — — — — — — — — — 30: — — — — — — — — — — — — — — — — 40: — — — — — — — — — — — — — — — — 50: — — — — UU UU UU UU — — — — — — — — 60: — — — — — — — — — 68 — — — — — — 70: — — — — — — — — roland@raspberrypi:~ $

Leave a Comment