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)





1-40 of 49Next »
rtorres2810 days ago

Hi, I'm having problems, with this stuff. I like to communicate an arduino Mega, UNO and 4 IR temperature sensors ( mlx90614 that use I2C communication ). The Uno will check temperature and send 1 byte if detect a temperature over the range that I will specify and 0 if not. The UNO and the sensors are both slaves (I dont know if this affect), and when I connect only UNO and Mega they communicate passing any data I want, If I connect UNO and the sensors it detect temperature, but when I communicate the 2 arduinos and the sensors the comunication stops and the sensors do not detect anything. How can I fix this?

CarnefixN1 month ago

Hi. I want to use this System with multiple Arduino Mega 2560.

From what i have understood, I don't need the Voltage connection as long as the devices get the same reference Voltage. So I would only need the CLOCK, DATA and GROUND connection.

Is that correct?

cornelam (author)  CarnefixN1 month ago
Hi, that is correct.
However, if you have some spare time, test it with and without the resistors and give me some feedback on it.

Thank you!

Best regards,
Cornel

Hi cornelam,

I looked into the wiring of the Mega 2560 board itself and found that the SDA and SCL already have each a 10kOhm resistance to 5V build in.

I work just fine without the external resistors you mentioned.

Greets CarnefixN

CarnefixN19 days ago

Hello cornelam,

I connected my 2 Mega 2560 together. The problem is that both of them have the same real time clock 'DS1307' which is in the I2C connection.

It has a fixed address 0x68(104). Is there a way to deactivate one without having to remove them?

Greets CarnefixN

Good Day Sir. Thank you for the wonderful guide regarding this protocol. I have tried sending data via I2C, arduino to arduino. numbers that is, or single character data's. If suppose I would like to send an array of characters, say INITIATE_PROTOCOL to another arduino in I2C, how would I assign the variables? will it be char*? Hoping for your reply. Thank you a lot again.

cornelam (author)  donaldece2k1521 days ago
Hello,

Thank you for the wonderful words.
You can attempt the string class, it basically implements the char pointer.
http://arduino.cc/en/Reference/String

Tell me if it worked.
Have fun!

Best regards,
Cornel

hello,

i am doing work on arduino for the first time i want to
know that if i have 3 slave ..and all of them are sender and one master
receiver how master will address them ...i want code frm master side..

dan30081 month ago

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

is there a reason for this? couldnt I just have both devices powered at the same voltage?

They have to get the same voltage reference.

akospenda1 month ago

Hey, I'm using this as comms between 2 arduinos. I got it to respond to a photoresistor via the slave but when I tried to add ultrasonic sensor they interact and neither of them work, any idea why?

ðkital1 month ago

hi i just wanna ask if i can make an arduino as master at the same time as slave on both arduino?

or can i send data from master to slave? and slave to master?

kinda confused.,

cornelam (author)  ðkital1 month ago
Hello,

The Arduino only has one I2C Bus, so you can only be either a Master or a Slave at any particular time.
A slave cannot write to a Master but a Master can request data from a slave. Here is an example:
http://arduino.cc/en/Tutorial/MasterReader

Best regards,
Cornel Amariei

I just came across this instructable today and while the basics work fine, I found out the hard way that you don't need pull-up resistors anymore. In fact it doesn't work if you use them. The Arduinos I tried were Uno, Due and Leonardo and neither of them requires resistors between 5V and SDA or SCL. It's also prudent to look up the SDA and SCL pin mapping for the different types of Arduino (ie: Leonardo: 2 and 3, Uno: 4 and 5, Due, 20 and 21) in the documentation of the wire library: http://arduino.cc/en/pmwiki.php?n=Reference/Wire

Hi

You might be right about the no need for doing it, but Im using it with Uno and it works fine for me.

Hi. What is max distance between two arduinos allowable ? thank you

ImeshU4 months ago

Hi... thank you very much for sharing mach valuable information... but i'm having trouble using pin4 and pin5 as i am using a gsm shield.. it consumes pin 4 and pin 5...is there any possibility to use pin 9 and pin 10 or any other pin couple ??? i am new to arduino...thank you very much again...

Find out the address of the GSM shield comms and use any different address. Just solder the header or wires to A4 and A5 and connect to another Arduino.

frivolas6 months 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!

Jayw2 frivolas3 months ago

You can use any address and it does not have to be in any particular order (although some are reserved; look it up online), although I tend to name them consequetely for the ease of programming :)

casonye3 months ago

Hi, I need a schematics for USING ARDUINO MICRO-CONTROLLER TO MANAGE THE MOVEMENT OF A FOUR BIT BINARY CODE OVER AN OPTICAL LINK

pkgdelion3 months ago

still need help!!!

cornelam (author)  pkgdelion3 months ago
Hello,
How can I help? :-)

ibenkos4 months ago
Smart idea! Thanks for shearig :)

I'm having trouble with flickering in my common cathode 4-digit 7-segment display as my code gets more complex. I've decided to drive it with a dedicated AVR chip that I will interface with my Arduino. That is a perfect example of how "I2C between Arduinos" is extremely handy. The I2C slave doesn't have to be a full Arduino. There are tons of examples of how to program ATTiny and ATMega microprocessors with the Arduino IDE. Making your own I2C peripheral is now pretty easy. Thanks for the info!

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/

Rifath10 months ago

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

thaear11 months ago

good

schel1 year ago

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

daryllukas1 year 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)  daryllukas1 year 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.
binibp3 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 binibp2 years 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.
1-40 of 49Next »