Web Banner

Project - Arduino I2C Setting the Bus Speed

Setting the I2C bus speed was not quite as easy as I thought. When I started using the I2C interface I assumed that I could adjust the speed of the I2C bus using the code below

But, when I checked the signal using a oscilloscope I found that the actual bus speed was not changed. After further investigation I found two things:

1: Some libraries set their own bus speed when being initialized.

2: Most libraries use the default bus speed and do not allow it to be changed.

Adafruit SSD1306 Library

This is only library that I have fount, that hides it's adjustment of the I2C bus speed. In the code below you can see that at the end of the line where the display object is initialized I have added two extra parameters (both 400000). These parameters are not used in the example script.

The first of the parameters sets the I2C clock speed to use for the display message exchange. The second parameter is the I2C speed that is set after any display message has been sent. This causes great problems since it will keep adjust the I2C bus speed every time a display message is sent...

The safest approach is to set both of these parameters to the same value that you are using as the default Wire bus speed.

Setting the default Wire I2C Bus Speed

To set the default I2C bus speed I had to change a file called 'twi.h'

The location of this file will change depending of the board and driver version you are using.

'C:\Users\XXXX\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src\utility'

If you open this file you will find #define TWI_FREQ 100000L

Change this value to your desired clock speed in Hz. If you are using the Adafruit SSD1306 library, just make sure to change those values to match the value you set here.

If you have any questions, please add a comment to the youtube video.

Arduino Libraries

Library Name Author Version
Adafruit BMP085 Library Adafruit 2.2.4
Adafruit BMP280 Library Adafruit 2.6.8
Adafruit BusIO Library Adafruit 1.15.0
Adafruit GFX Library Adafruit 1.11.9
Adafruit HMC5883 Unitied Adafruit 1.2.3
Adafruit SSD1306 Adafruit 2.5.9
Adafruit Unified Sensor Adafruit 1.1.14
Dallas Temperature Miles Burton, Time Newsome, Guil Barris, Rob Tillaart 3.9.0
GY521 Rob Tillaart 0.5.2
OneWire Jim Siudt, Tom Pollard, Robin James, Glenn Trewitt, Jason Dangle, Guillermo Lovato, et all. 2.3.7
QMC58883LCompass MPrograms 1.2.3
RDV GY-512 Library Gagan Singh 1.0
RTClib Adafruit 2.1.3