PARTS LIBRARY :: Arduino -> Arduino Wired Communication

PARTS NEEDED

   2x Arduino

   2x Jumper Cable

 

LIBRARIES USED

   NewSoftSerial

Method
This page shows you how to communicate between two arduinos using a serial interface. We will set two serial ports up on each arduino, one for communicating with the other arduino and the other so we can see what's going on. One arduino will send data to the other, which will show it on its other serial interface, and we'll look at it using the serial monitor in the Arduino software.

We will create a serial port using New Soft Serial making sure we sue the same baud rate at both ends.

Circuit
Connect digital pin 3 to digital pin 3 on the second arduino.
Connect GND pin to GND pin on the second arduino.
You must link the grounds.

Code
First Arduino:

#include <NewSoftSerial.h>
NewSoftSerial sender(2,3);
int i = 0;  
void setup() 
{
  sender.begin(9600);
  Serial.begin(9600);
  delay(1000);
}

void loop()
{
  sender.print(i);
  sender.print (", ");
  Serial.print(i);
  Serial.print (", ");
  i++;
  delay(1000);
}

When you have uploaded this to the arduino, open the serial monitor to see what is being sent.

Second Arduino:

#include <NewSoftSerial.h>
NewSoftSerial recvr(3, 2);

void setup() {
  recvr.begin(9600);
  Serial.begin(9600);
}

void loop() {
  if (recvr.available() > 0) {
    Serial.print((char)recvr.read());
  }
}

With the first arduino running, upload this code to the second arduino and open the serial monitor. What you see has been sent from the first arduino.


© Sam Rusling 2011