Using I2C as Communication Link between Arduinos

I have three arduino boards so far, but don’t get me wrong. I’m not arduino collectors, haha… as you can see here the main purpose is to learn how to do the data communication between these boards, or at least i can build a simple chatting project that communicate between arduinos. And to make it works properly, first in the head we need a communication link, which communication link we can use? There are several out there we can find, from the wired one or fantastic wireless, that most people use nowadays. From here, I will use I2C (Eye To See) as communication link between my arduinos, this is a slow wired type communication (10kbit/sec), so it required several cables to plug between arduinos.

Screen_shot_2012-10-04_at_10

I2C is a one master many slaves communication http://en.wikipedia.org/wiki/I%C2%B2C, the benefit was it can communicate between more than two arduinos, for this example i will use all the three boards: one arduino leonardo and two arduino uno(s). I2C is already built in inside the arduino board, so i don’t need to plug any I2C shield each of the boards, but just to remember I2C required two lines : SDA for data and SCL for clock synchronize the communication, as you can see I2C is a serial kind of communication link. For the leonardo board SDA located in pin 2 and SCL in pin 3 http://arduino.cc/en/Main/arduinoBoardLeonardo; for the uno board SDA located in analog 4 and SCL in analog 5 http://arduino.cc/en/Main/ArduinoBoardUno. I setup my leonardo board as a master and the two uno boards will be as slave, need help a breadboard just to make the wired communication between the boards like this picture below.

Wp_000907

For the debug purpose, each of the boards need to be connected to my laptop by using usb cable and also open three terminal applications ready. For the simple case, i need to program inside master board that will send some data to the selected slave board, very easy. Let’s start with the code for the master.

[gist https://gist.github.com/3844187 /]

One thing i like about arduino was so many libraries built inside the IDE, for this I2C communication purpose, they already provided with library named “Wire.h”, and i can just simply use it and it works properly, cool. Sketches inside the master ( or leonardo ) board, i start with statement “Wire.begin()” in order to run the I2C as a master, and for the sending data to selected slave i need to compose the validation after the read character from the terminal : type character ‘1’ for slave #1 selected, type character ‘2’ for slave #2 selected, and the rest characters will be forward to the selected slave board. To send the character was easy enough, by typing “beginTransmission” function inside the Wire first, then continues with “write” function the character that we want to send, finally to close the sending by type “endTransmission”, just like example code above.

Screen_shot_2012-10-04_at_8

Inside the terminal, i can just type ‘1’ and it will display statement “connected to slave id #1” this means that rest characters will forward to the uno board #1, like the example picture above all the strings “Hello slave one, this is master” will send to uno board #1, and if i type ‘2’ inside the terminal the master will switch to talk with uno board #2. Ok, after done with the code inside the master, lets move inside the slave one.

[gist https://gist.github.com/3844237 /]

Running as a slave inside the board, it’s simply by put statement “Wire.begin( address )” , the address parameter is in a numeric format, that will be use to define the unique address each of the slave device. Because i only have two slave boards, so each of the board i shall set slaveId with value 1 on uno board #1 and slaveId with value 2 on uno board #2. One thing about arduino’s sketch it provide callback function based on which event do u want to use, in this code i built function named “onWireReceive”, and this function will be call once there is a new inbound data coming from the master, but first must type “Wire.onReceive(onWireReceive)” to define inside the setup function. For debuging purpose, i just display all the received characters into the terminal app open in my laptop.

Screen_shot_2012-10-04_at_8
Screen_shot_2012-10-04_at_8

Fin :-)

One thought on “Using I2C as Communication Link between Arduinos

  1. Nivesh says:

    hi, I also have a leonardo and a Uno board where i have tried all codes but nothing worked yet. I have used the I2C scanner and it worked by detecting the Uno.
    Can you please help me.

    Lets say on the Leonardo as master will have a toggle switch and i want to switch the inbuilt LED on the UNO r3.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s