I now have a big collection of various Arduinos so I wanted to try and let them communicate with each other. There are several options to let 2 Arduinos communicate with each other. All Arduinos have serial, I2C and SPI busses which can be used for communication. The easiest way to get them to communicate is I2C and serial. I chose I2C, don’t ask me why. The Arduino IDE comes with the Wire library to use the I2C bus. I connected the I2C pins of 2 Bajduino 1284’s, these are digital pin 16 and 17 on the ATmega1284 with mighty 1284p bootloader. You also need to connect the ground of the 2 Arduinos with each other. It is recommended to use 2 pull up resistors for the I2C pins. I tried it without them and it worked but if you are building a permanent setup its better to add the 2 resistors. I tested the example Wire sketches and got the 2 Bajduinos talking to each other. The Arduino wire library just sends and receives bytes, this is nice but I just want to sent and receive some integers. After looking around on the internet I stumbled on Nick Gammons website where I found a good explanation of the I2C bus. Near the bottom of the page is a simple Arduino library which makes I2C communication very easy if all you want to do is send and receive float, int or long type. The library is called I2C_anything. I tried it out by connecting a small analog joystick to one Arduino and then sent the values of the joystick to the other Arduino.
This are the sketches I used:
Master sketch:
// http://www.bajdi.com // I2C communication between 2 Arduino // Analog joystick connected to analog pins A0 and A1 // Values from joystick are sent to another Arduino (the slave) #include <Wire.h> #include <I2C_Anything.h> const byte SLAVE_ADDRESS = 42; void setup() { Wire.begin (); } // end of setup void loop() { int X = analogRead(A0); int Y = analogRead(A1); Wire.beginTransmission (SLAVE_ADDRESS); I2C_writeAnything (X); I2C_writeAnything (Y); Wire.endTransmission (); }
Slave sketch:
// http://www.bajdi.com // I2C communication between 2 Arduino // Slave receives the integers X and Y from the master // Values X and Y are printed to the serial monitor #include <Wire.h> #include <I2C_Anything.h> const byte MY_ADDRESS = 42; void setup() { Wire.begin (MY_ADDRESS); Serial.begin (115200); Wire.onReceive (receiveEvent); } // end of setup volatile boolean haveData = false; volatile int X; volatile int Y; void loop() { if (haveData) { Serial.print ("Received X = "); Serial.println (X); Serial.print ("Received Y = "); Serial.println (Y); haveData = false; } // end if haveData } // end of loop // called by interrupt service routine when incoming data arrives void receiveEvent (int howMany) { if (howMany >= (sizeof X) + (sizeof Y)) { I2C_readAnything (X); I2C_readAnything (Y); haveData = true; } }
Thank you so very much for this page. VERY,VERY helpful.
Thanxs
Oh my god this helped me! I’ve been struggling for days trying to send a 5 digit number, I just about fell down when I found this!
Thank you, thank you, thank you!
I did not find library I2C_ Anything ?
You can download it on this page: http://gammon.com.au/i2c