How to use I2C

I2C (I-squared-C) is a communication technique used between multiple integrated circuits. The Micromelon Rover can use the I2C protocol to communicate with other I2C-compatible devices. This can expand the rover's capabilities, like using external sensors or controlling additional servos.

How I2C works:

Before using I2C on the rover, it is recommended to understand how it works at a basic level.

Credit: Scott Campbell - Basics of the I2C Communication Protocol

I2C communication is used between a master and a slave IC (integrated circuit). The clock line (SCL) synchronises the timing of the data sent over the data line (SDA). The data can be sent back and forth between master and slave, but this cannot happen simultaneously. We refer to this as half-duplex. Multiple slaves and masters can be used with I2C, but all require a unique address.

Credit: Scott Campbell - Basics of the I2C Communication Protocol

When a master wishes to send a packet of information to a slave, it starts by sending a 7-bit address through SDA. This is used to establish which slave is being communicated with. A master can write data to a slave or read data from a slave. The processes for these are slightly different, so we'll go through an explanation for both of them.

I2C devices have an address, which is used as an identifier. They also have registers, which can be described as different locations inside the device containing information that can be read from or written to.

Writing to an I2C slave:

Credit: Texas Instruments - Understanding the I2C Bus

When writing to an I2C device, the master first sends the start signal, followed by the 7-bit address of the slave. Next comes the read/write bit, 0 if the master will write and 1 if it will read. The master waits until the slave sends an acknowledge (ACK) bit on the SDA line. Then the master sends through the register address it wishes to write to. Again, the slave must acknowledge the request. The master then transmits through the data, with the slave sending an ACK on each byte. The stop condition is sent to end the transmission.

Start and End condition - To signal a start, SDA is changed from high to low while SCL stays high. SDA changes from low to high while SCL remains high to signal a stop.

Read and Write bit - This specifies whether the master wants to read or write data. Read is a 1, and write a 0.

ACK/NACK bit - These signal the device has received or has not received a byte of data. ACK is 0 and NACK is 1.

Reading from an I2C slave:

Credit: Texas Instruments - Understanding the I2C Bus

Reading from an I2C device is similar to writing. The master first sends the start signal, followed by the 7-bit address of the slave. Next comes the read/write bit, and then the master waits until the slave sends an acknowledge (ACK) bit on the SDA line. The master next sends through the register address it wishes to read from and waits until the slave acknowledges on the SDA line. This is where reading and writing start to differ.

The master sends the start condition again, followed by the slave's address and a read bit. Once the slave acknowledges, it will start sending a byte of data. The master acknowledges each byte of data and finally sends a negative acknowledgment (NACK) to signal the slave to stop sending. The stop condition is sent to end the transmission.

I2C on the rover:

Fortunately, the protocols are taken care of when using I2C on the rover. To wire an I2C device correctly to the rover, use the pinout below and connect SDA and SCL to the respective SDA and SCL on the device you are connecting to.

Once connected, the coding is pretty simple regarding reading, writing and scanning.

I2C Scan:

This code returns a list containing the addresses of all I2C devices currently connected to the rover.

I2C Read:

This block reads from a slave device. You must specify the slave address, the register to read from, and the number of bytes to read. This code returns a list containing each byte of data read from the slave device. The data you wish to read can be greater than 1 byte.

I2C Write:

This block writes to a slave device. You must specify the slave address, the register to write to, the data you wish to write, and the data length in bytes. The data you wish to write can be greater than 1 byte.

Example uses:

Rover with PCA9685 Servo Driver:

The PCA9686 servo driver is a device that allows 16 servos to be controlled using I2C communication. With this device attached, the rover can control up to 18 servos.

Rover with Arduino Nano:

The Arduino Nano is a highly adaptable microcontroller with many I/O ports. The default I2C ports on the Nano are A4 (SDA) and A5 (SCL). The rover can use I2C to easily communicate with an Arduino. This enables the rover to access all of the I/O ports on the Nano and use these as external sensor ports. In the example above, the rover uses an Arduino Nano to receive data from a line sensor array. For more info on this project, click the button below:

Previous
Previous

Build Guide: Ping Pong Shooter

Next
Next

Build Guide: RoboRAVE Line Follow