0

I am using two XBee Pro S2B modules. One (Router) connected to the laptop via a UART cable. The other (Coordinator) connected to an Arduino Mega with the sketch below. All I need it to do is turn a LED on and off and display the status in the IDE serial monitor. I am using Arduino IDE (v1.6.0).

Arduino sketch:

#include <SoftwareSerial.h>

SoftwareSerial xbee (2,3);

int myData = 0;
int ledPin = 13;

void setup()
{
    Serial.begin(9600);
    xbee.begin(Serial);

    pinMode(ledPin, OUTPUT);
}

void loop()
{
    if (Serial.available())
    {
         int myData = Serial.read();

         if(myData == '1')
         {
             digitalWrite(ledPin,HIGH);
             Serial.write(myData, DEC);
         }
         if(myData == '2')
         {
             digitalWrite(ledPin,LOW);
             Serial.write(myData, DEC);
         }
     }
 }

Where did I go wrong?

1

You have two serial connections. One for the main USB of the board and one for the XBee.

So you need to read from the XBee and write to the serial USB?

I don't know what speed you have configured for your XBee so I have set it to 9600.

This example reads from the XBee and writes to the Serial/USB port

#include <SoftwareSerial.h>

SoftwareSerial xbee (2,3);

int myData = 0;
int ledPin = 13;

void setup()
{
    Serial.begin(9600);
    xbee.begin(9600);

    pinMode(ledPin, OUTPUT);
}

void loop()
{
    if (xbee.available())
    {

         int myData = xbee.read();

         Serial.print("Data has arrived on the Arduino");
         Serial.write(myData);
         Serial.println();

         if(myData == '1')
         {
             digitalWrite(ledPin,HIGH);
             Serial.write(myData);
         }
         if(myData == '2')
         {
             digitalWrite(ledPin,LOW);
             Serial.write(myData);
         }
     }
 }

Note: Your example also used Serial.write() with the wrong argument DEC which applies to Serial.print(). X-CTU and Arduino both send ascii which is also what the Serial.print() does. However the Serial.read() just reads the byte so we need to use Serial.write() to send the same bytes back to the other end.

You can also make life a bit easier by replacing xbee.available() with Serial.available(). Then use the Arduino IDE serial monitor to both send and receive. This means that you just use the one USB connection to prove the code.

  • Hallo, My speed is 9600. I copied the program but when i tried to validate and upload i get the following error " C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:53:20: note: no known conversion for argument 1 from 'int' to 'const uint8_t* {aka const unsigned char*}'" – Jaco Apr 7 '15 at 5:26
  • I have changed the "Serial.write(myData,Dec);" to "Serial.write(myData)". Then i can upload it, but it still does not work. does it matter were the router and the co ordinator goes? – Jaco Apr 7 '15 at 5:47
  • What does "still doesn't work mean"? You need to provide more info. For example are you receiving data on the other arduino? did you hook up normal serial so you can trace/debug via the pc serial window? What is the code at the other end? – Visual Micro Apr 7 '15 at 14:58
  • I am not not receiving any data on the Adruino because nothing is being displayed in the Arduino serial monitor. I have connected the one xbee to the XCTU program that needs to send a 1 or 2 across to the other xbee/arduino to be displayed in the Arduino serial monitor and opperate a LED. I do not know what "hook up normal serial" means but both are connected to the laptop. There is no code on the other end. – Jaco Apr 9 '15 at 5:41
  • I see. Well one thing to do is to add a Serial.println("Hello, some serial was found") command just inside where you check for Serial.available(). I also suggest you post the code from the other Arduino that sends the 1 or 2 – Visual Micro Apr 10 '15 at 0:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.