Inter Integrated Circuit Protocol (I2C)
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
▪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
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