circuitprofessor.com

path to learn electronics

I2C Communication: A Comprehensive Guide to Understanding the Basics

ByMithila kumanjana

May 13, 2023
I2C communication

Welcome to the fascinating world of microprocessors and the fascinating I2C (Inter-Integrated Circuit) communication protocol they employ! In this article, we’ll begin on an exciting adventure to unravel the mysteries of I2C and learn how it enables devices to exchange information effectively. So buckle up as we explore the fascinating world of I2C communication.

What is I2C communication?

Inter-Integrated Circuit (I2C) is a synchronous, multiple-master, and multiple-slave communication protocol.

what is that mean?

The master and the slave are two crucial players in the world of electronic systems as well as in i2c communication. Imagine a master who begins and controls communication, and a slave as the subordinate who listens and follows the master.

Like a musician leading an orchestra, the master takes charge by starting data transfers, issuing commands, and requesting information. The master devices, on the other hand, wait for instructions before performing tasks or providing data.

Similar to a team, the master guides and schedules while the slave devices follow and carry out instructions.

only one master and multiple slaves

I2C communication

multiple masters and multiple slaves

I2C communication

It allows multiple electronic devices to communicate with each other, using just two wires: a clock line (SCL) and a data line (SDA).

SDA

 serial data line. Consider it as a pathway by the devices that transmit messages. SDA functions like a two-way virtual channel of communication where devices switch between speaking and listening.

A device uses the SDA line to send data when it needs to be in sync with the clock signal we’ll discuss this in a moment.

The data is able to be read and understood by other devices connected to the bus. Each device has a chance to share its information with other components of the system.

SCL

called the Serial Clock Line. Imagine SCL as the conductor who keeps everyone moving in sync. Similar to how an orchestra’s conductor determines the tempo, SCL sets the beat for inter-device communication.

It pulses on and off at a precise rate, creating a synchronized beat. A device always synchronizes its data transmission and reception on the SDA line with the clock pulses on the SCL line.

This avoids data conflicts and misunderstandings by ensuring that all communication devices move to the same beat.

you can find SDA and SCL pins from most of the devices.

arduino beginner guide…

How Does I2C Communication Work?

In an I2C setup, there can be multiple devices connected to the same bus.

 i2c communication

Each device has a unique address, enabling it to be identified during i2c communication.

let’s define some sample addresses for master devices and slave devices.

(master 1 = 0x00,master 2 =0x7F ,slave 1= 0x18,slave 2 =0x19,slave 3=0x20,slave 4=0x21)

 i2c communication

A master-slave relationship is used for communication, with the master starting and managing the data transfer.

Addressing and Data Transfer

The start signal is sent by the master, followed by the slave’s address, when it wants to connect with a particular slave device.

When the targeted slave is available, it responds to the master’s request.

There are two different ways to transfer data in i2c communication. the read and write.

if we consider the write mode the master transfers data to the slave and in the read mode the slave sends data to the master.

Start and Stop Conditions

An I2C communication session begins with the master device at the start condition.

The stop condition signals that the communication session has come to an end.

In order for the devices to synchronize and signal properly, these conditions must be fulfilled.

let’s talk about i2c communication Protocol Data Bit Arrangement

  • Start Bit

The first bit is where the communication trip starts. The start of a data transfer is indicated by a symbol similar to a green traffic light.

When the master device starts communication, it sends a start bit, which is a particular pattern on the bus that denotes the beginning of data transmission.

  • Address Bits

The address bits are the following. Consider them like home numbers that aid in the recipient’s identification by gadgets.

Similar to how every house on a street has a separate number, every device on the I2C bus has a distinct address.

The master communicates the slave’s address as a series of address bits whenever it wishes to connect with a particular slave device.

  • Read/Write Bit

We come to the read/write bit after the address bits. Whether the master desires to read data from the slave device (1) or write data to it (0) is indicated by this bit.

Similar to a note advising whether to read or respond to a letter, it is attached.

  • Data Bits

The data bits follow the read/write bit. The actual information being sent between the master and slave is transmitted by these bits.

It is like the information in a message that you want to send to a friend.

According to the devices and the data transmitted, the amount of data bits might change. often ranging from 8 to 16 bits.

  • Acknowledge (ACK) Bit

The acknowledge bit appears after the data bits have been delivered. This bit serves as the receiver’s about of confirmation of the receipt, confirming that the data has been successfully received.

In order to acknowledge reception, the receiver transmits an ACK bit by momentarily pulling the data line low.

If there was a mistake or the receiver was busy, it may also decide not to pull the data line low, which would indicate a NACK (Not Acknowledged) condition.

  • Stop Bit

The stop bit, which looks like a red traffic light and signals the conclusion of data transmission, is the last bit.

The stop bit is delivered by the master to end the communication and make it clear that no further data will be transmitted at this time.

these are the basic i2c communication Protocol Data Bit Arrangements.

let’s combine those bits and then communicate with masters and slaves according to i2c communication

i2c communication

The start bit is sent first. then the slave address, the slave’s internal register address, and finally the data. The stop bit is sent last.

now let’s jump to the practical example in i2c communication.

Let’s connect two arduino boards according to i2c communication. visit to Tinkercad to simulate the project

then we need to reed data in this sensor. Initially, the wires must be connected to the SDA and SCL pins.

according to arduino uno datasheet A4 (SDA), A5 (SCL).read more…

then we need to find the device address and internal register address. visit arduino original website to find the code.

i2c communication

then we need to use wire libraries in the coding part. visit this page to learn more about the “wire” library.

let’s break down the code.

in this example, we have to write two codes .on for the master and one for the slave.

Master code

#include <Wire.h>

int pushbutton=A0;

void setup()
{
  Wire.begin();
  pinMode(pushbutton,INPUT);
}

int x = 0;

void loop()
{
  Wire.beginTransmission(4); 
  x=digitalRead(pushbutton);
   Wire.write(x);                
  Wire.endTransmission();   
  delay(500);
}

This line starts the I2c communication transmission to another device with a unique address of 4

Wire.beginTransmission(4);

This line sends the value of “x” to the other device.

Wire.write(x);

This line ends the transmission to the other device.

 Wire.endTransmission(); 

Slave code

#include <Wire.h>
int led=13;
void setup()
{
  Wire.begin(4);                
  Wire.onReceive(receiveEvent); 
  Serial.begin(9600);
  pinMode(led,OUTPUT);
}

void loop()
{
  delay(100);
}


void receiveEvent(int howMany)
{
  int x = Wire.read();    
  Serial.println(x); 
 if (x==1)
 {
   digitalWrite(led,HIGH);
 }
 else
 {
   digitalWrite(led,LOW);
 }
 
  
}

This line sets up a function called “receiveEvent” to handle incoming data from other devices.

Wire.onReceive(receiveEvent);

When the microcontroller gets data from another device, this function is invoked. It accepts the option “how many,” which specifies how many bytes were delivered.

void receiveEvent(int howMany)
{
  int x = Wire.read();    
  Serial.println(x); 
 if (x==1)
 {
   digitalWrite(led,HIGH);
 }
 else
 {
   digitalWrite(led,LOW);
 }
 
  
}

This line reads the incoming data from the other device and stores it in a variable called “x.”

int x = Wire.read(); 
 i2c communication

see another article about gyroscopes and accelerometers…

Advantages of I2C

I2C communication is popular because of how easy and flexible it is.

It makes it possible to connect numerous devices to one bus at once, requiring fewer wires.

I2C devices can be added or removed from the bus with ease without causing a system-wide disruption.

Different data transfer speeds are supported, giving it flexibility for a range of applications.

Examples of I2C Applications

  • Temperature and Humidity Sensor-A microprocessor can receive real-time weather information via an I2C-enabled temperature and humidity sensor, enabling weather monitoring and forecasting.
  • Accelerometer-Applications like motion detection and gaming are made possible by the ability of an accelerometer, which detects acceleration forces, to interact with a microprocessor via I2C communication.
  • LED Display-LED displays, such as those used on digital clocks, can be controlled via I2C communication by providing instructions and data to the display module.
  • EEPROM (Electrically Erasable Programmable Read-Only Memory)-on microprocessor-based systems, small pieces of data, such as system configurations, are frequently stored on EEPROM chips with an I2C architecture.

Summary

In the field of microprocessors, I2C communication is a basic protocol that makes it possible for devices to effectively communicate with one another.

I2C communication opens the door to a number of exciting opportunities in the field of embedded systems, including managing LED displays, tracking weather, and providing motion detection.



Leave a Reply

Your email address will not be published. Required fields are marked *