i2c – inter integrated circuit

Sometimes you need to connect 2(or even more!) micrcontrollers to communicate with each other, thus, there would be more i/o or to control separate units.
In this post, I will cove connecting 2 arduino boards by I2C.

For this experiment, you will need:

  1. 2x arduino boards(same type or different)
  2. hook-up wires / jumper wires
  3. toggle switch, or any ON/OFF switch is fine
  4. LED

one of the 2 arduino boards is going to be the ‘master’ and the latter is the ‘slave’, basically the master tells the slave what to do, and the slave behave accordingly.

In order to connect the 2 boards, we’ll need to connect the analog pin 4 and analog pin 5 in both boards with each other(pin 4 on the first board with pin 4 on the second board and same thing with pin 5). One pin is foing to be the SCK(Serial clock) and the other one is the SDA(Serial data).
Also, connected a common ground on both boards:

The final connection should look something like this

Alright, now connecting the switch and the LED: the switch is going to be connected to a digital pin on the Master board and the LED on the digital pin of the Slave board:

left board is the master and right one is the slave. Notice that the LED is connected to pin 13 and the switch is connected to pin 7
left board is the master and right one is the slave.
Notice that the LED is connected to pin 13 and the switch is connected to pin 7

Here is the code for the master board:

#include <Wire.h>
int switchPin = 7;
void setup()
void loop()
  Wire.beginTransmission(4); // the adress of the other board(slave)
  Wire.write("switchMode is ");        
  Wire.write(digitalRead(switchPin)); // sends data
  Wire.endTransmission();    // stop transmitting

and the code for the slave:

#include <Wire.h>

int ledPin = 13;
int switchMode =0; //contains the recieved status of  the switch
void setup()
  Wire.begin(4); // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);  // start serial for output


void loop()
  if(switchMode == 1) digitalWrite(13, HIGH);
  else if(switchMode == 0) digitalWrite(13,LOW);


// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
  switchMode = Wire.read();    // receive byte as an integer
  Serial.println(switchMode);         // print the integer

when uploading both sketches to the boards accordingly, what should happen is when you change the switch mode from ‘high’ to ‘low’, the LED on the other board should light, and vice verse.

that was basically i2c connection, simply put, although is might be complex in theory!


2 thoughts on “i2c – inter integrated circuit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: