Master Slave I2C Connection

Master Slave I2C Connection

How to Set up a MASTER-SLAVE connection between two Arduinos using I2C (Inter-Integrated Circuit) protocol.

  • 8,917 views
  • 0 comments
  • 10 respects

Components and supplies

Apps and online services

About this project

Introduction

In this tutorial I'll show you how can you communicate between two (or more) Arduino boards, using I2C (Inter-Integrated Circuit) protocol. In this case one Arduino Uno acts as MASTER and acts as a SLAVE.We will program one master Arduino to command the other slave Arduino to blink its built-in LED once or twice depending on the received value.

I2C Protocol

I2C is an interesting protocol. It's usually used to communicate between components on motherboards in cameras and in any embedded electronic system.

We use such a MASTER-SLAVE connection to decrease the work load on one Arduino, or to connect more sensors to project etc.

Connections

Follow these steps to connect two Arduino UNOs using I2C:

  • Connect pins A4 and A5 on one Arduino to the same pins on the other one.
  • The GND line has to be common for both Arduinos. Connect it with a jumper.

In case of any doubt, consult the circuit diagram given below.

NOTE: Remember never to connect 5 V and 3.3 V Arduinos together. It won't hurt the 5V Arduino, but it will certainly annoy its 3.3 V brother!

There are two different codes, one for MASTER and other for SLAVE. Upload each code to respective Arduinos. Power them, and you good to go!!

You can also connect more than one SLAVE Arduino! But you have to address each Arduino differently.

Code

MASTER_CODEC/C++
Upload to MASTER Arduino.
// Include the required Wire library for I2C<br>
#include<Wire.h>
int x = 0;
void setup() {
  // Start the I2C Bus as Master
  Wire.begin(); 
}
void loop() {
  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(x);              // sends x 
  Wire.endTransmission();    // stop transmitting
  x++; // Increment x
  if (x > 5) x = 0; // `reset x once it gets 6
  delay(500);
}
SLAVE_CODEC/C++
Upload to SLAVE Arduino.
// Include the required Wire library for I2C
#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
  // Define the LED pin as Output
  pinMode (LED, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}
void loop() {
  //If value received is 0 blink LED for 200 ms
  if (x == 0) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
  //If value received is 3 blink LED for 400 ms
  if (x == 3) {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }
}

Schematics

CIRCUIT_DIAG
Ms rdz7qfxrol

Comments

Similar projects you might like

2€ OLED I2C Analog Pin Dynamic Plotting

Project showcase by javier muñoz sáez

  • 7,179 views
  • 0 comments
  • 14 respects

How to Use I2C EEPROM

by Arduino “having11” Guy

  • 3,513 views
  • 0 comments
  • 16 respects

ESP8266 ESP-01 Webserver

by Robin Kanattu Thomas

  • 119,356 views
  • 22 comments
  • 160 respects
Add projectSign up / Login