IoT PPT08 I2C Protocol and Demo
IoT PPT08 I2C Protocol and Demo
I2C Protocol
Demo Using NodeMCU and Arduino
Source: http://www.quatech.com/support/figures/async1.gif
• Only two communication lines for all devices on the bus (SDA, SCL)
• Bi-directional communication
• I2C link is half-duplex, means only one device transmits at any given time.
• Both the signal lines (SDA, SCL) are ‘open drain’, thus pull-up resistors are needed
Open-drain output stage supports multiple drivers on the same signal line using NMOS transistor
• Note, here we have an inverted logic scheme where driving a LOW to the NMOS yields
a HIGH on the line, and vice-versa.
??? HIGH
(1)
LOW (0) OFF LOW (0) OFF
• As soon as the START condition is generated, the SCL will be pulled LOW and start
pulsing to provide the clock for the message.
Operation Steps in I2C - 3/12
• Both the master and the slaves count the number of periods that have occurred since the
message started in order to know when certain frames and signals should be present.
Operation Steps in I2C - 4/12
• After the master generates the START condition, it first sends the slave address that
it wishes to communicate with.
• After the slave address and read/write signal (1 bit: 0 to write, 1 to read) are sent by the master,
each slave on the bus checks whether it is being addressed.
• Period 9 of the message is reserved for the slave acknowledge (ACK) or no-acknowledge
(NACK) signal.
Operation Steps in I2C - 6/12
• Intended slave will send an ACK signal back to the master by pulling SDA LOW.
• If no device exists with the specified slave address, no device will pull down SDA.
This will result in period 9 remaining HIGH and will be interpreted as a NACK.
Operation Steps in I2C - 7/12
• If the master sees the ACK signal, it knows a slave exists with the specified address and
proceeds with the message.
Operation Steps in I2C - 8/12
• After each byte is sent, the receiving device sends an ACK signal indicating that it
successfully received the data.
Operation Steps in I2C - 9/12
• A STOP condition occurs when there is a LOW-to-HIGH transition on SDA while SCL is HIGH.
• Once, SDA goes HIGH, SCL also remains HIGH indicating that the bus is idle again.
Operation Steps in I2C - 10/12
• A NACK in period 9 tells the master that no slave exists with the specified address.
• The master then generates a STOP condition and ends the message.
Operation Steps in I2C - 11/12
• When the master is writing to a slave, the master sends the 8-bits of data and the slave
produces the ACK/NACK signal.
• When the master is reading from a slave, the slave sends the 8-bits of data and the master
produces the ACK/NACK signal.
• After the data has been sent and acknowledged, the master can end the message by
generating the STOP condition anytime.
Writing 2-bytes (shaded bits are put on the bus by the master)
Reading 2-bytes (shaded bits are put on the bus by the master)
Source: http://www.byteparadigm.com/kb/article/AA-00255/22/Introduction-to-SPI-and-IC-protocols.html
void setup() {
Serial.begin(9600); /* begin serial for debug */
Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.write("Hello Arduino"); /* sends hello string */
Wire.endTransmission(); /* stop transmitting */
Wire.requestFrom(8, 13); /* request & read 13 byte data from slave device #8 */
while(Wire.available()){
char c = Wire.read();
Serial.print(c);
}
Serial.println();
delay(1000);
}
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
}
void loop() {
delay(100);
}
• There are both 7 or 8-bit versions of I2C addresses. 7 bits identify the
device, and the 8th bit determines if it’s being written to or read from.
• The Wire library uses 7 bit addresses throughout. However, the addresses
from 0 to 7 are not used because are reserved so the first address that can
be used is 8.
• Functions in Wire.h
begin() end()
requestFrom() beginTransmission()
endTransmission() write()
available() read()
setClock() onReceive()
onRequest() setWireTimeout()
clearWireTimeoutFlag() getWireTimeoutFlag()
Acknowledgement:
• Most of the images are taken from “Embedded Systems Design”, by Brock J. LaMeres, Springer
Publisher