XBee Setup

181214KA

Hello!

I’m working with  2 Series 1 actual-antenna XBees and an Arduino MEGA + MEGA sensor shield.

I’ve worked with XBees before, so I was leaning towards using them… but I wasn’t really sure if I’d be able to send packets of data, and received  suggestions to look into something less long-distance for my applications. Turns out it is possible to send packets with XBees! I talked to a couple of people about suggestions for the long-distance thing, and it didn’t seem too big a deal–XBees can be easily programmed to communicate on a different channel, and there are a huge range of options. More on setting up XBees here. I’ve set up the ones I’m using so both can send + receive.

(Note that you’ll need to download a software called XTCU. In case you inherited a few XBees, as I did initially, and are having trouble, consult here and here… the second link is useful if you’re XBees are the similar to the ones I’m using, but in general look into updating XBee firmware + reconfiguring them).

How to Connect XBees to Arduino MEGA

Unlike other Arduinos–particulary the Uno, which I’ve used for such purposes– the Arduino MEGA has multiple hardware serial ports. From what I understand, when you #include <Softwareserial.h> , etc. you’re basically configuring some other pins on the Uno, for example, to do a similar function to what pins on the MEGA have already been hardwired to do. (This forum tries to explain. Key word being tries).

What does this mean for your coding? (I was initially confused by forums like this and this , but maybe you’ll find them useful/interesting?) Turns out you really don’t need to do anything special: wire it up correctly, and then instead of writing XBee.whatever, refer to Serial and everything works! Here’s the website I referenced, but I’ll also paste the code below. (You’ll note that I’ve wired up to Serial3, so that’s what I’m referencing when I want to have the XBee do something. The regular Serial just refers to the Serial Monitor).

Wiring

In my case, I’m trying to communicate between computer and robot via XBee, so I only need to wire an XBee to the the MEGA sensor shield (semi-helpful pinouts here).  Essentially, the Hardware Serial ports on the MEGA (Serial 1 on pins 19 (RX) and 18 (TX), Serial 2 on pins 17 (RX) and 16 (TX), and Serial 3 on pins 15 (RX) and 14 (TX)) need to be wired to the proper pins on the XBee. Connect the XBee’s DOUT to one of MEGA’s RX, and then the DIN to a corresponding TX. Make sure the XBee’s VIN is connected to one of the Arduino’s 3.3V pins–not the V pin you’ll see in every row on the sensor shield. And don’t forget about GND. 🙂

Here’s a video of it working:

(It’ll download): 181215KA_XBee_Test

Code:

void setup() {
 // initialize serial ports
 Serial.begin(9600); // USB serial port 0
 Serial3.begin(9600); // serial port 3
}

byte rx_byte = 0; // stores received byte

void loop() {
 // check for data byte on USB serial port
 if (Serial.available()) {
 // get byte from USB serial port
 rx_byte = Serial.read();
 // send byte to serial port 3
 Serial3.write(rx_byte);
 }
 // check for data byte on serial port 3
 if (Serial3.available()) {
 // get a byte from serial port 3
 rx_byte = Serial3.read();
 // send the byte to the USB serial port
 Serial.write(rx_byte);
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *