In order to use certain I2C sensors, such as the BNO055, BNO085 and the CCS811, you'll need to enable I2C clock stretching 'support' by greatly slowing down the I2C clock on the Raspberry Pi using the device tree overlay.

This is done by adding a line in /boot/config.txt.  Log in to a terminal on your Pi and open that file in Nano, or your text editor of choice:

sudo nano /boot/config.txt

Scroll down until you find a block like:

# Uncomment some of all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
dtparam=i2s=on
dtparam=spi=on

This block might vary depending on what you've enabled in raspi-config. Directly below it, add the following:

# Clock stretching by slowing down to 10KHz
dtparam=i2c_arm_baudrate=10000

Clock stretching is used by certain peripheral devices to signal to the Raspberry Pi to give it more time to respond, but the Raspberry Pi's hardware I2C doesn't support this feature. However, by slowing down the bus speed, it should give the peripheral more time.

The default baudrate may be 100KHz or 1MHz, by slowing it down to 10KHz or more, you may be able to be slow enough to avoid missing clocks.

In Nano, your screen should look like this:

Next, save the file and exit (in Nano, press Ctrl-Xy for yes, and Enter).

Now you can reboot your Pi and proceed to testing your I2C device:

sudo reboot

If you still get bad data, try slowing it down more, maybe to 5 KHz or 1 KHz rate. Reboot after each change

This guide was first published on Jun 30, 2018. It was last updated on Mar 19, 2024.

This page (I2C Clock Stretching) was last updated on Mar 08, 2024.

Text editor powered by tinymce.