Master-Slave Communication Protocol -> I2C – “Inter-Integrated Circuit”

Reading Time: 5 minutes

I2C Communication Protocol. So we have already read about Serial communication. Serial Communication is considered simple and better in comparison to other protocols like Bluetooth and USB. But it also has a downside in exchanging data like reading a digital sensor which requires another protocol to carry out this operation.

Don’t worry as we have plenty of protocols for a digital sensor.

Now as we already know that the board we have studied (STM 32 DISCOVERY BOARD) contains multiple sensors like an accelerometer, magnetometer, and gyroscope.

The accelerometer and magnetometer are packed in a single component which is accessible by an I2C bus.


What is I2C ??

I2C transmission

I2C stands for Inter-Integrated Circuit which is used for exchanging data bit by bit along a single wire (the SDA line).

I2C works on the Synchronous Serial Communication protocol. What is this now ???


Synchronous Serial Communication

Synchronous Serial Communication describes a serial communication protocol in which data is sent in a continuous stream at a constant rate.

It requires that the clocks in the transmitting and receiving devices are synchronized – running at the same rate – so the receiver can sample the signal at the same time intervals used by the transmitter. No start or stop bits are required

This type of communication takes place in the I2C protocol which uses two-way or lines to exchange data.

  1. A data line (SDA)
  2. A clock line (SCL)
**Clock line is used to synchronize the communication.

How I2C Works?

With the I2C communication protocol, data is transfer in form of messages. Messages are broken up into frames of data. Each message has an address frame that contains the binary address of the slave, and one or more data frames that contain the data being transmitted. The message also includes start and stop conditions, read/write bits between each data frame:

I2C Works

Start Condition: The SDA line switches from a high voltage level to a low voltage level before the SCL line switches from high to low.

Stop Condition: The SDA line switches from a low voltage level to a high voltage level after the SCL line switches from low to high.

Let’s know more about it

Each slave has its own unique 7 to 10-bit address which the master uses to identify them. Whenever the master wants to send data it first generates a request which has the particular address of that slave. The slave whose address get match responds to the master.

**I2C communication protocol has the ability to support communication between several devices.

Every message initiates with a start condition and ends with a stop condition. A single message can hold multiple data bytes, each having an acknowledge (ACK) or negative acknowledge (NACK) bit in between them.

Once the communication gets starts between the master and the slave, the bus status is set to busy and no other device can use the bus until the data is exchanged or the communication stops by the master.

What does the clock line (SCL) do here ??

The clock line determines the rate at which the data get exchange between the device (master) and sensor (slave).


Let’s go through the Master-Slave communication process step by step

*Master -> Slave

When the master SENDS data to the slave.

Master send slave
  • Master: Broadcast START
  • Master: Broadcast slave address (7 bits) + the R/W (8th) bit set to WRITE
  • Slave: Responds ACK (ACKnowledgement)
  • Master: Send one byte
  • Slave: Responds ACK
  • Repeat steps 4 and 5 zero or more times
  • Master: Broadcast STOP OR (broadcast RESTART and go back to (2))
**As we discussed before the address here can be 10 bits also.

The second operation can be when the Master wants to read the data from the slave.

*Master <- Slave

Here the only difference is the bit in the second step would be set to READ which tells that the master is in the reading mode now.

Master read slave
  • Master: Broadcast START
  • Master: Broadcast slave address (7 bits) + the R/W (8th) bit set to READ
  • Slave: Responds ACK (ACKnowledgement)
  • Master: Send one byte
  • Slave: Responds ACK
  • Repeat steps 4 and 5 zero or more times
  • Master: Broadcast STOP OR (broadcast RESTART and go back to (2))

Use of I2C in DISCOVERY Board

Two sensors the magnetometer and accelerometer of the DISCOVERY BOARD are present in a component, the component is LSM303DLHC.

So, here onboard these two sensors behave like the slave of a master, to access these sensors we use the I2C protocol.

Whatever data they get from sensing the environment is stored in their own memory so our task is simple to just read that memory and get the result.

A point to note here is that their memory is modeled as byte-addressable registers but they are not mapped into the microcontroller’s memory.

Therefore I2C access their registers.


The difference between the SPI (Serial Peripheral Interface) and I2C (Inter-Integrated Circuit)

  • I2C protocol can support multiple slave devices but unlike SPI, which only supports one master device, I2C can support multiple master devices as well
  • Every device sends/receives data using only one wire which is SDA. SCL maintains sync between devices through a common clock which is provided by the active master
  • I2C is slower than SPI
  • In I2C communication we get the acknowledgment bit after each byte but not in SPI
  • SPI Costly as compared to I2C
  • I2C is the address base bus protocol, you have to send the address of the slave for the communication whereas, in SPI, you have to select the slave using the slave select pin for the communication

The last point will be, what are the advantages and disadvantages of using I2C over SPI

Advantages:
  • Multiple masters and multiple slaves can be interfaced together.
  • By using two wires only we can make the communication.
Disadvantages:
  • It is slower as compared to SPI because this protocol does a lot of frameworks.
  • It consumes more power than other serial communication busses due to open-drain topology.
If you want to see the practical example of this theory then you can click here.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


Written by 

Nitin is a Software Consultant, with experience of more than 7 months. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.