Skip to content

Serial Comunication between Arduinos – With Wire & Wireless

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);
}

{ 13 } Comments

  1. Jose | January 4, 2009 at 9:09 pm | Permalink

    I want to see you do the wireless part with ladyadas softserial library on pins 2 & 3. I am having problems.
    Thanks
    -Jose

  2. GUI | January 4, 2009 at 9:50 pm | Permalink

    SoftSerial library emulates rx / tx pins and if you want to have a reliable connection you should the original Serial protocol.

  3. Rico Bravo | January 6, 2009 at 2:12 am | Permalink

    Ok, got it working here is the code below for anyone that wants to do it with the AFsoftserial software:
    #include

    int LEDpin = 5;
    byte address;
    byte data;

    AFSoftSerial mySerial = AFSoftSerial(2, 3);

    void setup() {
    pinMode(LEDpin,OUTPUT);
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    Serial.println(“Chip 2 online”);
    // set the data rate for the SoftwareSerial port
    mySerial.begin(9600);

    }

    void loop() // run over and over again
    {

    if (mySerial.available()) {
    // Serial.print((char)mySerial.read()); // took this out so I do not loose a byte
    address = mySerial.read();
    Serial.print(address);
    if (address == ‘2′)
    {
    data = mySerial.read();
    if (data == ‘5′);
    {
    digitalWrite(LEDpin,HIGH);
    delay(1000);
    digitalWrite(LEDpin,LOW);
    }

    Serial.print(data);

    }

    }
    if (Serial.available()) {
    mySerial.print((char)Serial.read());
    }
    delay(50);

    }

  4. Jose | January 6, 2009 at 2:53 am | Permalink

    Oh,
    Here is the sender arduino code the above is the receiver arduino code!

    #include
    #define Button 6
    //int inPin = 6; //
    int val = 0;
    int old_val = 0;
    int state = 0;
    int val5 = 86;
    AFSoftSerial mySerial = AFSoftSerial(2, 3);

    void setup() {
    pinMode(Button,INPUT); //
    pinMode(13, OUTPUT);
    Serial.begin(9600);
    Serial.println(“Chip 1 online”);
    delay(2000);
    // set the data rate for the SoftwareSerial port
    mySerial.begin(9600);
    // mySerial.println(“Whats for dinner?”);
    }

    void loop() // run over and over again
    {
    val = digitalRead(Button); // read input value and store it. Yum, fresh
    // check if there was a transition
    if ((val == HIGH) && (old_val == LOW))
    {
    state = 1 – state;
    mySerial.println(2);
    mySerial.println(5);
    Serial.println(“Rising Edge”);
    delay(250);
    }
    old_val = val; // val is now old, let’s store it

    if (mySerial.available()) {
    Serial.print((char)mySerial.read());
    }
    if (Serial.available()) {
    mySerial.print((char)Serial.read());
    }

    delay(50);
    Serial.println(val); //

    }

  5. GUI | January 6, 2009 at 9:57 am | Permalink

    must try this!
    thanks for sharing! ;-)

  6. Shawn | February 19, 2009 at 1:30 am | Permalink

    I got our Xbee’s in the mail today. I am going to start off with your example here to try and get an understanding before attempting to use them in a robot, etc. I wish I had one of the protoshields you are using though.

  7. GUI | February 19, 2009 at 7:01 am | Permalink

    Glad to know that, If you need help just say!
    Protoshields are great, I advice you to get a bunch of them :-)

  8. wesley | August 10, 2009 at 1:38 pm | Permalink

    Hi there

    Sorry – new to arduino and trying to understand basic rx tx. I’ve set up everything (the hardwired setup) exactly like you have yet the LED only goes on or off – the pot has very little control – definitely no smooth fading. Does the value of the pot matter? No idea what i could be doing wrong?

  9. GUI | September 1, 2009 at 7:08 am | Permalink

    @ wesley
    It’s hard to tell what might be wrong with your setup! Have you double check all the connections / wirings?
    Can you send me a photo?

  10. filipe | December 31, 2009 at 2:21 pm | Permalink

    Para comunicar é preciso ter o TX, e o RX? ou dá para ter um master, ligado a um slave apenas com um TX(no master) e um RX(no slave)? só um fio?

  11. GUI | December 31, 2009 at 2:26 pm | Permalink

    Não te quero induzir em erro, mas julgo que sim, se usares apenas uma ligação de Tx (master) – Rx (slave) tens comunicação unidireccional.

  12. filipe | December 31, 2009 at 2:26 pm | Permalink

    Isto porque tenho um controlo remoto de apenas um canal, e queria com a penas um canal controlar vários servos/motores

  13. GUI | December 31, 2009 at 6:28 pm | Permalink

    sim é possivel, como pretendes fazer a distinção do sinal, ou seja, como vais fazer para mexer um servo e não outro?

Post a Comment

Your email is never published nor shared. Required fields are marked *