Project showcase
Using I2C Communication Protocol to Connect 6 Arduino Megas

Using I2C Communication Protocol to Connect 6 Arduino Megas © GPL3+

If you've ever wanted to connect devices with just 3 pins, this is the perfect project for you!

  • 3,355 views
  • 9 comments
  • 21 respects

Components and supplies

Apps and online services

About this project

After learning about different communication protocols, I decided it would be fun if I took all my classmates Arduino Megas and connect them all with the I2C (I squared C, not I two C) protocol. With a little help from them, we created this masterpiece:

Although it doesn't look too impressive, I'm actually sending bytes of data to 5 different Arduino Megas. I do this with only 3 pins (DATA, CLK, and GROUND). Data can be sent 2 ways, meaning the readers can become writers with the same wires. Compared to the UART (universal asynchronous receiver-trasmitter) and the SPI (serial peripheral interface) communication protocols, this allows for transmission of data without a bunch of wires or pins being used. Each Arduino Mega gets its own data packet sent to them by my one master Mega, and if each of the Arduino's wanted to, they could become the master Mega as well.

After a while of being amazed at how awesome my boards were, I realized I couldn't even visualize the bits of data that I was sending. This is where the trusty oscilloscope comes in! I hooked up Channel 1 to Data and Channel 2 to Clock. I set 2.00V per division (per square) for both the DATA and CLK, and set the trigger on the rising edge of the clock. After fiddling around with other knobs and buttons, this is what I finally saw:

Pretty neat, right? Although a little curved due to interference, it's perfectly readable. Here, we see the master attempting to communicate to an address that doesn't respond back. Both DATA and CLK pins have an active low, and have an idle high. First, the DATA pin gets pulled down, shortly followed by the CLK pin. On the rising edge of the CLKs, the data is read from the LSB (least significant bit). There are only 7 bits for the address, 1 read/write bit (LOW for write), and then 1 response bit, for a total of 9 bits. Here, we can see the address has a value of: 0000011. In decimal, 3. Since the response bit is high, no one is at the end of the line, and no one responds. I find out that my friend has wired his GND on his Mega incorrectly, but that's okay, as the bus frees itself for another response by pulling both the CLK and DATA back to high to be idle. My Mega can still talk to 4 other Megas, and one of them actually responds: (ditched that one friend)

Now we're talking! Data actually being sent! It's amazing. Again, the DATA pin is pulled down, shortly followed by the CLK being pulled down. We can read the address again (which is pretty simple this time around) to being: 0000000. In decimal, I think you can guess what it is! After that, 1 read/write bit, 1 response bit (pulled low by the receiver), and then a little gap in the CLK before reading again. This time, data is actually being sent! The pattern goes 8 bits of data, followed by 1 response bit. In binary, we read: 11101000. Translating to human numbers, we read: 128 + 64 + 32 + 8 = 232! Exactly the number I was trying to send to address 0. The DATA pin goes back to high, then to low, then the CLK pin goes up, shortly followed by the data pin. My classmates and I have successively transferred data between our Arduino Megas with only 3 pins. Imagine how much data I sent to the other boards!

As I once said, that's crazy.

Seriously though, the I2C protocol is like the a combination of both the UART and SPI protocols. It allows for less pins, but is synchronous and allows for precise communication with specific devices. Its applications can vary from controlling displays, reading sensors, and controlling a network of chips with only 2 IO pins on the Arduino. Although I didn't show it control sensors/devices here, simply sending a byte through 2 wires and to 5 different boards still blows my mind. Although not the most complex project, its very cool!

Code

Sender CodeArduino
Importing the built-in Wire library from Arduino to use I2C.
/* Sender for I2C Protocol 
 * Sherwin Chiu 
 * Nov 19th, 2019
 */

#include <Wire.h> // built in Arduino library
int data[] = {232, 25, 63, 162, 25};

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  for(int address = 0; address < 5; address++){
    Wire.beginTransmission(address);   
    Wire.write(data[address]);     // send data to other Arduinos
    Wire.endTransmission();  // stop transmitting
    Serial.println(data[address]); // display on Serial Monitor
    delay(1000);  // wait one second so friends can read it 
  } 
}
Reader CodeArduino
Importing the built-in Wire library from Arduino to use I2C.
/* Reader for I2C Protocol 
 * Sherwin Chiu 
 * Nov 19th, 2019
 */
 
#include <Wire.h> // built in Arduino library
int address = 0;
int data;

void setup(){
  Serial.begin(9600);
  Wire.begin();
}
void loop(){
  Wire.requestFrom(address, 1);      
  while (!Wire.available()){};  // keep checking if avaliable       
  data = Wire.read();           // read data        
  Serial.println(data);         // display on Serial Monitor
  delay(100);                   // pause reading time
}

Schematics

Schematic
Yellow - SDA Pin, Serial Data
Blue - SCL Pin, Serial Clock
Black - Ground Pin
In real life, might need pull up resistors due to resistance
I2c schematic 1hdqyzp788

Comments

Similar projects you might like

I2C Communication Between Two Arduino Boards with Visuino

Project tutorial by Boian Mitov

  • 3,422 views
  • 0 comments
  • 26 respects

Morse Code Communication Using Laser Module (Both)

Project tutorial by SURYATEJA

  • 2,270 views
  • 1 comment
  • 12 respects

Morse Code Communication Using Arduino

Project tutorial by Jalal_Mansoori

  • 10,099 views
  • 17 comments
  • 30 respects
Add projectSign up / Login