Go Down

Topic: Arduino Mega and Xbee serial communication (Read 8436 times) previous topic - next topic

shayaan123

Hi,

I am using a Arduino Mega 2560 and a 2 Xbee series1. 1 Xbee is connected to the arduino mega using a breadboard with RX and TX pins (19, 18). The other Xbee is connected to the laptop directly. The problem is that i can send data from the Xbee connected to the arduino however, i cannot receive any data.
 
The code im using works with arduino uno with pins (2, 3) 
Code: [Select]
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }
}


Any help would be appreciated.

Thanks

ieee488

The Arduino Mega 2560 has hardware serial at 18 and 19 and at 16 and 17 and at 14 and 15. That is one of the main advantages of the Mega.


shayaan123

So what should i do to receive the data on the serial of arduino.

ieee488

Your current sketch is using the SoftwareSerial library which is conflicting with the hardware serial.

shayaan123


hieifcb

Hi,

I am using a Arduino Mega 2560 and a 2 Xbee series1. 1 Xbee is connected to the arduino mega using a breadboard with RX and TX pins (19, 18). The other Xbee is connected to the laptop directly. The problem is that i can send data from the Xbee connected to the arduino however, i cannot receive any data.
 
The code im using works with arduino uno with pins (2, 3) 
Code: [Select]
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }
}


Any help would be appreciated.

Thanks
How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.

Robin2

#6
Jun 12, 2016, 02:31 pm Last Edit: Jun 12, 2016, 02:33 pm by Robin2
How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.
On a Mega pins 18 and 19 are Serial1 so use that in the places where the code uses XBee. For example
Serial1.begin(9600) in place of XBee.begin(9600)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

shayaan123

How did you solve this?
I am also using an Arduino Mega with pin 18 and 19. Running the exact example code you are using.
But I can see nothing in my ArduinoIDE serial monitor.
Instead of using pins 18 and 19, i used pins 10 and 12 for RX and TX respectively. Try changing the RX and TX pins to something similar.
Also make sure your pin connections are proper and fixed as most of my problem lied with the connections of the wire.



Regards,
Shayaan

Robin2

i used pins 10 and 12 for RX and TX respectively
They are not HardwareSerial pins on a Mega.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

fordbuff39

Could you post your resulting sketch to another newbie, I thought I was following along, but couldn't get functioning.

I'm using a Mega with XBee Series 2 on a Sparkfun shield.  Aren't some jumpers needed??

I, also, have a general question:  Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?

Of all the digging, this thread seems to be the most straight forward>

Thanks

PaulS

Quote
on a Sparkfun shield.
An ethernet shield? A GSM shield? A link would be far more useful than this hand-waving.

Quote
Aren't some jumpers needed??
Your question mark key needs some lubrication. It is sticking!!!!!!!!!!!!!

Quote
Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?
The PC doesn't run sketches, so, no.
The art of getting good answers lies in asking good questions.

Robin2

I, also, have a general question:  Is there a way to run 2 sketches from the same PC showing a serial monitor for each session?
If you mean using 2 different Arduinos the answer is YES.

Quote
I thought I was following along, but couldn't get functioning.
Post the code you have tried.

And please use the code button </>
Code: [Select]
so your code looks like thisand is easy to copy to a text editor

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

fordbuff39

The code below is my starting point, hopefully, posted correctly.   Should have included is the post: One Ardunio is configured as Admin API 2, a second Ardunio is Router API 2, and third is USB on a Digi XBIB-U as Router API 2.

I want to get the Ardunio(s) going first, the Admin is going into a remote control box to operate the other Ardunio Router in a robot.  At some point, I'll get the USB Router involved.

The shield is a Sparkfun XBee shield.

And, yes two Ardunios on the same PC.  The intent is to get the conversation going between the Ardunios before putting into the controller and robot.


Code: [Select]


// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(19, 18); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  if (Serial1.available())
  { // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial1.read());
  }
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Serial1.write(XBee.read());
  }
}


Thanks

PaulS

Quote
The code below is my starting point
On the Mega? Why are you using SoftwareSerial to talk to the XBee when the Mega has 4 hardware serial ports?
The art of getting good answers lies in asking good questions.

Robin2

And, yes two Ardunios on the same PC.  The intent is to get the conversation going between the Ardunios before putting into the controller and robot.
If you open 2 instances of the Arduino IDE (open them from the operating system) each one can work with one of the Arduinos.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up