2 arduinos talking

Hi, I have just bought 2 arduinos for a project for my dads car. I am going to be using them for steering wheel mounted buttons to control his stereo.

No I have written the code and would like to test it, how ever the wireless receiver and transmitter have not arrived yet.

What I would like to do is wire the to boards together and make 1 send information and the other recieve it. Once the info has been received I would like it to be outputted through the serial monitor.

Is there a way to make aurduinos talk when they are wired together some how.

Regards Matt

You can wire serial tx of one board to the serial rx of the other and vice-versa, and use the Serial api, or do the same using two other digital pins and use the NewSoftSerial library.

You could use the software serial library on each arduino, and a couple of wires between them on two different pins (one for transmit, the other for receive) - for instance:

On Arduino1, run a wire from pin 2 to pin 3 on Arduino2; then run a wire from Arduino1 pin 3 to Arduino2 pin 2.

Then code on Arduino1:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // setup software serial for output
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);
}

void loop() {
  mySerial.print("X");
}

Then code on Arduino2:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // setup software serial for input
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  mySerial.begin(9600);

  // setup hardware serial port
  Serial.begin(9600);      
}

void loop() {
  char someChar = mySerial.read();
  Serial.println(someChar);
}

Your USB cable should be hooked to Arduino2 to see the output of the hardware serial port (the above should work - but I haven’t tested it, its all “off the cuff”). You can find more information on SofwareSerial here:

http://arduino.cc/en/Reference/SoftwareSerial

Hope this helps…

:slight_smile:

Ack - doh; NewSoftSerial has kinda "taken over" SoftwareSerial - IIRC - like mromani recommends (told you it was "off the cuff"!) - both work pretty much the same, though - you should just have to change the instantiation and the library include...

:)

guys, thanks so much for your responses, my code was very similar I. When I look in the serial monitor now, there are a load of random characters, not just the "X" as expected? Is this a background noise issue?

Regards

Make sure the serial monitor speed in the dropdown box matches the one you specify in the begin() call of Serial or NewSoftSerial.

Also, remember that the Arduino pins RX and TX are shared with the FTDI chip (i.e. the USB serial port), so if you use those to make the two arduinos talk to each other there might be problems (haven't tried myself though).

I suggest you leave the hw serial (i.e. pins TX and RX) alone and use two other pins with the software serial lib.

HTH