I2C bus communication protocol tutorial,applications

Data transmission can mainly occur in two ways, parallel or serial. To communicate with microcontrollers, serial protocol is used. A language in which mode of communication is defined between devices and a system is known as protocol. It specifies the electrical aspects, mechanical aspects, bit ordering and bit pattern meaning. For serial data communication different protocols are used such as TWI, USB, SPI or UART etc. It have many applications in microcontrollers projects

I2C communication

The I2C (Inter-integrated circuit pronounced as “eye-squared-see”) is a protocol which allows different slave chips or circuits to communicate with different master chips. It is only used for short distance communications. It is advantageous in terms of usage of wires, as it requires only 2 wires but 1008 devices can be supported by these 2 wires only.

I2C is more preferable over UART or SPI that is why it will be our main focus in this article. But let us take a brief look at other protocols also and find out why they are not preferred over this IC.

Types of communication protocols

Universal Asynchronous Receiver and Transmitter (UART)

Its first chip was designed in 1971. The transmission of data was done through a controller or PC. It requires two pins, one is the transmitter pin (TX) and the other is a receiver pin (RX). The transmitter of one device is connected with the receiver of the other device. The data transmission is from transmitter to the receiver. A common ground line is provided for data communication.

UART communication

UART communication

MAX232 is required for the communication of controller with a PC. It is a level converter IC. The baud rate of both the devices should be same for the transmission of data. UART has limited speed therefore it is not used in high speed devices. This chip is found inbuilt in modern microcontrollers. Serial communication using pic microcontroller is best example of this.




Serial Peripheral Interface (SPI)

For full operation a UART requires 9 pins, whereas an SPI has only three lines (SCK, MISO and MOSI) for both data transmission and handshake. There is only one slave controller and one master controller in case of SPI therefore there is no need for slave addressing. The master reads the data from MISO line and transmits the data on MOSI line. On the SCL line the synchronization clock is also provided by the Master and the slave does not even have to worry about the clock frequency or synchronization. In short, the Master controls all the processes.

Serial Peripheral Interface

Serial Peripheral Interface

One of the major disadvantages of SPI is that it is only applicable for small distance communications. In slave device an additional pin SS is also required hence the number of pins required in SPI becomes greater as those required for I2C.

Inter-integrated circuit (I2C)

It was invented by Philips. Initially its frequency was only 400 kHz and only 7 bit addresses could be used, which limited the number of devices. Only 112 devices could be used on the bus. Later on in 1992, the specifications were modified. The address space was expanded to 10 bit and 400 kHz fast mode was added. Due to this addition the number of devices and the speed of operations, both increased.

Three additional modes have been specified in this IC:

  1. At 1 MHz: ‘Fast mode plus’.
  2. At 3.4 MHz: ‘high speed mode’.
  3. At 5 MHz: ‘ultra fast mode’.

I2C bus signals

There are two signals at every bus.

  1. SCL (Serial Clock).
  2. SDA (Serial Data).

The bus master generates the clock signal. In some cases, the slaves become slow. This may be because the data given to them is too lengthy or the time span for each clock cycle is too short, then the slaves force the clock low so that they can easily prepare the data before the next cycle starts. This phenomenon is known as “clock stretching”.

The bus drivers of I2C are “open drain”, which means that they can pull a signal line low, but they are unable to drive it high. Suppose if one device is trying to pull the line low whereas another device is trying to drive the line high, then there will be no bus contention. This secures the drivers from getting damaged and protects the system from being over heated. It is shown in figure below:

I2C bus signals master slave

I2C bus signals master slave

The pull up resistors connected on the line drives that line to a high signal when no other signal is pulling it low. The resistors could be of different values but commonly we start with a 4.7k resistor and then decrease the value according to our requirements, if needed.

I2C bus signals levels

Some of the times, the devices in a system have different voltage potentials, i,e the voltage of one device is higher as compared to the other device. But it is possible to connect the both devices without using a level shifting circuit. This is done by connecting a pull up resistor to that device which is lower in voltage level. This method is not sure to work in all cases, but it works just fine in some cases where the lower voltage is greater than the input level of higher voltage system. A good example of this could be a 3.3V accelerometer and a 5V arduino.

I2C communication PROTOCOL

The communication through an I2C is very complex. For valid communication, the signals for the devices on the bus must be adhered to a specific protocol. But luckily, most of the devices deal with the small details by themselves and we can concentrate just on the data we want to transmit.

SCL SDA signals

SCL SDA signals

All the messages are split in two frames:

  1. An address frame.
  2. Data frames.

In address frame, the master tells the address of the slave to which the data is to be transmitted. And the data frames consist of 8 bit messages which are passed from the master to slave or from slave to the master. When the SCL line goes low, data is placed on the SDA line. And when the SCL line goes high, data is then sampled.

i2c PROTOCOL START CONDITION

The master device pulls SDA (serial data) low and leaves SCL (serial clock) high in order to start the address frame. It alerts all the slave devices that a transmission is going to get started. If there are two or more master devices, then whichever master device brings the SDA low first will take the ownership of the bus.

I2C Start Stop condition

I2C Start Stop condition

i2c PROTOCOL ADDRESS FRAME

The address frame always comes first, because without an address the device would have no idea that to which slave, the data is to be transmitted. If the address is 7-bit, the MSB (most significant bit) is read first and then an R/W bit indicates that the operation is either a read or a write operation.

i2c PROTOCOL DATA FRAMES

Now the data transmission can get started. The clock pulses will be generated by the master regularly.  The R/W bit will indicate whether the data is being read or written by the master. If the data is to be read then the slave will put data on the SDA and if the data is to be written then the master will put data on the SDA. The internal register is auto incremented in most cases according to the number of reads or writes.

DATA FRAMES

DATA FRAMES

Protocol STOP CONDITION

After the successive transmission of all the data, a stop condition will be generated by the master. The SCL should transit from low to high and then it should remain high, while the SDA transits from low to high, to define the stop condition. It is shown in above figure.

APPLICATION OF I2C IN MICROCONTROLLERS

There is a built in I2C in modern microcontrollers such as PIC microcontroller. The basic working and functioning of this chip is same as explained above. The figure below shows that different devices can be connected with it by using only two pins of the microcontroller:

I2C applications with microcontrollers

I2C applications with microcontrollers

In this figure, the first device is a thermometer, the second one is an alarm clock and the third device is a LCD display. All of these are connected as slaves with the microcontroller. If there is any difference in their voltage levels then it can be adjusted by adding pull up resistors. Only one set of pull up resistors will be enough for the whole bus, we don’t have to connect the resistors separately with each device.

The communication will start as the clock signal is initiated. Both the master and slaves will be synchronized, after which the master will initiate the data exchange. First, it will send the start bit, then the 7 bit address which will indicate the slave to be selected and finally the read/write bit is sent. As soon as the first byte is sent the master waits for an acknowledgement signal and the data transfer continues till the stop bit is finally sent by the master.

The programming of I2C is not difficult once we familiarize with the keywords and libraries used for its programming. Different series of controllers have different compilers and libraries. For example, PIC microcontrollers are supported by the compiler ‘mikro C pro’ which contains built in libraries which make the programming quick and easy.

Photo Credits:

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 823 other subscribers