Serial Comunication between Arduinos – With Wire & Wireless

— please check an updated code sample on the following post —
http://lab.guilhermemartins.net/2013/08/01/new-basic-arduino-serial-communication/

 

I´ve created this example to make two Arduinos talk in a simple and clear fashion.
You will see below an example with wires, and another one without wires.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Setup with TX and RX pins connected by wire:

On the sender Arduino there is a potenciometer, the values are read and sent through the serial port.
On the receiver Arduino one Led intensity will change accordingly the pot value.

Tx on Arduino#1 -> connect to ->  Rx  on Arduino#2
Rx on Arduino#1 -> connect to ->  Tx on Arduino#2

NOTE:
Don´t forget to disconnect Tx / Rx wires before upload

// SENDER

int analogValue5, val5;

void setup() {
// Serial port enable
Serial.begin(19200);
}

void loop() {
// read analog pin 5
analogValue5 = analogRead(5);

// remap values from the analogValue5 variable to 0 / 255
val5 = map(analogValue5, 0, 1023, 0, 255);

// send the value to the serial port
Serial.println(val5, BYTE);

}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// RECIEVER

byte incomingByte;

void setup() {
// Serial port enable
Serial.begin(19200);

// declare pin 11 as output, this is the LED
pinMode (11, OUTPUT);
}

void loop() {

// if there is bytes available coming from the serial port
if (Serial.available()) {

// set the values to the ‘incomingByte’ variable
incomingByte = Serial.read();

// write the value to the pin 11
analogWrite(11, int(incomingByte));

}
}


– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

Wireless setup with XBee modules:

Recently I got two XBee modules, and here is the setup to make them talk to each other:
As a start I used 2 potenciometers to control the intensity of two LEDs and It works pretty well.  :]

I´m using this adapters to connect the XBee to the circuit. They are very simple to assemble and to use.

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// SENDER

int analogValue2, analogValue5, val2, val5;

void setup()
{
// start serial port at 19200 bps
Serial.begin(19200);
}

void loop()
{
// read analog input
analogValue2 = analogRead(2);
analogValue5 = analogRead(5);

// remap values

val2 = map(analogValue2, 0, 1023, 253, 0);  // 254 and 255 for SYNC
val5 = map(analogValue5, 0, 1023, 253, 0);

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);

Serial.print(255, BYTE); //SYNC char
Serial.print(val5, BYTE);

delay(150);
}

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –

// RECIEVER

byte incomingByte, sensor1, sensor2;

void setup() {

// start serial port at 19200 bps
Serial.begin(19200);
Serial.println(“Ready!”);

// led pins

pinMode (5, OUTPUT);
pinMode (6, OUTPUT);

delay(1000);

}

void loop() {
if (Serial.available()) {   // are there any bytes available on the serial port ???

// assign bytes to the var ‘incomingByte’
incomingByte = Serial.read();

Serial.print(int(incomingByte));

// from now on is pretty clear I guess   :)

if ((int(incomingByte) == 254)) {
sensor1 = Serial.read();
Serial.print(“Sensor 1 = “);
Serial.print(int(sensor1));
}

if ((int(incomingByte) == 255)) {
sensor2 = Serial.read();
Serial.print(“  Sensor 2 = “);
Serial.print(int(sensor2));
}
}

analogWrite (5, sensor1);
analogWrite (6, sensor2);
}