---
title: "How to use I2C"
date: "2024-12-31"
categories: ["Guides"]
tags: ["Guide", "Guides"]
excerpt: "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 se"
featuredImage: "/images/resources/how-to-use-i2c.PNG"
---

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.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/7105257a-8f69-4c42-8498-75b065115db2/Master+Slave.png)

*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.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/bd5ffc0f-d3df-430e-a20a-a506ba1c2d11/master+slave+address.png)

*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:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/2a49148c-a641-46db-a765-7588404e5d58/write.PNG)

*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:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/d6fe46c3-e792-419e-b9d5-ca54564328fb/Read.PNG)

*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.

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/d8d56175-6268-4994-926b-ea7a29a7304e/Rover+pinout.png)

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

#### I2C Scan:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/d59b5379-adae-44a7-9ae1-4cb4dd1399fb/rover+scan.PNG)

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

#### I2C Read:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/abcd637b-0760-4eb6-b89f-b27acc4538f5/rover+read.PNG)

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:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/382021b8-521a-4018-bd3c-4baa6b8015d5/rover+write.PNG)

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:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/92d26c4d-a024-482f-8a9a-058b24e983b3/IMG_1365.JPG)

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:

![](https://images.squarespace-cdn.com/content/v1/60a43bf842d7b601064a8828/554bb5b9-e49a-4423-bf4d-2e1f0640c737/IMG_1366.JPG)

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:

    [
      Showcase: Rover and Arduino Line Follow
    ](showcase-rover-and-arduino-line-follow)
