Maybe sometimes we want to share the workload of one Arduino with another. Or maybe we want more digital or analog pins. Inter-Integrated Circuit or I2C (pronounced I squared C) is the best solution.

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

Here, we will make an I2C bus using two Arduinos. 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.

In order to build this we need the following "ingredients":

  • 2 Arduinos
  • Jumper cables
This instructable and many more can be found in my Arduino Development Cookbookavailable here. :D

Step 1: How to Connect Them

Follow these steps to connect two Arduino UNOs using I2C:

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

We also have a schematic and a "breadboard" implementation, both easy to follow. Luckily, it's a simple implementation.

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!

Step 2: Code

The following code is split in two parts: the master code and the slave code, which run on two different Arduinos. First, let's take a look at the master code:

// Include the required Wire library for I2C<br>#include 
int x = 0;
void setup() {
  // Start the I2C Bus as Master
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

And here is the slave code that interprets the characters sent from the master:

// Include the required Wire library for I2C<br>#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
  // Attach a function to trigger when something is received.
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);
    digitalWrite(LED, LOW);
  //If value received is 3 blink LED for 400 ms
  if (x == '3') {
    digitalWrite(LED, HIGH);
    digitalWrite(LED, LOW);

Step 3: Code Breakdown

First, let's look at the master. We need to include the required Wire.h library:

#include <Wire.h> 

Then, in the setup function, we begin the I2C bus using the Wire.begin() function. If no argument is provided in the function, Arduino will start as a master.

Lastly, we send a character x, which is between 0 and 5. We use the following functions to
begin a transmission to the device with the address 9, write the character, and then stop the transmission:

Wire.beginTransmission(9); // transmit to device #9
Wire.write(x);             // sends x
Wire.endTransmission();    // stop transmitting 

Now let's explore the slave Arduino code. We also include the Wire.h library here, but now we start the I2C bus using Wire.begin(9). The number in the argument is the address we want to use for the Arduino. All devices with address 9 will receive the transmission.

Now we need to react somehow when we receive an I2C transmission. The following function appends a trigger function whenever a character is received. Better said, whenever the Arduino receives a character on I2C, it will run the function we tell it to run:


And this is the function. Here, we simply store the value of the received character:

void receiveEvent(int bytes) { 
   x = Wire.read();

In loop(), we simply interpret that character to blink the built-in LED at different speeds depending on the received character.

Step 4: More About I2C

To briefly go through the theory, I2C requires two digital lines: Serial Data line (SDA) to transfer data and Serial Clock Line (SCL) to keep the clock. Each I2C connection can have one master and multiple slaves. A master can write to slaves and request the slaves to give data, but no slave can directly write to the master or to another slave. Every slave has a unique address on the bus, and the master needs to know the addresses of each slave it wants to access.

Each I2C bus can support up to 112 devices. All devices need to share GND. The speed is around 100 kb/s—not very fast but still respectable and quite usable. It is possible to have more than one master on a bus, but it's really complicated and generally avoided.

A lot of sensors use I2C to communicate, typically Inertial Measurement Units, barometers,
temperature sensors, and some Sonars. Remember that I2C is not designed for long cable lengths. Depending on the cable type used, 2 meters might already cause problems.

I2C is a complicated transmission protocol, but it's very useful. All Arduinos implement it, with a few differences in pin mappings:

Board I2C pins

Uno, Pro Mini A4 (SDA), A5 (SCL)

Mega, Due 20 (SDA), 21 (SCL)

Leonardo, Yun 2 (SDA), 3 (SCL)

Step 5: Connecting More Devices

If we need to connect more than two devices on an I2C bus, we just have to connect all SDA and SCL lines together. We will need the address of every slave to be addressed from the master Arduino.

Here is a video of a personal implementation using 1 master and 3 slaves.

More topics regarding Arduino communications such as Wireless Serial, SD cards or Ethernet can be found in my Arduino Development Cookbook available here. :D
<p>Is there any resistors in this circuit ??</p>
<p>For those wanting to use longer cables (I used a 5m cat5 cable), you will need to use pull up resistors. My breadboard has a 5v rail, so added a 4.7k resistor from that to pin 4 and another to pin 5. I didn't actually have a 4.7k resistor, so used 2 10k in parallel to give ~5k resistance. This worked straight away.</p>
<p>Replace in the example the conditions from x == '0' and x == '3' to x == 0 and x == 3, otherwise it does not work.</p>
<p>Hey how to know the address of arduino board ..??</p>
<p>In the slave code, we start the I2C bus using <em>Wire.begin(9)</em>. The number in the argument is the address we want to use for the Arduino. </p>
<p>What can i do to use I2c between arduinos 5-6 meters apart?</p>
<p>I think that max. distance for I2C communication is around 35 meters.</p>
<p>Max distance of I2C depends of cable capacitance, not cable length, so it could be longer or shorter than 35 meters.</p>
<p>When I enter the given code, I get an error message that says</p><p>exit status 1<br>'Wire' was not declared in this scope</p><p>I'm new to Arduino, what's missing?</p>
You have to put #include at the very top of the program (before any void) or you have brackets misplaced
<p>The most likely cause for this kind of message would be that the Arduino IDE doesn't recognize the Wire library. Try reinstalling the Wire library and / or make sure it is placed in the correct &quot;library folder&quot; on your hard drive.</p>
<p>or example ı want master arduino 1. pin home lamb on off slave arduino 2. pin saloon lamb on off how write code ? on master arduino</p>
Hello I want to cannect arduino mega and arduino nano 3.3v via i2c with SparkFun Logic Level is it possible
Hello I want to cannect arduino mega and arduino nano 3.3v via i2c with SparkFun Logic Level
<p>congratulations! Thanks!</p>
<p>helllo i am ussing these code and testing it using proteus but when i run proteus only one time data is received by slave, i have connected i2c terminal, thus can say master is properly sending the data</p>
<p>Hello!<br>I'm new to arduino and I want to start a project in which I will use a lot of vibration sensors that will change the color or turn ON/OFF the led strip or led lamp and maybe a buzzer !<br>The sensors with LEDs and the buzzer will be attached to panels that after being hit with with a ball the led will turn on or of or maybe change the color!<br><br>Can someone help with this project!<br>What arduino boards should I use, what kind of vibration sensors and so one!<br><br>Thank you very much!</p>
<p>Thanks a lot, it was really helpful for me!</p>
<p>I have connected a standard Arduino Uno with a RedBoard Uno precisely as shown. The Standard UNO is the slave and the RedBoard UNO is the master. Everything checks (numerous times) but after loading the sketches, after power up - nothing. Anybody have advice on what I left out? -Thanks</p>
<p>It would be nice to know if both boards' I2C ports are working. You can test this by connecting to any other I2C devices you may have lying around or else spend a few dollars to buy something like an I2C LCD panel on eBay.</p>
<p>If i want to do sensors in I2C, I see a lot of info that the sensors need to be &quot;I2C sensors&quot;. But is it possible to use a simple sensor like a photocell (or a LM35 temperature sensor) and put it into the slave arduino? If this is done, the slave has an address(the slave arduino address), so the &quot;address&quot; requirement is met; but if this wouldn't work, why not?</p>
<p>Here is a very good article where he did exactly that: http://www.techbitar.com/how-to-network-many-arduinos-with-sensors-using-i2c.html</p>
<p>Trying to download the PDF and I'm asked to login. I'm already logged in but unable to access the PDF. What's up with this??? </p>
<p>hello. im just learning how to use arduino. i got a question. how to connect multiple analog sensors to slave arduino uno. the master is also another arduino uno. i try it. but i only read one analog output from slave. but i want more then that. can anyone tell me how??? by the way, im using i2c and wire.h method. thanks.</p>
<p>Hi, I have set this up and it works very well transferring data from the Master to the Slave and Serial Printing it.</p><p>But, I want to 'print' it to a TFT and this does not work.</p><p>My sketch for the slave writes header text to the TFT and then should write text sent from the master.</p><p>The header writes OK, however, as soon as the wire.onReceive event is triggered the TFT screen goes blank.</p><p>Anyone know why this may be?</p><p>Bill</p>
<p>Hi Cornel,</p><p>First of all thank you very much for explaining the I2C concept using Arduino in a simple and straight forward way. I used your example and it worked like a charm (but after replacing old methods with read() and write() in new version of Arduino). </p><p>You helped me to generate interest in using the i2C communication than USART, where I struggled lots of to get the right values and not the garbled data. </p><p>However, I have a question, in my case, if master also has to receive response from the slave after they performed the action- let's called it acknowledgement. Could you pls provide your an expert advice how to achieve this? In other words, how to achieve bidirectional communication by Master and Slave ? </p><p>Currently, in UART, we can achive this by using both Tx &amp; Rx at the same time.</p><p>Once again thanks for your support and publishing this topic here.</p><p>Regards,</p><p>Pradip</p>
<p>Hi Pradip,</p><p>Just implement the according protocol. You can send data to the slave and you can read data. So send a command to provide the data at a certain address. Eventually you need to instruct the slave to provide byte after byte.</p>
<p>Hi,</p><p>I dunno any one asked before,so I asked again.</p><p>How to make Slave to Sent data to Master Arduino??</p>
Hi,<br><br>The Slave cannot directly write to the Master.<br>However, the master can request data from the Slave.<br>Here is a link to the procedure:<br>https://www.arduino.cc/en/Tutorial/MasterReader<br>
<p>hi,can anyone tell hoe to send a variable string using I2c protocol??</p><p>I want to send the variable type String name dataString using i2c protocol,wire library, Wire.write() function </p>
<p>Write a for loop from 0 to the length of the string you want to write and for each iteration just put Wire.wirte(dataString[index]); </p>
<p>hello im quite new to arduino im trying to build a slider for my camera, with a tft screen and a stepper motor, i own a leonardo and a UNO is it possible to link these 2 together for one to work with the motor and the other to work with the screen?</p>
<p>Yes, it is possible and I2C is one of the possible communication protocols which can connect them. </p>
<p>Thanks For explaining the i2c concept</p>
<p>Good Day!</p><p>This is really interesting, thanks for sharing!</p><p>Question though, how's the pin assignments ? Like, if MEGA is master then UNO is slave. D1 on Mega remains as pin 1 and the D1 of UNO would be what in coding?</p><p>Sorry if it doesn't seem clear, I'm learning Arduino :D</p>
Hello,<br><br>I don't really understand your question.<br>The digital pins have nothing to do with I2C, however, in general the pin numbers are the same across Arduinos.<br>For example, if you do digitalWrite(10, HIGH); It will do the same thing on all Arduino models - make pin 10 High...<br><br>Hope this helps.<br><br>You can also check my newly published book: Arduino Development Cookbook by Cornel Amariei on Amazon.<br><br>Best regards,<br>Cornel
<p>OH, because I have an arduino UNO where the pins are all used up (CNC machine). I plan to buy another to incorporate an LCD display to my project, therefore I'll need to connect the LCD pins on another UNO.</p><p>But since Im connecting the two Arduinos together, do I still have to program them both separately or add the LCD codes to the master?</p>
<p>hello i want to make 1 master using mega 2560 and slave using 2 unit uno,how can my mega recognize the address of the uno to avoid sending data mistake,please help me </p><p>regard</p>
Hello,<br><br>The Mega has to be set as master and it has to know the addresses of the 2 Uno slaves.<br>Each Uno slave will have a different address.<br><br>Best regards,<br>Cornel
<p>hi....i want to rum two DC servo motors with arduino uno board, but i know that one uno board can move only two servo motors, but by interfacing two arduino uno, can i able to move 4 servo motors ... with some time delays ofcourse.</p><p>help me plz..in this regard</p>
<p>Hello, I'm not sure if anyone has asked this or not, but doesn't the arduino uno have 2 pins above pin AREF labeled SCL and SDA? Can't these pins be used without interfering with other pins? Plus, is it possible to use the internal pull-ups over externat ones?</p>
<p>In the arduino uno analog pin A4 and analog pin A5 work as SDL and SCL </p>
Hello,<br><br>Yes, If you have a newer R3 board with independent SDA and SCL pins you can use those.<br>Also, no external resistors are required anymore, as they are built in.<br><br>Best regards,<br>Cornel
<p>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?</p>
<p>Hi. I want to use this System with multiple Arduino Mega 2560.</p><p>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. </p><p>Is that correct?</p>
Hi, that is correct.<br>However, if you have some spare time, test it with and without the resistors and give me some feedback on it.<br><br>Thank you!<br><br>Best regards,<br>Cornel
<p>Hi cornelam,</p><p>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.</p><p>I work just fine without the external resistors you mentioned.</p><p>Greets CarnefixN</p>
<p>Hello cornelam,</p><p>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.</p><p>It has a fixed address 0x68(104). Is there a way to deactivate one without having to remove them?</p><p>Greets CarnefixN</p>

About This Instructable




Bio: Inventor, Author, Senior R&D Engineer, Entrepreneur, Forbes 30 Under 30
More by cornelam:Arduino Servo Motors Arduino Distance Sensors Arduino Stepper Motors 
Add instructable to: