Page 1
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.
Page 2 of 13 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.
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...
Page 4 of 13 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: XBee Pin...
Page 5
Page 5 of 13 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.
Page 6
Page 6 of 13 /************************************************************* **** XBee_Serial_Passthrough.ino Set up a software serial port to pass data between an XBee Shi eld and the serial monitor. Hardware Hookup: The XBee Shield makes all of the connections you'll need between Arduino and XBee. If you have the shield make sure the SWITCH IS IN THE "DLINE" POSITION. That will connec t the XBee's DOUT and DIN pins to Arduino pins 2 and 3. ************************************************************** ***/ // We'll use SoftwareSerial to communicate with the XBee: #include <SoftwareSerial.h> // XBee's DOUT (TX) is connected to pin 2 (Arduino's Software // XBee's DIN (RX) is connected to pin 3 (Arduino's Software T SoftwareSerial XBee(2, 3); // RX, TX void setup() { // Set up both ports at 9600 baud. This value is most import // 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 XB XBee.write(Serial.read()); } if (XBee.available()) { // If data comes in from XBee, send it out to serial monit Serial.write(XBee.read());...
Page 7
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.
Page 8
Page 8 of 13 /************************************************************* **** XBee_Remote_Control.ino Write your Arduino's pins (analog or digital) or read from tho se pins (analog or digital) using a remote XBee. Jim Lindblom @ SparkFun Electronics Original Creation Date: May 7, 2014 This sketch requires an XBee, XBee Shield and another XBee tie d to your computer (via a USB Explorer). You can use XCTU's consol e, or another serial terminal program (even the serial monitor!), t o send commands to the Arduino. Example usage (send these commands from your computer termina l): w#nnn analog WRITE pin # to nnn e.g. w6088 write pin 6 to 88 d#v digital WRITE pin # to v e.g. ddh Write pin 13 High r# digital READ digital pin # e.g. r3 Digital read pin 3 a# analog READ analog pin # e.g. a0 Read analog pin 0 Use hex values for pins 1013 Upper or lowercase works Use 0, l, or L to write LOW Use 1, h, or H to write HIGH Hardware Hookup: The Arduino shield makes all of the connections you'll need between Arduino and XBee. Make sure the SWITCH IS IN THE "DLINE" POSITION. Development environment specifics: IDE: Arduino 1.0.5 Hardware Platform: SparkFun RedBoard ...
Page 9
Page 9 of 13 } void loop() { // In loop() we continously check to see if a command has be // received. if (XBee.available()) { char c = XBee.read(); switch (c) { case 'w': // If received 'w' case 'W': // or 'W' writeAPin(); // Write analog pin break; case 'd': // If received 'd' case 'D': // or 'D' writeDPin(); // Write digital pin break; case 'r': // If received 'r' case 'R': // or 'R' readDPin(); // Read digital pin break; case 'a': // If received 'a' case 'A': // or 'A' readAPin(); // Read analog pin break;...
Page 10
Page 10 of 13 // Must send all 3 digits, so use leading zeros if necessar void writeAPin() { while (XBee.available() < 4) ; // Wait for pin and three value numbers to be received char pin = XBee.read(); // Read in the pin number int value = ASCIItoInt(XBee.read()) * 100; // Convert next t hree value += ASCIItoInt(XBee.read()) * 10; // chars to a 3d igit value += ASCIItoInt(XBee.read()); // number. value = constrain(value, 0, 255); // Constrain that number. // Print a message to let the control know of our intention XBee.print("Setting pin "); XBee.print(pin); XBee.print(" to "); XBee.println(value); pin = ASCIItoInt(pin); // Convert ASCCI to a 013 value pinMode(pin, OUTPUT); // Set pin as an OUTPUT analogWrite(pin, value); // Write pin accordingly } // Read Digital Pin // Send 'r' or 'R' to enter // Then send a digital pin # to be read // The Arduino will print the digital reading of the pin to XB void readDPin()
Page 11
Page 11 of 13 XBee.print(" = "); XBee.println(analogRead(pin)); } // ASCIItoHL // Helper function to turn an ASCII value into either HIGH or int ASCIItoHL(char c) { // If received 0, byte value 0, L, or l: return LOW // If received 1, byte value 1, H, or h: return HIGH 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; } ...
Page 12
Page 12 of 13 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, ’s are used to write XBee.print()
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: •...
Need help?
Do you have a question about the XBee Shield and is the answer not in the manual?
Questions and answers