0% found this document useful (0 votes)
20 views50 pages

Inter Integrated Circuit Protocol (I2C)

The Inter Integrated Circuit (I2C) protocol is a serial communication method that allows multiple slaves to connect to a single master using only two wires, supporting up to 128 devices with 7-bit addressing. Data is transferred in messages consisting of address frames and data frames, with mechanisms for start/stop conditions and acknowledgment bits. I2C is widely used due to its simplicity and effectiveness, although it has slower data transfer rates compared to SPI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views50 pages

Inter Integrated Circuit Protocol (I2C)

The Inter Integrated Circuit (I2C) protocol is a serial communication method that allows multiple slaves to connect to a single master using only two wires, supporting up to 128 devices with 7-bit addressing. Data is transferred in messages consisting of address frames and data frames, with mechanisms for start/stop conditions and acknowledgment bits. I2C is widely used due to its simplicity and effectiveness, although it has slower data transfer rates compared to SPI.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Inter Integrated Circuit Protocol (I2C)

▪It is a serial communication protocol allow to connect multiple


slaves to a single master (like SPI) and multiple masters
controlling single, or multiple slaves.
▪Only two wires are required for communication between up to
almost 128 (112) devices when using 7 bits addressing and up
to almost 1024 (1008) devices when using 10 bits addressing.
▪This is really useful when more than one microcontroller
logging data to a single memory card or displaying text to a
single LCD.
Inter Integrated Circuit Protocol (I2C)

▪SDA (Serial Data) – The line for the master and slave to
send and receive data.
▪SCL (Serial Clock) – The line that carries the clock signal.
Inter Integrated Circuit Protocol (I2C)
Inter Integrated Circuit Protocol (I2C)
Inter Integrated Circuit Protocol (I2C)

Signals of I2C
▪Each I2C bus consists of two signals: SCL and SDA.
▪SCL is the clock signal, and SDA is the data signal.
▪The clock signal is always generated by the current
bus master.
▪Some slave devices may force the clock low at times
to delay the master sending more data. This is called
“clock stretching”.
HOW I2C WORKS

▪With I2C, data is transferred in 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, and ACK/NACK bits between each data
frame.
HOW I2C WORKS
HOW I2C WORKS
HOW I2C WORKS

▪Start Condition:
To initiate the address frame, the master device leaves SCL
high and pulls SDA low. This puts all slave devices on notice
that a transmission is about to start. If two master devices
wish to take ownership of the bus at one time, whichever
device pulls SDA low first wins the race and gains control of
the bus.
HOW I2C WORKS

▪Stop condition:
Once all the data frames have been sent, the master will
generate a stop condition. Stop conditions are defined by a
0->1 (low to high) transition on SDA after a 0->1 transition
on SCL, with SCL remaining high.
▪Address Frame:
A 7 or 10 bit sequence unique to each slave that identifies
the slave when the master wants to talk to it.
HOW I2C WORKS

▪Data Frame:
After the master detects the ACK bit from the slave, the
first data frame is ready to be sent.
The data frame is always 8 bits long, and sent with the
most significant bit first. Each data frame is immediately
followed by an ACK/NACK bit to verify that the frame has
been received successfully. The ACK bit must be received
by either the master or the slave (depending on who is
sending the data) before the next data frame can be
sent.
HOW I2C WORKS

▪Read/Write Bit:
The address frame includes a single bit at the end that informs the
slave whether the master wants to write data to it or receive data
from it. If the master wants to send data to the slave, the
read/write bit is a low voltage level. If the master is requesting
data from the slave, the bit is a high voltage level.
▪ACK/NACK Bit:
Each frame in a message is followed by an acknowledge/no-
acknowledge bit. If an address frame or data frame was
successfully received, an ACK bit is returned to the sender from
the receiving device.
ADDRESSING

I2C doesn’t have slave select lines like SPI, so it needs another
way to let the slave know that data is being sent to it, and not
another slave. It does this by addressing. The address frame is
always the first frame after the start bit in a new message.
The master sends the address of the slave it wants to
communicate with to every slave connected to it. Each slave
then compares the address sent from the master to its own
address. If the address matches, it sends a low voltage ACK bit
back to the master. If the address doesn’t match, the
slave does nothing and the SDA line remains high.
STEPS OF I2C DATA TRANSMISSION

1. The master sends the


start condition to every
connected slave by
switching the SDA line from
a high voltage level to a
low voltage
level before switching the
SCL line from high to low.
STEPS OF I2C DATA TRANSMISSION

2. The master sends each


slave the 7 or 10 bit address
of the slave it wants to
communicate with, along
with the read/write bit.
STEPS OF I2C DATA TRANSMISSION

3. Each slave compares the


address sent from the master
to its own address. If the
address matches, the slave
returns an ACK bit by pulling
the SDA line low for one bit. If
the address from the master
does not match the slave’s
own address, the slave
leaves the SDA line high.
STEPS OF I2C DATA TRANSMISSION

4. The master sends or


receives the data frame.
STEPS OF I2C DATA TRANSMISSION

5. After each data frame has


been transferred, the
receiving device returns
another ACK bit to the
sender to acknowledge
successful receipt of the
frame.
STEPS OF I2C DATA TRANSMISSION

6. To stop the data


transmission, the master
sends a stop condition to the
slave by switching SCL high
before switching SDA high.
Advantages of I2C

▪Only uses two wires


▪Supports multiple masters and multiple
slaves
▪ACK/NACK bit gives confirmation that each
frame is transferred successfully
▪Hardware is less complicated than with
UARTs
▪Well known and widely used protocol
Disadvantages of I2C

▪Slower data transfer rate than SPI


▪The size of the data frame is limited to 8
bits
ARDUINO Wire Library

▪This library allows to communicate with I2C / TWI devices.

Board I2C / TWI pins


Uno A4 (SDA), A5 (SCL)
Mega2560 20 (SDA), 21 (SCL)
Leonardo 2 (SDA), 3 (SCL)
20 (SDA), 21
Due
(SCL), SDA1, SCL1
ARDUINO Wire Library

Functions

▪begin() ▪available()
▪requestFrom() ▪read()
▪beginTransmission() ▪SetClock()
▪endTransmission() ▪onReceive()
▪write() ▪onRequest()
Master Reader/Slave Sender

Circuit
Connect pin 4 (the data, or SDA, pin) and pin 5 (the clock, or
SCL, pin) on the master board to their counterparts on the slave
board. Make sure that both boards share a common ground. In
order to enable serial communication, the master board must be
connected to your computer via USB.
If powering the boards independently is an issue, connect the 5V
output of the Master to the VIN pin on the slave.
Master Reader/Slave Sender
Master Reader/Slave Sender
Schematic
Code for Master Reader - Program for Arduino 1
Code for Slave Sender - Program for Arduino 2
Master Writer/Slave Receiver

Circuit
Connect pin 5 (the clock, or SCL, pin) and pin 4 (the data, or
SDA, pin) on the master Arduino to their counterparts on the
slave board. Make sure that both boards share a common
ground. In order to enable serial communication, the slave
Arduino must be connected to your computer via USB.
If powering the boards independently is an issue, connect
the 5V output of the Master to the VIN pin on the slave.
Master Writer/Slave Receiver
Master Writer/Slave Receiver
Schematic
Master Writer Code - Program for Arduino
1
Slave Receiver Code - Program for Arduino
2
Master Writer Code – LED Blink
Slave Receiver Code – LED Blink
Functions

Wire.begin()
Wire.begin(address)
▪Description
Initiate the Wire library and join the I2C bus as a master or slave. This
should normally be called only once.
▪Parameters
address: the 7-bit slave address (optional); if not specified, join the bus as
a master.
▪Returns
None
Functions

Wire.requestFrom()

▪Description
Used by the master to request bytes from a slave device. The bytes
may then be retrieved with the available() and read()functions.
If true, requestFrom() sends a stop message after the request,
releasing the I2C bus.
If false, requestFrom() sends a restart message after the request. The
bus will not be released, which prevents another master device from
requesting between messages. This allows one master device to send
multiple requests while in control.
The default value is true.
Functions

Wire.requestFrom()
▪ Syntax
▪ Wire.requestFrom(address, quantity)
Wire.requestFrom(address, quantity, stop)
▪ Parameters
▪ address: the 7-bit address of the device to request bytes from
▪ quantity: the number of bytes to request
▪ stop : boolean. true will send a stop message after the request, releasing the
bus. false will continually send a restart after the request, keeping the
connection active.
▪ Returns
▪ byte : the number of bytes returned from the slave device
Functions

Wire.available()
▪Description
▪Returns the number of bytes available for retrieval with read().
This should be called on a master device after a call
to requestFrom() or on a slave inside the onReceive() handler
▪Parameters
▪None
▪Returns
▪The number of bytes available for reading.
Functions
read()
▪Description
▪ Reads a byte that was transmitted from a slave device
to a master after a call to requestFrom() or was
transmitted from a master to a slave.
▪Syntax
▪ Wire.read()
▪Parameters
▪ none
▪Returns
▪ The next byte received
Functions

Wire.onRequest(handler)
▪Description
▪Register a function to be called when a master
requests data from this slave device.
▪Parameters
▪handler: the function to be called, takes no parameters
and returns nothing, e.g.: void myHandler()
▪Returns
▪None
Functions
Wire.onReceive(handler)
▪Description
▪ Registers a function to be called when a slave device receives a
transmission from a master.
▪Parameters
▪ handler: the function to be called when the slave receives data; this
should take a single int parameter (the number of bytes read from the
master) and return nothing, e.g.: void myHandler(int numBytes)
▪Returns
▪ None
Functions

write()
▪Description
▪ Writes data from a slave device in response to a request from a
master, or queues bytes for transmission from a master to slave
device (in-between calls to beginTransmission() and
endTransmission()).
▪Syntax
▪ Wire.write(value)
Wire.write(string)
Wire.write(data, length)
Functions

▪Parameters
▪ value: a value to send as a single byte
▪ string: a string to send as a series of bytes
▪ data: an array of data to send as bytes
▪ length: the number of bytes to transmit
▪Returns
▪ byte: write() will return the number of bytes written,
though reading that number is optional
Functions

Wire.beginTransmission(address)
▪Description
▪ Begin a transmission to the I2C slave device with the given
address. Subsequently, queue bytes for transmission with
the write() function and transmit them by
calling endTransmission().
▪Parameters
▪ address: the 7-bit address of the device to transmit to
▪Returns
▪ None
Functions

Wire.endTransmission()
▪Description
▪ Ends a transmission to a slave device that was begun
by beginTransmission() and transmits the bytes that were queued by write().
▪ If true, endTransmission() sends a stop message after transmission,
releasing the I2C bus.
▪ If false, endTransmission() sends a restart message after transmission. The
bus will not be released, which prevents another master device from
transmitting between messages. This allows one master device to send
multiple transmissions while in control.
▪ The default value is true.
Functions

Wire.endTransmission()
▪Syntax
▪ Wire.endTransmission()
Wire.endTransmission(stop)
▪Parameters
▪ stop : boolean. true will send a stop message, releasing the
bus after transmission. false will send a restart, keeping the
connection active.
Functions

Wire.endTransmission()
▪Returns
▪ byte, which indicates the status of the transmission:
⮚ 0:success
⮚ 1:data too long to fit in transmit buffer
⮚ 2:received NACK on transmit of address
⮚ 3:received NACK on transmit of data
⮚ 4:other error
Link for different Communication
Protocol of Arduino
◦ https://maker.pro/arduino/tutorial/common-communication-peripherals-on-the-
arduino-uart-i2c-and-spi
THANK YOU

You might also like