Instructables
Picture of I2C between Arduino's
Ever wanted to connect more Arduino's to transfer data or commands between them?
What purpose? Maybe you don't have enough I/O Pins or Analog In's. Maybe you want to do separate tasks on the same project but still want to communicate between the Arduino's. Or maybe you just need to do a simple thing more complicated. 

There are many ways to send a command to another Microcontroller:

UART -  Requires pins 0 and 1. The problem with UART is that only one device can send data to another. And also it's already in use by the USB port of the Arduino to connect to the computer.

DIGITAL I/O
-  Basically put a pin HIGH and read it on the other Arduino. But with this you have a
limited number of commands and devices.

I2C - I2C Requires Analog Pins 4 and 5 and two pull-up resistors. You can connect more than 100 Arduino's on the same 2 pins. It's simple, reliable and easy-to-use. 



 
Remove these adsRemove these ads by Signing Up

Step 1: I2C Basics

Picture of I2C Basics
I2C is a bit complex in theory. The Arduino environment makes it all simple but anyway, here is a small part of the theory you should know. 

 I2C (pronounced I-squared-C) created by Philips Semiconductors and commonly written as "I2C" stands for Inter-Integrated Circuit and allows communication of data between I2C devices over two wires. It sends information serially using one line for data (SDA) and one for clock (SCL).

Master and Slave

The I2C protocol defines the concept of master and slave devices. A master device is the device that is in charge of the bus. This device controls the clock and generates the START and STOP signals. Slave devices listen to the commands sent by the Master and respond to them.

Basic details:

Transfer rate
: 10 Kb/s (low speed) - 100Kb/s 
SDA - Serial DAta line
SCL - Serial CLock line
128 possible addresses 
16 reserved addresses
112 devices max

Devices have to share both 5V (Power) and GND (Ground)





thnx xD

thanks, if you only need one or a few nodes 1k5 works great, but the more nodes i have, i prefer higher values to lower the current. I made a I2C developer pcb with a atmega328 and i2c bus to make a BMS for lifepo4 batteries with attiyby 85 i2c nodes on each battery http://techmind.dk/arduino-singleboard/bms-developer-pcb/

frivolas1 month ago

Hi All!

Thanks cornelam for this instructable! Clear and easy to follow. I finally get a grasp on WTH is I2C.

Just a quick question, is there any particular convention to assign addresses to the slave devices? Can I just name any device as I want (1,2,3...116), or should I name them in a particular way?

Thanks!

Rifath5 months ago

finally i know about I2C by you, very very thank you

thaear6 months ago

good

schel6 months ago

PERFECT!!! EXACTLY what I was lookin' for!!

daryllukas8 months ago

Do the devices have to share the same power source? is it possible to use two different power source e.g. one Arduino is taking its power from USB, and the other
from a battery pack?

cornelam (author)  daryllukas8 months ago

Yes it is, only the Ground has to be common

Cool. I actually figured it out before seeing you reply. But thanks :)
tekkentux4 years ago
Is that pin 3 and 4 (as in the text), or pin 4 and 5 (as in the picture)? Thank you!
seems to be 4 & 5 for the uno.
binibp2 years ago
Can anyone attach de code for master and slave in dis ckt.......de two .pde attachments are not readable.....plz help....plz plz vry urgent....
odalcet binibp1 year ago
// ============= master1.pde

#include

#define LED_PIN 13
byte x = 0;

// ======================================================

void setup()
{
Wire.begin(); // Start I2C Bus as Master
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);

}

// ======================================================

void loop()
{

Wire.beginTransmission(9); // transmit to device #9
Wire.send(x); // sends x
Wire.endTransmission(); // stop transmitting
x++;
if (x > 5) x=0;
delay(450);
}
// ==================== end of file ====================




// ============= slave1.pde

#include

#define LED_PIN 13
#define LED_1 12
#define LED_2 11

int x;

// ======================================================

void setup()
{
Wire.begin(9); // Start I2C Bus as a Slave (Device Number 9)
Wire.onReceive(receiveEvent); // register event

pinMode(LED_PIN, OUTPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);

digitalWrite(LED_PIN, LOW);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);

x = 0;
}

// ======================================================

void loop()
{
//If value received is 0 blink LED 1
if (x == 0)
{
digitalWrite(LED_1, HIGH);
delay(200);
digitalWrite(LED_1, LOW);
delay(200);
}
//If value received is 1 blink LED 2
if (x == 1)
{
digitalWrite(LED_2, HIGH);
delay(200);
digitalWrite(LED_2, LOW);
delay(200);
}
}

// ======================================================

void receiveEvent(int howMany)
{
x = Wire.receive(); // receive byte as an integer
}
// ==================== end of file ====================
varuninnz2 years ago
hey man i have a quick question, I was using the same analogy 2 connect arduino UNO and mega 1280. I want the UNO to be my master because UNO works properly with processing while my mega 1280 does not work with processing.

I did as described in this tutorial http://arduino.cc/en/Tutorial/MasterWriter. Basically I connected
SDA pin 4 of UNO to Arduino Mega SDA digital pin 20
SCL pin 5 of UNO and SCL is 21 of mega
Ground UNO to ground MEGA (I dont think it matter which ground on the board you connect to)
5V of UNO to Vin of Mega.

Uploaded the master program to UNO and slave to mega as decribed on the website.
Now when I run the serial monitors I get a lot of gibberish popping up on my serial monitor. Please help as I dont understand whats going on. I dont believe the gibberish Im receiving is right?
I have a few 1k resistors lying around. Should I use your pull-up resistor theory?
act casual2 years ago
how do you enable the pull ups
my wire seen it not do it, i have 0022

hje3 years ago
In the reference design it says nothing about common ground and Vcc. You can do without :D as it is a 2-wire bus. Otherwise great introduction to the subject.
chris911ny3 years ago
I have read in some articles that the wire library will enable internal pull-ups, is that the case or we really need to add pull-ups?
jeremyvnc3 years ago
Pins 4 & 5 are SDA and SCLK for I2C
WyoJustin3 years ago
Thanks for taking the time to post this! It is just what I was looking for.

Justin
easydaman4 years ago
Nice! Thanks man!
A little sidenote for when working with an Arduino Mega: Comm-Pins are 20 and 21. But well... figuring that out takes 5 minutes. On the other hand: you just saved them ;-)
amando964 years ago
 sweet, making an i2c controlled motor driver to learn some of this...
cornelam (author) 4 years ago
 Thank You!
The fact is that you don't need an extra wire per slave.  :-)
That's the beauty of I2C. You simply put on each slave unique address and put the master to talk with that specific address...and it works. Simple and reliable. 
:-)
You are right (and I was wrong:().
I mixed I2C and SPI (Serial Peripheral Interface) where you need a chip select per slave.
Thanks for the instructable anyway.

You need to reply direct to a comment or the person won't get notice you wrote a reply.
smessud4 years ago
This is a simple case where there is only one slave.
If you add more slaves on the bus, you need an extra wire per slave (or some multiplexing circuit).
Apart from that, a nice an clear instructable.