XBee Shield Hookup Guide
CONTRIBUTORS: JIMB0
Introduction
The XBee Shield gives your Arduino a seamless interface to XBee one of
the most popular wireless platforms around. With XBee, instead of being
tied down by a serial cable – inches away from a paired device – your
Arduino can pass data over the air to another device hundreds of feet
away.
Part of what makes XBee so popular is its simplicity. XBees are controlled
over a serial interface in the most basic operation they can be used as a
wireless serial cable. Setting up XBee networks and addresses is also
simplified with Digi’s free software – XCTU – which we explain in a
separate tutorial.
Covered In This Tutorial
The goal of this tutorial is to set up wireless XBee communication between
a computer and an Arduino/XBee Shield combo. Then, using a terminal
program, we can remotely send data to an Arduino, or read data off of it.
Page 1 of 1
3
We’ll begin by examining the schematics and hardware of the XBee Shield,
then move on to example code. First we’ll set up a test program to make
sure our XBees are communicating with each other. Then we’ll move on to
the remote control Arduino sketch.
Materials Required
1x XBee Shield The star of this tutorial.
You’ll also need headers to install into your shield. We
recommend stackable headers.
1x Arduino The XBee Shield should work with any Arduino-
compatible board – Uno, RedBoard, Mega, you name it.
2x XBees XBees exist in a variety of series, frequencies, and
ranges. If you’re just getting started with XBee, we highly
recommend going with Series 1 models – either with a trace
antenna, wire antenna or u.fl connector.
For more help picking an XBee, check out our XBee Buying
Guide.
1x Explorer The Explorer board allows you to connect an XBee to
your computer. You can use either the Explorer USB, Explorer USB
Dongle, or Explorer Serial.
Depending on which explorer you have, you may also need a
matching mini-B USB or serial cables.
At least one computer with X-CTU installed.
The latest version of X-CTU is available for both Mac and
Windows!
Soldering tools to install headers into your shield. The most basic of
irons and solder should do.
Suggested Reading
Before reading through this tutorial, we highly recommend checking out the
Exploring XBees and XCTU tutorial. That tutorial will introduce you to XCTU
and explain how to configure XBee networks and addresses.
In addition to that tutorial, we also recommend checking these guides out:
Serial Communication Serial communication is critical to controlling
and managing XBees.
Arduino Shields The basics of Arduino Shields, including how to
assemble a shield.
XBee Buying Guide We highly recommend Series 1 XBee’s, if this
is your first time playing with them. If you’re curious about other XBee
classes, check out this guide!
Hardware Overview
Here’s a quick overview of the most components of the XBee Shield:
Page 2 of 1
3
Below we’ll go more in-depth on the most important components of the
shield.
UART/SoftwareSerial Switch
One of the most important components on the XBee Shield is the
DLINE/UART switch. This switch controls which Arduino pins interface with
the XBee.
The Arduino Uno has a single hardware UART, which is usually either used
for programming (via the Arduino’s serial bootloader) or communication
with the serial monitor. That serial port can only be used to communicate
with one device at any time, lest you run into problems of bus contention.
There’s also a chance that, during program upload, spurious – even
harmful – data might be sent to any device attached to the Arduino’s
hardware UART.
So to avoid any problems that might arise from connecting the XBee to the
Arduino’s hardware UART, we usually take advantage of the Software
Serial library, connecting the XBee’s RX and TX pins to a pair of free digital
pins on the Arduino.
To select between software and hardware serial, the XBee Shield includes
a small, surface-mount slide switch. This switch allows you to select
between the hardware serial port (UART position) and a software serial port
connected to pins 2 (Arduino-side RX) and 3 (Arduino-side TX).
The switch configuration from the XBee Shield schematic. “D_TX” connects
to Arduino pin 3, and “D_RX” connects to Arduino pin 2.
For all of our example sketches we’ll assume the switch is in the DLINE
position. At the very least, make sure the switch is in the “DLINE”
position when uploading sketches.
LED Indicators
Page 3 of 1
3
There are 5 LEDs on the XBee Shield. Each of these LEDs connects to a
pin on the XBee, which does most of the LED driving. Here’s a table
explaining the operation of each LED:
LED
Label
LED
Color
XBee Pin
Connection Default Operation Notes
PWR Red 3.3V Indicates power is present.
DIO5 Green Associate/DIO5 Associated indicator -- blinks when the
XBee is associated with another XBee.
DOUT Red DOUT Indicates wireless data is being
received.
DIN Green DIN Indicates wireless data is being
transmitted.
RSSI Green PWM0/RSSI Indicates relative signal strength (RSSI)
of last received transmission.
These LEDs can be very useful for debugging. The DIO5/Associate
indicator should blink when the XBee is paired with a compatible device.
The RSSI LED is actually PWM’d so it will be brighter when the paired
XBee is closer (sending a stronger signal).
Assembly Tips
Before you can use the XBee Shield with your Arduino, you’ll need to solder
in some headers.
Check out the assembly page of our Shield tutorial for all of the tips and
tricks related to header installation.
XBee Socket
There is some white silkscreen on the Shield PCB to help orient your XBee
as you’re plugging it in. Make sure to match up the XBee’s two diagonal
edges with the two diagonal lines on the PCB.
With everything installed, you’re ready for the next step! Time to code…
Example: Communication Test
Lets start simple. In this section we’ll upload a sketch which passes data
between your serial monitor and the XBee. This sketch can be use to
create a “portal of communication” between your Arduino’s serial monitor,
and another XBee (connected to a computer via a USB Explorer).
Page 4 of 1
3
Double-Check Your XBee Network
Before continuing with this example, you’ll need to make sure your XBee’s
are configured correctly – they need to be on the same network and have
compatible destination and MY addresses. By default, XBees will all be
compatibly configured, but we recommend setting up unique network ID’s
and addresses. Check out the Configuring Networks page of our Exploring
XBee’s and XCTU tutorial for help with that.
This example assumes you have XCTU installed and two compatibly-
configured XBees – one connected to your computer via a USB Explorer
(or Dongle, or Serial Explorer) and another plugged into the Shield/Arduino.
The Arduino Sketch
Here’s the sketch we’ll use. It makes use of the SoftwareSerial library,
which is included with all of the recent Arduino releases. Before uploading
this sketch make sure the switch on the Shield is in the “DLINE”
position!
Page 5 of 1
3
/*************************************************************
****
XBee_Serial_Passthrough.ino
SetupasoftwareserialporttopassdatabetweenanXBeeShi
eld
andtheserialmonitor.
HardwareHookup:
TheXBeeShieldmakesalloftheconnectionsyou'llneed
betweenArduinoandXBee.Ifyouhavetheshieldmake
suretheSWITCHISINTHE"DLINE"POSITION.Thatwillconnec
t
theXBee'sDOUTandDINpinstoArduinopins2and3.
**************************************************************
***/
//We'lluseSoftwareSerialtocommunicatewiththeXBee:
#include<SoftwareSerial.h>
//XBee'sDOUT(TX)isconnectedtopin2(Arduino'sSoftware
RX)
//XBee'sDIN(RX)isconnectedtopin3(Arduino'sSoftwareT
X)
SoftwareSerialXBee(2,3);//RX,TX
void setup()
{
//Setupbothportsat9600baud.Thisvalueismostimport
ant
//fortheXBee.Makesurethebaudratematchestheconfig
//settingofyourXBee.
XBee.begin(9600);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{//Ifdatacomesinfromserialmonitor,senditouttoXB
ee
XBee.write(Serial.read());
}
if (XBee.available())
{//IfdatacomesinfromXBee,senditouttoserialmonit
or
Serial.write(XBee.read());
}
}
After you’ve uploaded the code, follow this series of steps to verify that
everything is working:
1. Open the Arduino’s Serial Monitor. Make sure the baud rate is set to
9600.
2. Switch to XCTU and click over to console mode.
3. Type something in the console view, it should show up on the Serial
Monitor.
4. Type something into the Serial Monitor (and press “Send”), it should
show up in the console view.
5. Yay!
Page 6 of 1
3
You can use this setup to create a chat system. If you have another
computer nearby, try to see how far your XBees can be from each other
while still reliably communicating.
If your XBee’s aren’t communicating with each other, try getting them closer
together (if they were far apart to begin with). Otherwise, check out our
troubleshooting section of the Exploring XBee tutorial.
Example: Remote Control Arduino
Setting up a chat system is fun, but where XBees and the XBee Shield
really shine is in passing data to and from an Arduino, so you can remotely
control it or receive data from it. In this example, we’ll create a simple serial
interface, which can be used to set and read analog and digital pins.
Example Sketch
Here’s the sketch. Copy and paste from below, or click here to download it.
Page 7 of 1
3
/*************************************************************
****
XBee_Remote_Control.ino
WriteyourArduino'spins(analogordigital)orreadfromtho
se
pins(analogordigital)usingaremoteXBee.
JimLindblom@SparkFunElectronics
OriginalCreationDate:May7,2014
ThissketchrequiresanXBee,XBeeShieldandanotherXBeetie
dto
yourcomputer(viaaUSBExplorer).YoucanuseXCTU'sconsol
e,or
anotherserialterminalprogram(eventheserialmonitor!),t
osend
commandstotheArduino.
Exampleusage(sendthesecommandsfromyourcomputertermina
l):
w#nnnanalogWRITEpin#tonnn
e.g.w6088writepin6to88
d#vdigitalWRITEpin#tov
e.g.ddhWritepin13High
r#digitalREADdigitalpin#
e.g.r3Digitalreadpin3
a#analogREADanalogpin#
e.g.a0Readanalogpin0
Usehexvaluesforpins1013
Upperorlowercaseworks
Use0,l,orLtowriteLOW
Use1,h,orHtowriteHIGH
HardwareHookup:
TheArduinoshieldmakesalloftheconnectionsyou'llneed
betweenArduinoandXBee.MakesuretheSWITCHISINTHE
"DLINE"POSITION.
Developmentenvironmentspecifics:
IDE:Arduino1.0.5
HardwarePlatform:SparkFunRedBoard
XBeeShield&XBeeSeries11mW(w/whipantenna)
XBeeUSBExplorerconnectedtocomputerwithanother
XBeeSeries11mWconnectedtothat.
Thiscodeisbeerware;ifyouseeme(oranyotherSparkFun
employee)atthelocal,andyou'vefoundourcodehelpful,ple
ase
buyusaround!
Distributedasis;nowarrantyisgiven.
**************************************************************
***/
//SoftwareSerialisusedtocommunicatewiththeXBee
#include<SoftwareSerial.h>
SoftwareSerialXBee(2,3);//ArduinoRX,TX(XBeeDout,Din)
void setup()
{
//InitializeXBeeSoftwareSerialport.Makesurethebaud
//ratematchesyourXBeesetting(9600isdefault).
XBee.begin(9600);
printMenu();//Printahelpfulmenu:
Page 8 of 1
3
}
void loop()
{
//Inloop()wecontinouslychecktoseeifacommandhasbe
en
//received.
if (XBee.available())
{
char c= XBee.read();
switch (c)
{
case 'w'://Ifreceived'w'
case 'W'://or'W'
writeAPin();//Writeanalogpin
break;
case 'd'://Ifreceived'd'
case 'D'://or'D'
writeDPin();//Writedigitalpin
break;
case 'r'://Ifreceived'r'
case 'R'://or'R'
readDPin();//Readdigitalpin
break;
case 'a'://Ifreceived'a'
case 'A'://or'A'
readAPin();//Readanalogpin
break;
}
}
}
//WriteDigitalPin
//Senda'd'or'D'toenter.
//Thensendapin#
//Usenumbersfor09,andhex(a,b,c,ord)for1013
//Thensendavalueforhighorlow
//Useh,H,or1forHIGH.Usel,L,or0forLOW
void writeDPin()
{
while (XBee.available()<2)
;//Waitforpinandvaluetobecomeavailable
char pin= XBee.read();
char hl= ASCIItoHL(XBee.read());
//Printamessagetoletthecontrolknowofourintention
s:
XBee.print("Settingpin");
XBee.print(pin);
XBee.print("to");
XBee.println(hl?"HIGH" :"LOW");
pin= ASCIItoInt(pin);//ConvertASCCItoa013value
pinMode(pin,OUTPUT);//SetpinasanOUTPUT
digitalWrite(pin,hl);//Writepinaccordingly
}
//WriteAnalogPin
//Send'w'or'W'toenter
//Thensendapin#
//Usenumbersfor09,andhex(a,b,c,ord)for1013
//(it'snotsmartenough(butitcouldbe)toerroron
//anonanalogoutputpin)
//Thensenda3digitanalogvalue.
Page 9 of 1
3
//Mustsendall3digits,souseleadingzerosifnecessar
y.
void writeAPin()
{
while (XBee.available()<4)
;//Waitforpinandthreevaluenumberstobereceived
char pin= XBee.read();//Readinthepinnumber
int value= ASCIItoInt(XBee.read())* 100;//Convertnextt
hree
value+= ASCIItoInt(XBee.read())* 10;//charstoa3d
igit
value+= ASCIItoInt(XBee.read());//number.
value= constrain(value,0,255);//Constrainthatnumber.
//Printamessagetoletthecontrolknowofourintention
s:
XBee.print("Settingpin");
XBee.print(pin);
XBee.print("to");
XBee.println(value);
pin= ASCIItoInt(pin);//ConvertASCCItoa013value
pinMode(pin,OUTPUT);//SetpinasanOUTPUT
analogWrite(pin,value);//Writepinaccordingly
}
//ReadDigitalPin
//Send'r'or'R'toenter
//Thensendadigitalpin#toberead
//TheArduinowillprintthedigitalreadingofthepintoXB
ee.
void readDPin()
{
while (XBee.available()<1)
;//Waitforpin#tobeavailable.
char pin= XBee.read();//Readinthepinvalue
//Printbegginingofmessage
XBee.print("Pin");
XBee.print(pin);
pin= ASCIItoInt(pin);//Convertpinto013value
pinMode(pin,INPUT);//Setasinput
//Printtherestofthemessage:
XBee.print("=");
XBee.println(digitalRead(pin));
}
//ReadAnalogPin
//Send'a'or'A'toenter
//Thensendananalogpin#toberead.
//TheArduinowillprinttheanalogreadingofthepintoXBe
e.
void readAPin()
{
while (XBee.available()<1)
;//Waitforpin#tobeavailable
char pin= XBee.read();//readinthepinvalue
//Printbeginningofmessage
XBee.print("PinA");
XBee.print(pin);
pin= ASCIItoInt(pin);//Convertpinto06value
//Printtherestofthemessage:
Page 10 of 1
3
XBee.print("=");
XBee.println(analogRead(pin));
}
//ASCIItoHL
//HelperfunctiontoturnanASCIIvalueintoeitherHIGHor
LOW
int ASCIItoHL(char c)
{
//Ifreceived0,bytevalue0,L,orl:returnLOW
//Ifreceived1,bytevalue1,H,orh:returnHIGH
if ((c== '0')|| (c== 0)|| (c== 'L')|| (c== 'l'))
return LOW;
else if ((c== '1')|| (c== 1)|| (c== 'H')|| (c== 'h'))
return HIGH;
else
return 1;
}
//ASCIItoInt
//HelperfunctiontoturnanASCIIhexvalueintoa015byt
eval
int ASCIItoInt(char c)
{
if ((c>= '0')&& (c<= '9'))
return c 0x30;//Minus0x30
else if ((c>= 'A')&& (c<= 'F'))
return c 0x37;//Minus0x41plus0x0A
else if ((c>= 'a')&& (c<= 'f'))
return c 0x57;//Minus0x61plus0x0A
else
return 1;
}
//printMenu
//Abigol'stringofSerialprintsthatprintausagemenuo
ver
//totheotherXBee.
void printMenu()
{
//Everythingis"F()"'d whichstoresthestringsinflas
h.
//That'llfreeupSRAMformoreimportanatstuff.
XBee.println();
XBee.println(F("ArduinoXBeeRemoteControl!"));
XBee.println(F("============================"));
XBee.println(F("Usage:"));
XBee.println(F("w#nnn analogWRITEpin#tonnn"));
XBee.println(F("e.g.w6088 writepin6to88"));
XBee.println(F("d#v digitalWRITEpin#tov"));
XBee.println(F("e.g.ddh Writepin13High"));
XBee.println(F("r# digitalREADdigitalpin#"));
XBee.println(F("e.g.r3 Digitalreadpin3"));
XBee.println(F("a# analogREADanalogpin#"));
XBee.println(F("e.g.a0 Readanalogpin0"));
XBee.println();
XBee.println(F("Usehexvaluesforpins1013"));
XBee.println(F("Upperorlowercaseworks"));
XBee.println(F("Use0,l,orLtowriteLOW"));
XBee.println(F("Use1,h,orHtowriteHIGH"));
XBee.println(F("============================"));
XBee.println();
}
Page 11 of 1
3
Upload that, then switch over to your XCTU console window. You’ll use the
XBee connected to your computer to control and read data from your
Arduino.
All of the XBee magic occurs in serial prints and reads. To send data from
the Arduino XBee, XBee.print() and XBee.println() ’s are used to write
strings and other data. To read data from the computer XBee, we can use
XBee.read() , adding XBee.available() tests to check if data has come
in. That’s all there is to it!
Check out the comments in the code for a line-by-line dissection.
Remote Controlling/Receiving
When the Arduino sketch first starts up, it will print a helpful usage menu.
After that’s printed, follow the directions to send commands to your Arduino.
To control pins 10, 11, 12, and 13, send the hexadecimal equivalent
characters (A, B, C, and D).
w#nnn analog write pin #to nnn . Use leading zeros for single-
and double-digit values.
Example: w6088 will write pin 6 to 88
d#v digital write pin #to v. vcan be 1, h, or H for HIGH, and
0, l, or L for LOW.
Example: ddh will write pin 13 High
r# -digital read digital pin #
Example: r3 will digtally read from pin 3.
a# analog read analog pin #
Example: a0 will read analog pin 0
In each case, the Arduino will respond with the action it’s taken after you’ve
sent a viable string.
As an initial test, try turning the D13 LED on and off, by sending dd1 and
dd0 .
Then try setting analog values, or reading from pins. Maybe try making it
more advanced – have a button press trigger an XBee communication. Or
send an alert when an analog input rises past a certain threshold.
This example barely scrapes the surface of what the Arduino-XBee
combination is capable of. XBee’s allow you to remotely control your robot
from the comfy confines of your computer. Or you can set up a network of
XBees to monitor carbon-monoxide conditions in every room, while logging
to a single computer.
The power of XBees comes from their simplicity – they make your projects
wireless by simply “serial printing”.
Resources & Going Further
That should be enough to get you started. If you’re looking for more XBee
Page 1
2
of 1
3
and XBee Shield info, check out these resources:
XBee Shield Schematic If you confused about the layout of the
schematic, check out this PDF.
XBee Shield GitHub Repo This is where you’ll find the latest PCB
design files.
XBee Series 1 Manual For more advanced users, if you really want
to take advantage of the XBee’s unique abilities, check out this
guide.
XBee Wireless Class Materials Check out our teaching materials
for the XBee class we lead every once-in-a-while.
Going Further
With XBee and the XBee Shield you have all of the tools necessary to take
your project to the airwaves. What are you going to make? Need some
inspiration? Check out these related tutorials:
Internet Datalogging with XBee WiFi Need to do a little
“cloud”-based data loggging. Check out this tutorial, which shows you
how to stick sensor readings on the Internet using an XBee WiFi,
XBee Shield, and Arduino?
XBee WiFi Hookup Guide Take the next step with XBees, using the
XBee WiFi. These modules allow you to connect to a wireless
network and give your Arduino Internet access!
Simon Splosion Wireless This tutorial demonstrates one of many
techniques to “hack” the Simon Says – use XBee’s to make the
Simon game wireless!
Getting Started With the RedBot The RedBot is our popular,
Arduino-based robot platform. Once you get it rolling, you can take it
a step further by controlling it with an XBee!
Page 1
3
of 1
3
4
/
16
/
201
5
htt
p
s://learn.s
p
arkfun.com/tutorials/xbe
e
-shiel
d
-hooku
-
g
uide?
_g
a=1.197679855.193945
6
...