-
Notifications
You must be signed in to change notification settings - Fork 34
Multiple I2C buses
When a sensor has only one I2C address and a number of those sensors should be connected, then a software I2C bus can be used, because most of them allow to be used multiple times.
It is often done by creating multiple objects, each with a different set of pins for SDA
(serial data) and SCL
(serial clock).
As long as each I2C sensor or I2C slave is used one by one, there is often no problem. When they are used in a multitasking code or used non-sychronous/mixed mode (another bus is used, while the previous bus has not finished yet) it depends on the software library if that will work.
Some software I2C libraries use fast pin changing by writing directly to the registers. If changing the mode of a pin is done with two commands, then the interrupts should be turned off while doing so.
With multiple I2C buses, it is possible to share a single SCL
(clock) pin. Thus reducing the total number of used pins.
When the SDA
(serial data) goes low, a beginning of a start condition is issued.
However, when the SDA
stays high and the SCL
goes low, nothing happens. That means that the SCL
(serial clock) can do anything, and as long as SDA
(serial data) stays high, the I2C devices (the sensors, the I2C slaves) do not react. It is therefore possible to use the same SCL
(serial clock) pin for multiple I2C buses.
It is even possible to use the SCL
(serial clock) pin for something entirely unrelated to I2C.
So far no one has implemented this.
When many sensors with many I2C buses are used, reading data from the sensors is slow. When all the sensors are the same, a software I2C library could have parallel I2C buses and read and write all of them at the same time. This is such very specific use, that no one has implemented it yet.
The Arduino Uno has a number of pins in the same register. PortC and PortB have both 6 pins on the Arduino board. That makes it possible to read and write 6 I2C buses at the same time.