JS I2C Bus

I2C Bus

I2C Bus is a way to transfer data between 2 digital circuits. There is typically one master circuit that is in charge of initiating transfers. And then there are one or more slaves on the bus. The BrainPad MicroComputer is always the bus master. A slave can be an accelerometer or motor controller.

When a master talks to a slave, it needs to know the slave’s address. Each slave must have its own unique address. The master then commands the addressed slave to either read or write data. I2cBytes() is used for transferring the data. The function needs 2 arrays, one for the outgoing data and one for the incoming data.

BrainBot, for example, has headlights that can be controlled using 4 values sent over I2C. The slave address of the robot is 0x01, and then the 4 values must be 0x01 followed by the three color levels (Red, Green, Blue). Do not confuse the 0x01 in the first sent value with the slave address, which happens to also be 0x01.

This example will use the Red (reflected as 0xff), while keeping the Green and Blue in the off state (which is 0x00). By the way, these hex numbers are just how codes are used to present values. You can simply use 0 instead of 0x00 and 255 instead of 0xff.

let data = new Array(4);
data[0]= 0x01;
data[1]= 0xff;
data[2]= 0x00;
data[3]= 0x00;
BrainPad.I2c.Write(0x01,data,0, data.Length);

Restart Condition

Every I2C transaction starts with a START condition, then the slave’s address, then data, and finally the STOP condition. Some sensors require a continuous write-read transaction with no STOP in the middle. Those transactions require have a RESTART condition instead. When I2cBytes() is called with both read and write, it automatically sends RESTART apropiately.


BrainStorm

Where is the “bus” in I2C bus? Data is delivered down the wire by one as a stream of data. In computer world, this is called a data bus. What other busses are used around us? Think of how a Keyboard is wired to a PC.

Content Licensing
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023