Arduino Communicating with Another Arduino
Yes it is possible to make 2 Arduinos to get talking with each other. Having Arduino-Arduino communication can
be useful for many projects, such as having one Arduino for displaying a detailed output in a GLCD
and another sensing the surroundins and then relaying the inputs to the
other. This can be done in several methods, using I2C and
Serial.
This article will focus on Arduino to Arduino communication through the serial ports (RX and TX or pin 1 & 0 of Arduino UNO particularly, and if its MEGA, you can connect it to any of the pair of RX & TX pins).
The data is sent through serial communication ports in binary form, that is in bytes. Arduino reads Bytes by Bytes sent by the other. Even the characters sent through the serial port of Arduino, first it is converted to data bytes then transmitted & then decoded as a character.
To get an idea about it, there are several example codes available for the serial communication using arduino, just go to "File >> Examples >> Communication" in the Arduino IDE.
One of the code to upload on the Arduino for Serial Communication is:
void setup()
{ Serial.begin(4800); } void loop() { Serial.print('1'); delay(500); Serial.print('0'); delay(500); }
The above code will initialize the serial communication port with a BAUD rate of 4800bps.
And in one second it will send a character value of '1' in one half of the second & in the other half it will send '0'. That is the wave generated by it will have the frequency of 1Hz.
Sender Code
The sender code changes characters into byte stream &, if necessary, it changes number values into characters before
turning it into bytes. Below is a sample of the Sender code:
char xyz[3]; void setup()
{ Serial.begin(4800); //baud rate define } void loop()
{ int value=888; //Or a sensor output from an Input Analog Pin itoa(value, str, 10); //Turn any value into a character array Serial.write(str, 3); //to send the character through the serial port }
Arduino to Arduino Connection For Serial Communication ( Schematic Created Through Fritzing Software ) |
Receiver Code
The
receiver will then receive the byte array & decode it. Below is the code to be uploaded in the receiver Arduino board. The receiver Arduino will receive the serial data, and then print to the Serial Monitor so that the user can view it
char xyz[3]; void setup()
{ Serial.begin(4800); Serial1.begin(4800); // this notation is for arduino MEGA only } void loop()
{ int i=0; if (Serial1.available())
{ delay(100); while(Serial1.available() && i<3)
{ str[i++] = Serial1.read(); //read the string of length 4
} str[i++]='\0';
} if(i>0)
{ Serial.println(str,3); } }
Thus 2nd arduino, will display on the serial monitor
whatever the the 1st arduino will send.
The above Experiment can be also done effectively through I2C pins.
NOTE:- The BAUD rate or Serial Communication rate should be equal in order to make them communicate successfully.
One of the reader commented: <Tore Eriksson>
If I want just one-way communication from one Arduino to another can I exclude the other wire or is there still some handshaking that requires wires both ways? The answer is you need just one wire (plus Ground, of course!) if you want just one-way communication.
If anyone wishes to make a very stripped down example with no confusing extra information, I would have liked to see a one-way communication example with only one signal wire.
In my experiment, I had a switch on one pin of the sending Arduino. Depending on how long time I pressed that switch, the pin 13 LED flashed a number of times. After releasing the button, the first Arduino sent a message to the second Arduino, telling the number of flashes, and then the second Arduino repeated the flash sequence on its pin 13 LED.
One of the reader commented: <Tore Eriksson>
If I want just one-way communication from one Arduino to another can I exclude the other wire or is there still some handshaking that requires wires both ways? The answer is you need just one wire (plus Ground, of course!) if you want just one-way communication.
If anyone wishes to make a very stripped down example with no confusing extra information, I would have liked to see a one-way communication example with only one signal wire.
In my experiment, I had a switch on one pin of the sending Arduino. Depending on how long time I pressed that switch, the pin 13 LED flashed a number of times. After releasing the button, the first Arduino sent a message to the second Arduino, telling the number of flashes, and then the second Arduino repeated the flash sequence on its pin 13 LED.