# Xbee knowldege wanted

**URL:** <https://forum.arduino.cc/t/xbee-knowldege-wanted/386103>\
**Category:** General Guidance\
**Created:** [May 15, 2016, 7:08pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103 "2016-05-15T19:08:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 15, 2016, 7:08pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/1 "2016-05-15T19:08:50Z")

</div>

If you know how to work with xbees and how they communicate via Arduino I need your help. I have been messing around with these things for almost a year on and off and have had success only when using xbees without arduino's and have no idea why. If you could leave your email I would very much appreciate your help. Thank you

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [May 15, 2016, 8:15pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/2 "2016-05-15T20:15:50Z")

</div>

I've had success using XBees with Arduino, but I don't think using email is a good idea. Working things out in the forum allows everyone to benefit from the discussion.

XBees communicate via the serial ports on Arduinos. It really is that simple (provided the XBees are configured correctly).

What type of XBees do you use?

---

<div class="post-metadata">

**Author:** ![jrdoner](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@jrdoner](https://forum.arduino.cc/u/jrdoner)\
**Post date:** [May 16, 2016, 2:16am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/3 "2016-05-16T02:16:55Z")

</div>

I use the tiny little RFM-22B's in all my projects (half the size of a postage stamp, $5.60 on EBay). I've even designed a board which integrates a Nano with an RFM-22B, then brings all remaining pins out to one edge, so I can use the board to build up a radio-based project. Just took about ten resistors and two transistors to accommodate the needed level shifting. I will state only that they are very docile, easy to use, and all they ever do is work right. Almost boring.

Now I know nothing about XBee's. They look expensive. But if they communicate with a serial port, then you would definitely run into trouble with the Arduino siblings that use the hardware serial pins D0, D1, unless you establish a software serial connection, or are using a more sophisticated board (like a Mega) with multiple hardware serials.

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 16, 2016, 4:44am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/4 "2016-05-16T04:44:01Z")

</div>

Thank you for replying, and I have two series 1 xbees with two sparkfun xbee explorers, two sparkfun xbee shields, and two Arduino unos. My problem seems to be that when I use the arduino's serial to send data to another Arduino, it works, however, the other does not receive the data. To be honest I know very little about the programming language and how the serial and xbees work in general.

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [May 16, 2016, 11:28am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/5 "2016-05-16T11:28:01Z")

</div>

> Mrbobokins:  
> Thank you for replying, and I have two series 1 xbees with two sparkfun xbee explorers, two sparkfun xbee shields, and two Arduino unos. My problem seems to be that when I use the arduino's serial to send data to another Arduino, it works, however, the other does not receive the data. To be honest I know very little about the programming language and how the serial and xbees work in general.

Series 1 XBees are configured to work "out the box" and are very easy to work with.

Since you we using an Arduino Uno, you only have one serial port. I'm guessing the XBees are talking via the hardwired serial port? I'm also guessing you are verifying that you received data by printing read data via the same serial port?

This would be your problem. The solution would be to use the softserial library to allow the XBees to talk without hijacking the serial port used to talk to your PC.

Also, make sure all of the jumpers on your shield are present and are making the correct connections. Can you share pictures of your boards, shields, and code?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 16, 2016, 2:50pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/6 "2016-05-16T14:50:05Z")

</div>

> It really is that simple (provided the XBees are configured correctly).

And the XBee is connected to the Arduino properly (via a decent shield is easiest). So, how ARE the XBees connected to the Arduino(s)? What IS the problem?

> Series 1 XBees are configured to work "out the box" and are very easy to work with.

But, they work even better when properly configured.

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 16, 2016, 9:53pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/7 "2016-05-16T21:53:42Z")

</div>

My project is an attempt to build this

[![](https://europe1.discourse-cdn.com/arduino/original/4X/2/2/f/22f7bdd37e9a5c2d4826a2e9049564436ce03edb.jpeg "Tutorial 09 for Arduino: XBee Wireless Communication") ](https://www.youtube.com/watch?v=vKVNmA8C6m8)

I've hooked all of the parts correctly. When I uploaded the sketch to each Arduino, the shield was in DLINE mode and I set it to UART when trying to use it. My problem is that when I turn the potentiometer, the servo only twitches slightly repeatedly. I have the correct potentiometer resistance. Here are the sketches and pictures.

To Arduino with servo

#include \<Servo.h\>

int servoPin = 9;  
Servo Trevor;

void setup()  
{  
Serial.begin(9600);  
Trevor.attach(servoPin);  
delay(500);  
}

void loop()  
{  
while(Serial.available() == 0);  
int data = Serial.read() - '0';  
int pos = map(data, 0, 9, 0, 180);  
pos = constrain(pos, 0, 180);  
Trevor.write(pos);  
Serial.flush();  
}

To Arduino with Potentiometer

int potPin = 0;

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

void loop()  
{  
int val = map(analogRead(potPin), 0, 1023, 0, 9);  
Serial.println(val);  
delay(50);  
}

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 16, 2016, 10:09pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/8 "2016-05-16T22:09:49Z")

</div>

Pics

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 16, 2016, 10:13pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/9 "2016-05-16T22:13:16Z")

</div>

```cpp
  Serial.println(val);

```

Do you have a clue what this sends?

Try Serial.print(), instead.

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 16, 2016, 10:15pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/10 "2016-05-16T22:15:47Z")

</div>

Ok thanks and no I just copied the code from the guys video. Also for some reason I can't attach my pictures to the attach file section.

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 16, 2016, 10:21pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/11 "2016-05-16T22:21:03Z")

</div>

Did you happen to mean Serial.println(); instead of Serial.print() because the later gives me an error.

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [May 17, 2016, 12:12am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/12 "2016-05-17T00:12:49Z")

</div>

```cpp
while(Serial.available() == 0);

```

^ delete this from your code. 😉 It's a "blocking" function and is not necessary. Use:

```cpp
while(Serial.available())
{ 
  //all your executable code
}

```

instead.

```cpp
  int val = map(analogRead(potPin), 0, 1023, 0, 9);

```

^ Why are you mapping the pin reading from 0-1023 to 0-9? you should be mapping the pin reading from 0-1023 to **0-180**.

^ Also, you should initialize "val" as a **byte** instead of an int.

```cpp
pos = constrain(pos, 0, 180);

```

^ not needed if you made the circuit correctly and connected the correct pins to the correct wires.

```cpp
Serial.flush();

```

^ Don't use this function, period. 🙂

```cpp
Serial.println(val);

```

^ Unless you are using the serial port to communicate with your PC, **ONLY USE Serial.write()**. Serial.println() does a lot of auto-formatting if your data that you don't want.

Here is the revised code that should allow your XBees to work properly. I don't know where you got the code you posted, but I would **never** go back to him/her for more code. Just use these programs:

TRANSMITTER (TX) Arduino Code:

```cpp
int potPin = 0;

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

void loop()
{
  byte val = map(analogRead(potPin), 0, 1023, 0, 180);
  Serial.write(val);
  delay(50);
}

```

RECEIVER (RX) Arduino Code:

```cpp
#include <Servo.h>

int servoPin = 9;
Servo Trevor;

void setup()
{
  Serial.begin(9600);
  Trevor.attach(servoPin);
}

void loop()
{
  while(Serial.available())
  {
    byte data = Serial.read();
    int position = data & 0xFF;
    Trevor.write(position);
  }
}

```

Try this and see if it works.

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 17, 2016, 1:59am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/13 "2016-05-17T01:59:25Z")

</div>

Wow! that works perfectly thank you very much Power\_Broker. Where did you learn to program this well? just reading books about Arduino or other sources? I would really like to learn how to program Arduino as well as you do. Thank you very much!!

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [May 17, 2016, 2:32am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/14 "2016-05-17T02:32:55Z")

</div>

Thank you so much! 😃

I learned to code through several electrical and computer engineering classes at Virginia Tech. The Arduino and XBee specific material came from reading up online from different people's projects in addition to researching the built in classes/functions that the Arduino IDE provides. Before you call a function (i.e. Serial.println()), you need to look it up on the Arduino reference website and get familiar with "what's under the hood" so to speak. Also, doing general research on the types of variables and binary/hex representations of data will help a lot.

Good luck and I'm glad the code works!

---

<div class="post-metadata">

**Author:** ![Mrbobokins](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Mrbobokins](https://forum.arduino.cc/u/Mrbobokins)\
**Post date:** [May 17, 2016, 2:47am UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/15 "2016-05-17T02:47:03Z")

</div>

Thank you so much. Sounds like you've had a lot of training. I will have to start reading up.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 5:51pm UTC](https://forum.arduino.cc/t/xbee-knowldege-wanted/386103/16 "2021-05-05T17:51:48Z")

</div>


