We commence this week by taking an online local class, our instructor explained to us how to use the I2C protocol using two Arduino or more in TinkerCAD
software. In addition, he provides us with some documents and tutorials to read and watch to improve our knowledge about networking and learn more about the working principles of different communication protocols.
Consequently, I tried to implement what we learned at the class in real life. First to build basic I2C communication between two boards, I used two Arduino UNO
boards.
In any type of communication protocol between devices or processors, there is master
and slave
devices.
Where the master device is responsible to control one or more other slaves.
The code below was provided for us in the local class, run this code will establish an I2C communication between two Arduino.
I uploaded the code first in the master device, then on the slave device.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus
}
void loop ()
{
Wire.beginTransmission(8); //transmit to device number 8
Wire.write('a'); //send one byte 00000000
Wire.endTransmission(); //stop transmitting
delay (500);
Wire.beginTransmission(8); //transmit to device number 8
Wire.write('b');
Wire.endTransmission(); //stop transmitting
delay (500);
}
#include <Wire.h>
void setup()
{
Wire.begin(8); // join i2c bus with address 8
Wire.onReceive(receiveEvent); //register event
Serial.begin(9600); //start serial communication
pinMode(13,OUTPUT);
}
byte x = 0;
void loop ()
{
delay(100);
}
//function that executes whenver data is received from master
//this function is registered as an event
void receiveEvent(int howMany){
while(Wire.available()){
char c = Wire.read();
Serial.println(c);
if (c == 'a')
{digitalWrite (13,HIGH);}
else if (c == 'b')
{digitalWrite (13,LOW);}
}
}
The video below shows my first trial:
I had some problems the codes where not working correctly even though I did same exactly what the instructor did, also the serial monitor was not showing any output to debug this error I did the following:
Upload different code for I2C protocol.
Upload sample print code only to test if the output appear in the serial monitor.
Connect both Arduino to the laptop with USB.
Changed the jumper wires used to connect SCL and SDA pins, the serial monitor work and was showing the outputs correctly.
Even though the serial monitor worked, when I tried to run it one more time it didn’t, also their was a problem in the LED, it was blinking only once. so maybe the problem from the slave Arduino also while trying I was getting this error message on my laptop.
So I tried to upload the code in the ATemga board I fabricated in electronic design week. The code worked perfectly as shown:
When I used my ATemga board, I had to modife the code and connect the LED to pin 3 not 13.
#include <Wire.h>
void setup()
{
Wire.begin(8); // join i2c bus with address 8
Wire.onReceive(receiveEvent); //register event
Serial.begin(9600); //start serial communication
pinMode(3,OUTPUT);
}
byte x = 0;
void loop ()
{
delay(100);
}
//function that executes whenver data is received from master
//this function is registered as an event
void receiveEvent(int howMany){
while(Wire.available()){
char c = Wire.read();
Serial.println(c);
if (c == 'a')
{digitalWrite (3,HIGH);}
else if (c == 'b')
{digitalWrite (3,LOW);}
}
}