I2C COMMUNICATION BETWEEN TWO ARDUINO

Introduction

In this project we are establishing i2c communication between two Arduino by making one Arduino as master and another Arduino as slave. The main objectives of our project is to glow a led that is connected to the slave Arduino by pressing the button from the master Arduino.

Components Required

arduino uno ——- 2pics

push button ——-1pics

led ——-1pics

jumpers ——– 1pics

Circuit Diagram

We are using two Arduino uno in our project Here one Arduino serves as Master and another Arduino serves as slave. The I2C pins of Arduino uno are A4 and A5. The A4 of one Arduino is connected to the A4 of another Arduino that will provide common SDA. The A5 of one Arduino will be connected to the A5 pin of another Arduino that will provide common SCL. The GND of one Arduino will be connected to the GND of another Arduino that will provide common ground. So, this much is our connection for this project.

Coding

Push button connected to master

#include<Wire.h>

void setup() {

 Serial.begin(9600);

 Wire.begin();   

 pinMode(2, INPUT_PULLUP);//B(egins I2C communication with Slave Address as 8 at pin (A4,A5)

}

void loop()

{

Wire.beginTransmission(8); 

int sensorVal = digitalRead(2);

  Serial.print(“sensorVal=” );

   Serial.println(sensorVal);

    Wire.write(sensorVal);                          // sends one byte converted POT value to slave

    Wire.endTransmission();   

}

Led connected to the slave

#include<Wire.h>

void setup() {

 Serial.begin(9600);

 pinMode(12,OUTPUT);

 Wire.begin(8);

 //B(egins I2C communication with Slave Address as 8 at pin (A4,A5)

}

void loop()

{

 Wire.onReceive(receiveEvent);

//Wire.onRequest(requestEvent);

}

void receiveEvent (int howMany)                    //This Function is called when Slave receives value from master

{

int   SlaveReceived = Wire.read();

   Serial.print(“button state= “);

   Serial.println(SlaveReceived);

   if (SlaveReceived == HIGH) {

    digitalWrite(12, LOW);

  } else {

    digitalWrite(12, HIGH);

  }

   //Used to read value received from master and store in variable SlaveReceived

}

Recommended For You

About the Author: admin

Leave a Reply

Your email address will not be published.