Arduino Mega 2560 not receiving Xbee messages.

I am currently working on robot and utilize Xbee communication to send commands to the robot from Matlab via a Sparkfun USB Xbee explorer connected to to my computer and the robot's Xbee sits on a breadboard adapter and I use jumpers to connect the necessary pins. However, in the early stages I have run into some issues when trying to get the Arduino Mega 2560 to receive said commands. Mind you, I have been able to get the robot to transmit data to the computer just fine, to both Matlab as well as XCTU. I have also successfully been able to create this TX/RX command structure using a Sparkfun Xbee shield sitting on a Arduino Uno and the same PC setup, so I know it is something with my Mega's setup.

Here is a more detailed description of my setup:

Matlab or XCTU --> USB --> XBEE <===> XBEE <-- Arduino Mega 2560 (D14,D15, 3.3V, GND)

  • Both xbees have 10ed firmware on them and are operating with the default configurations, same PAN ID, channel, baud(9600), pull-up resistors enabled, etc.

Here is what I have tried in terms of troubleshooting this issue.

  1. I have tried just about every PCINT digital pin combination on the mega, even swapped the pins in each run to make sure I connected the Xbee to the Mega properly
  2. I have tried specifying the destination addresses in the Xbee firmware
  3. I have tried configuring the Mega's Xbee to broadcast a global message all Xbee's can read
  4. I have scoped the data lines and have come up with a few interesting wave forms (below)

This image shows the signal I am getting from the Uno when probing the digital pin breakout. (I know its at 5V but it still worked and I think there is something wrong with my Sparkfun shield).

This image shows the signal I am getting from the Uno when probing the corresponding pin on the micro itself (pin4 aka D2)

These two traces from the Uno appear to be exactly the same, aside from the slight warping of the display of the signal in the micro's pin trace.

This image shows the signal I am getting at the breakout pin of the Mega which is coming directly from the Xbee. As you can see it is the same message I get from probing the Uno, and at ~3.3V.

Now this is where it gets interesting, this is the trace from reading the Mega's micro pin. It seems there is an extra bit of data tacked onto the end of the message. From what it looks like, it seems to be tacking on another byte of data from somewhere. I replicated this result multiple times to verify it wasn't a one time thing.

Here is the code I am using: (mind that I have also tried to run this without the pinMode assignments as well as with every combination of INPUT and OUTPUT assignments for each pin)

#include <SoftwareSerial.h>

// Xbee serial setup
SoftwareSerial xbee (14,15); // RX,TX

// state of received data compared to expected 
boolean startState = false;
// stores incoming data
String response = String("");
// data compare 
String expected = String("START");

void setup() {
  pinMode(2, INPUT);
  pinMode(3, OUTPUT);
  // begin xbee com
  xbee.begin(9600);
}

void loop() {
   
  // loops through checking received buffer for new data to compare to expected
  if(!startState){
   
     if(xbee.available())
     {
       while(response.length() < expected.length()) {
         response += (char) xbee.read();
       }
       // If we have a match, success
       if( response == expected) {
         startState = true;
       }
     }
     delay(100);
  }
  // If we have success, start sending data every second
  if(startState) {
    xbee.print("running...");
    delay(1000); 
  }
   delay(100);
}

So in all I think there is something I am not doing properly in my setup on the Mega or possibly, but less likely, there is an issue with my Mega. I have not tried to replicate these results on a different one since I only have one Mega at the moment.

On a side note. I have also encountered the issue where I am unable to change configuration parameters using XCTU. For instance, I will change the DL or RO parameters and it will show up as changed. However, when I refresh the list, the parameter I changed reverts back to the previous value I just overwrote. I even have updated the firmware and unchecked "Force module to keep its current configuration". Missing something here as well.

Thank you for any help,

--Larry

SoftwareSerial xbee (2,3); // RX,TX

This is from the SoftwareSerial reference page.

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

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

edit: I'm not sure why you are not using a hardware serial port. You have an extra three available.

I'm not sure why you are not using a hardware serial port. You have an extra three available.

Not an issue, I am sorry I made a mistake copying my code. That is what I uploaded to my Uno with 2 and 3 for RX and TX, respectively. I already stated in the post if you had read carefully that I tried all those pins already.

I see now where you used the hardware serial port pins (Serial1) as SoftwareSerial pins. I just don't see any attempt to use the hardware serial like this:

// state of received data compared to expected 
boolean startState = false;
// stores incoming data
String response = String("");
// data compare 
String expected = String("START");

void setup() {
  // begin xbee com
  Serial1.begin(9600);
}

void loop() {
   
  // loops through checking received buffer for new data to compare to expected
  if(!startState){
   
     if(Serial1.available())
     {
       while(response.length() < expected.length()) {
         response += (char) Serial1.read();
       }
       // If we have a match, success
       if( response == expected) {
         startState = true;
       }
     }
     delay(100);
  }
  // If we have success, start sending data every second
  if(startState) {
    Serial1.print("running...");
    delay(1000); 
  }
   delay(100);
}

I just don't see any attempt to use the hardware serial like this:

I have tried running this code as well, also to no avail. Even if this did solve my issue, wouldn't I have to disconnect the Xbee from the TX/RX pins each time I want to upload a new sketch? That is less than ideal.

No. The usb is connected to Serial, not Serial1, or Serial2, or Serial3. Those are additional hardware ports you can use without interfering with sketch uploads. Use one of those. Serial1 is on D14 and D15.

Use one of those. Serial1 is on D14 and D15.

I have tried running this code as well, also to no avail.

larry12193:

Use one of those. Serial1 is on D14 and D15.

I have tried running this code as well, also to no avail.

And when you tried this, how was the Xbee shield wired? If it was my shield, I would do a little pin bending. I would bend the pins on the Xbee shield connected to D0 and D1 (Serial) so they do not insert into the Serial pins on the Mega. Then use two jumper wires to connect those pins on the shield to D14 and D15 on the Mega and use Serial1.

But that is just me...

And when you tried this, how was the Xbee shield wired?

The Xbee sits on a breadboard adapter, not on a shield, and I used jumpers to connect it to the Mega through 14/15, 16/17, 18/19 and had no luck using Serial1, Serial2, Serial3, respectively. And I made sure it was getting 3.3V.

Did you notice:
the Mega 2560 is 5v logic and the Xbee is 3.3v?
the Sparkfun Xbee shield has MOSFET logic level converters?
the Sparkfun Xbee shield uses the Arduino 5v bus and does its own 3.3v regulation?

Does your breadboard version have those features?

Solved the issue. Swapped for a new Mega 2560 and code ran fine. Thanks for all the suggestions.

Can you copy to me the code that you uses?

As you can see in my topic: Arduino Mega 2560 + Xbee Shield + WifiBee + WiflyHQ = ERROR! - Programming Questions - Arduino Forum

Im having a lot of problems to set it up correctly...

Im not finding de problem and the i dont know how to solve this situation...

May be your code give to me some light! :sweat_smile:

Thanx

I had exactly the same problem and just figured out how to solve it. I know it's a dead topic but still if someone stumbles upon this answer it could help:

Don't use SoftwareSerial.h, arduino mega has it's own extra Serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). Just initialize the Serial[#] you have connected and use it.