I2C bus protocol Tutorial, Interface with applications

Nowadays the protocols play an essential role in the embedded system design. Without going to the protocols, if you want to expand the peripheral features of the microcontroller, the complexity and power consumption will increase. There are different types of bus protocols available such as USART, SPI, CAN, I2C bus protocol, etc., which are used for transferring the data between two systems.


I2C Protocol

What is I2C Bus?

Transmitting and receiving the information between two or more than two devices require a communication path called as a bus system. A I2C bus is a bidirectional two-wired serial bus which is used to transport the data between integrated circuits. The I2C stands for “Inter Integrated Circuit”. It was first introduced by the Philips semiconductors in 1982. The I2C bus consists of three data transfer speeds such as standard, fast-mode and high-speed-mode. The I2C bus supports 7-bit and 10-bit address space device and its operation differ with low voltages.

I2c Bus Protocol
I2c Bus Protocol

I2C Signal Lines

I2C Signal Lines
I2C Signal Lines

The I2C is a serial bus protocol consisting of two signal lines such as SCL and SDL lines which are used to communicate with the devices. The SCL stands for a ‘serial clock line’ and this signal is always driven by the ‘master device’. The SDL stands for the ‘serial data line’, and this signal is driven by either the master or the I2C peripherals. Both these SCL and SDL lines are in open-drain state when there is no transfer between I2C peripherals.

Open-Drain Outputs

The open-drain is the concept for FET transistor wherein the drain terminal of the transistor is open state. The SDL and SCL pins of the master device are designed with the transistors in open state, so data transfer is possible only when these transistors are conducted. Hence, these lines or drain terminals are connected thorough pull-up resistors to VCC for conduction mode.

I2C Interfaces

Many slave devices are interfaced to the microcontroller with the help of the I2C bus through I2C level shifter IC for transferring the information between them. The I2C protocol used to connect a maximum of 128 devices that are all connected to communicate with the SCL and SDL lines of the master unit as well as the slave devices. It supports Multimaster communication, which means two masters are used to communicate the external devices.

I2C Data Transfer Rates

The I2C protocol operates three modes such as: fast mode, high-speed mode and standard mode wherein the standard mode data speed ranges 0Hz to 100Hz, and the fast mode data can transfer with 0Hz to 400 KHz speed and the high speed mode with 10 KHz to 100KHz. The 9-bit data is sent for each transfer wherein 8-bits are sent by the transmitter MSB to LSB, and the 9th bit is an acknowledgement bit sent by the receiver.

I2C Data Transfer Rates
I2C Data Transfer Rates

I2C Communication

The I2C bus protocol is most commonly used in master and slave communication wherein the master is called “microcontroller”, and the slave is called other devices such as ADC, EEPROM, DAC and similar devices in the embedded system. The number of slave devices is connected to the master device with the help of the I2C bus, wherein each slave consists of a unique address to communicate it. The following steps are used to communicate the master device to the slave:

Step1: First, the master device issues a start condition to inform all the slave devices so that they listen on the serial data line.

Step2: The master device sends the address of the target slave device which is compared with all the slave devices’ addresses as connected to the SCL and SDL lines. If anyone address matches, that device is selected, and the remaining all devices are disconnected from the SCL and SDL lines.

Step3: The slave device with a matched address received from the master, responds with an acknowledgement to the master thereafter communication is established between both the master and slave devices on the data bus.

Step4: Both the master and slave receive and transmit the data depending on whether the communication is read or write.

Step5: Then, the master can transmit 8-bit of data to the receiver which replies with a 1-bit acknowledgement.

I2C Tutorial

Transmitting and receiving the information step by step serially with respect to the clock pulses is called I2C protocol. It is an inter-system and short-distance protocol, which means, it is used within the circuit board to communicate the master and slave devices.

I2C Protocol Basics

In general, the I2C bus system consists of two wires that are used easily to expand the input and output peripheral features such as ADC, EEROM and RTC, and other basic components to make a system whose complexity is very less.

Example: Since 8051 microcontroller has no inbuilt ADC – so, if we want to interface any analog sensors to the 8051 microcontroller – we have to use ADC devices such as ADC0804-1 channel ADC, ADC0808- 8 channel ADC, etc. By using these ADCs, we can interface the analog sensors to the microcontroller.

Without using the protocol to expand the I/O features of any microcontroller or processor, we can go to 8255 ICit 8-pin device. The 8051 microcontroller is a 40-pin microcontroller; by using the 8255 IC, we can expand the 3-I/O ports with 8-pins in each port. By using all the devices such as RTC, ADC, EEPROM, Timers, etc. – for expanding the peripheral circuitry – complexity, cost, power consumption and product size are also increased.

To overcome this problem, the protocol concept comes into the picture for reducing the hardware complexity and power consumption. We can expand more number of features, such as I/0 peripherals, ADCs, T/C and memory devices upto 128 devices by using this I2C protocol.
Terminology Used in I2C Protocols

Transmitter: The device that sends data to the bus is called transmitter.

Receiver: The device that receives data from the bus is called a receiver.

Master: The device that initiates transfers to generate a clock signals and terminate a transfer is called a master.

Slave: The device addressed by a master is called a slave.

Multimaster: More than one master can attempt to control the bus at the same time without corrupting the message is called a Multimaster.

Arbitration: Procedure to ensure that, if more than one master simultaneously tries to control the bus – only one is allowed to do so; the winning message is not corrupted.

Synchronization: Procedure to synchronize the clock singles of two or more devices is called synchronization.

I2C Basic Commands Sequence

  1. Start Bit Condition
  2. Stop Bit Condition
  3. Acknowledgement Condition
  4. Master to slave Write operation
  5. Read Operation Slave to Master

Start and Stop Bit Condition

When the master (microcontroller) wishes to talk to a slave device (for example ADC), it begins communication by issuing a start condition on the I2C bus, and then issues a stop condition. The I2C start and stop logic levels are shown in the figure.

The I2C start condition defines as a high to low transition of the SDA line while the SCL line is high. AN I2C stop condition occurs when the SDA line toggles from low to high while the SCL line is high.

The I2C master always generates the S and P conditions. Once the I2C master initiates a START condition, the I2c bus is considered as being in busy state.

Start and Stop Bit Condition
Start and Stop Bit Condition

Programming:

START Condtion:

sbit SDA=P1^7; // initialize the SDA and SCL pins of the microcontroller//
sbit SCL=P1^6;
void delay(unsigned int);
void main ()
{
SDA=1; //processing the data//
SCL=1; //clock is high//
delay();
SDA=0; //sent the data//
delay();
SCL=0; //clock signal is low//
}
Void delay(int p)
{
unsignedinta,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}

STOP Condition:

void main ()
{
SDA=0; // Stop processing the data//
SCL=1; //clock is high//
delay();
SDA=1; //Stopped//
delay();
SCL=0; //clock signal is low//
}
Void delay(int p)
{
unsignedinta,b;
For(a=0;a<255;a++); //delay function//
For(b=0;b<p;b++);
}

Acknowledgement (ACK) and No Acknowledgement (NCK) Condition

Each byte transmitted over the I2C bus is followed by an acknowledge condition from the receiver, which means, after the master pulls SCL low to complete the transmission of 8-bit, the SDA will be pulled low by the receiver to the master. If, after the transmission of the receiver does not pull, the SDA line LOW is considered to be a NCK condition.

Acknowledgement (ACK)
Acknowledgement (ACK)

Programming

Acknowledgement
void main()
{
SDA=0; //SDA line goes to low//
SCL=1; //clock is high to low//
delay(100);
SCL=0;
}
No Acknowledgement:
void main()
{
SDA=1; //SDA line goes to high//
SCL=1; //clock is high to low//
delay(100);
SCL=0;
}

Master to Slave Writes Operation

The I2C protocol transfers the data in the form of packets or bytes. Each byte is followed by an acknowledgement bit.

Data Transfer Format

Data Transfer Format
Data Transfer Format

Start: Primarily, the data transfer sequence initiated by the master generating the start condition.

7-bit Address: After that the master sends the slave address in two 8-bit formats instead of a single 16-bit address.

R/W: If the read and write bit is high, then the write operation is performed.

ACK: If the write operation is performed in the slave device, then the receiver sends the 1-bit ACK to the microcontroller.

Stop: After completion of the write operation in the slave device, the microcontroller sends the stop condition to the slave device.

Programming

Write Operation

voidwrite (unsigned char d)
{
Unsigned char k, j=0x80;
For(k=0;k<8;k++)
{
SDA=(d&j);
J=j>>1;
SCL=1;
delay(4);
SCL=0;
}
SDA=1;
SCL=1;
delay(2);
c=SDA;
delay(2);
SCL=0;
}

Master to Slave Read Operation

The data is read back from the slave device in the form of bit or bytes – read the most significant bit first and read the least significant bit last.

The Data Read Format

Data Read Format
Data Read Format

Start: Primarily, the data transfer sequence is initiated by the master generating the start condition.

7-bit Address: After that the master sends the slave address in two 8-bit formats instead of a single 16-bit address.

R/W: If the read and write bit is low, then the read operation is performed.

ACK: If the write operation is performed in the slave device, then the receiver sends the 1-bit ACK to the microcontroller.

Stop: After completion of the write operation in the slave device, the microcontroller sends the stop condition to the slave device.

Programming

Void read ()
{
Unsigned char j, z=0x00, q=0x80;
SDA=1;
for(j=0;j<8;j++)
{
SCL=1;
delay(100);
flag=SDA;
if(flag==1)
{
z=(z|q);
q=q>>1;
delay(100);
SCL=0;
}

Practical Example of Interfacing ADC to the 8051 Microcontroller

The ADC is a device which is used to convert the analogue data into the form of digital and digital to analogue. The 8051 microcontroller does not have an inbuilt ADC so we have to add externally through I2C protocol. The PCF8591 is the I2C based analogue to digital and digital to analogue converter. This device can support a maximum of 4-analogue input channels along with 2.5 to 6v voltages.

Analogue Outputs

The analogue outputs come in the form of voltages. For example, 5v analogue sensor gives output logic of 0.01v to 5v.
The maximum digital value of 5v is= 256.
The value of 2.5v is =123 according to the maximum voltage value.

The formula of analogue output is:

The Formula of Digital Outputs:

Interfacing ADC to the 8051 Microcontroller
Interfacing ADC to the 8051 Microcontroller

The above figure shows the data transfer using I2C protocol from ADC device to the 8051 microcontroller. The ADC pins of SCL and SDA are connected to the pin 1.7 and 1.6 of microcontroller for establishing communication between them. When the sensor gives analog values to the ADC, it converts to digital and transfers data to the microcontroller through the I2C protocol.

This is about the I2C bus protocol tutorial with appropriate programs. We hope that the given content gives you practical concept of interfacing several devices with microcontrollers using the I2C communication. If you have any doubt in the interfacing procedure of this protocol, you can reach us by commenting below.