Connecting Arduinos together

Contents

  1. Introduction
  2. Toolkit
  3. Wiring
  4. Example Code
  5. References

Intoduction


Working with an Arduino is a really inexhaustible DIY job but what if you wish to connect two or more hardware to work with?
Having Arduino-Arduino communication can be useful to run motors and having another sense the surroundings and then relay commands to the other Arduino and other projects.
It is possible to chain Arduinos together in such a way as to get communication between the two hardware. In the following example, two Arduinos are programmed to communicate with each other via the I2C synchronous serial protocol.

In this example Arduino 1, the Master, is programmed to send 6 bytes of data every half second to a uniquely addressed Slave Arduino. Once that message is received, it can then be viewed in the Slave Arduino's serial window.

The I2C protocol involves using two wires to send and receive data: a serial clock pin (SCL) that the Arduino pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices.
As the clock pulse changes from low to high (known as the rising edge of the clock), a bit of information is transferred from the Arduino to the I2C devices over the SDA line.
When the clock pin changes from high to low (the falling edge of the clock), the called upon device transmits a bit of data back to the Arduino over the same line.
The initial eight bits (i.e. eight clock pulses) from the Master to Slaves contain the address of the device the Master wants data from. The bits after that contain the memory address on the Slave that the Master wants to read data from or write data to, and the data to be written, if any.
Each Slave device have its own unique address and both master and slave devices to take turns communicating over a the same data line line. In this way, it's possible for your Arduino to communicate with many device or other Arduinos using just two pins of your microcontroller, using each device's unique address.

Toolkit


  • 2x Arduino Boards
  • Hook-up wire

schematic for arduino connection
Figure 1 - Schematic for Arduino connection

Wiring


Master Slave
SDA (A4) A4
SCL (A5) A5
GND GND

The slave Arduino must be connected to your computer via USB. If powering the Arduinos independently is an issue, connect the 5V output of the Master Arduino to the VIN pin on the slave.

how to connect arduinos together
Figure 2 - How to connect Arduinos together

Example Code


Master Writer Code - Program for Arduino 1

void setup()
{
Wire.begin();
}
byte x = 0;
void loop()
{
 Wire.beginTransmission(4); 
 Wire.write("x is ");         
 Wire.write(x);               
 Wire.endTransmission();    
 x++;
delay(500);
}

Slave Receiver Code - Program for Arduino 2

void setup()
{
 Wire.begin(4);
 Wire.onReceive(receiveEvent);
 Serial.begin(9600);
}
void loop()
{
 delay(100);
}
void receiveEvent(int howMany)
{
 while(1 < Wire.available())
 {
   char c = Wire.read();
   Serial.print(c);
 }
 int x = Wire.read();
 Serial.println(x);
}

References


More information