Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Sherwin Chiu
Published © GPL3+

Using I2C Communication Protocol to Connect 6 Arduino Megas

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

BeginnerShowcase (no instructions)30 minutes14,130
Using I2C Communication Protocol to Connect 6 Arduino Megas

Things used in this project

Story

Read more

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

Code

Sender Code

Arduino
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 Code

Arduino
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
}

Credits

Sherwin Chiu
8 projects • 9 followers
Just a guy who occasionally blows up capacitors. I love doing this type of stuff!

Comments