Sending & Receiving "1byte" data - I2C bus
Objective:
Here we will learn how to establish a I2C bus serial communication between 2 Arduinos and how both Master & Slave Arduino can send & receive "1byte" of data (ex: 0xAB,0x47).
Theory:
Before we go for coding, we must know the basics of I2C bus and how it works. I highly recommend you to go through Scott Campbell's article on I2C bus to understand it better.
Hardware Setup:
Full Code:
ArduinoA (Master)
#include<Wire.h> //the main library
byte x; //stores incoming byte
void setup()
{
Wire.begin(); //only for master to create communication
Serial.begin(9600); //MCU & Serial Monitor communication starts
}
void loop()
{
Wire.requestFrom(0x52, 1); //requesting 0x52 to send 1 byte
while(Wire.available()>0)
{
x =Wire.read(); //reads data
Serial.println(x, HEX);
}
Wire.beginTransmission(0x52);
Wire.write(0xAB); //data sent
Wire.endTransmission(); //data sending complete
}
ArduinoB (Slave)
#include<Wire.h> //the main library
byte x; //stores incoming data
void setup()
{
Wire.begin(0x52); //slave address to call by Master
Serial.begin(9600); //MCU & Serial Monitor communication starts
Wire.onReceive(getData);
Wire.onRequest(sendData);
}
void loop()
{
Serial.println(x, HEX); //incoming data is print
}
void sendData()
{
Wire.write(0x47); //sending data at request
}
void getData(int n) //n = the number of data received
{
x=Wire.read(); //saving incoming data
}
Code Explanation:
At Master
Wire.requestFrom(0x52, 1);
Requesting Slave no-0x52 send 1byte data
byte n = Wire.requestFrom(0x52, 1);
Requesting Slave no-0x52 send 1byte data. If the slave 0x52 is online on the bus, then n=1, not necessary any data to come. This value "1" is used to allocate memory space for incoming data.
byte n = Wire.requestFrom(0x52, 5);
Here n=5. Which means the Master allocated 5 bytes of data where incoming data will be stored. Now if 2/3 bytes data comes then the rest of the memory location will be auto-filled with 0xFF (in decimal = 255)
while( Wire.available() > 0 )
Till there are data coming from Slave/data is available to receive, this loop will run to receive & store incoming data.
Serial.println(x, HEX);
print 1 byte in hexadecimal form (0x47), without HEX it would be a Decimal value (71)
Wire.beginTransmission(0x52);
In I2C bus protocol, its not possible to share (send & receive) data simultaneously. We have to complete one task first then do the next. if Slave is sending first then Master has to receive first & vice-versa. So here the "while( )" loop will keep receiving data until all data sending is done. Then with "Wire.beginTransmission(0x52)" the master will send a clock signal to Slave No-0x52 to tell that it will now start its data transmission,so be prepared to receive.
Wire.endTransmission();
This command will tell the receiver that no more data will be transmitted from now. The data transmission is ended. Now the receiver waits for another "Wire.beginTransmission(0x52)" to start receiving again.
At Slave
Wire.onReceive(getData);
When the Master sends any data to this Slave, the "getData" function will run automatically to receive & store incoming data. This function works only for Slaves. Masters dont have these type of receiving function. "getData" is a regular function name, it can be replaced by any name that follows the Variable naming rules.
Wire.onRequest(sendData);
When the Master asks to send any data by terminating "Wire.requestFrom(0x52, 1)" in the Master side code, the "sendData" function will run automatically to send the requested data. This function works only for Slaves. Masters dont have this type of sending function. Similarly as before, "sendData" is a regular function name, it can be replaced by any name that follows the Variable naming rules.
Result:
Master's Serial Monitor Slave's Serial Monitor
See More Examples:
Comments