BLOG > Tutorial 9 for Arduino: Wireless Communication

This tutorial was featured on the official Arduino blog on 3/9/2011

This week, we go wireless! We’ll be using a set of arduino Unos, XBee shields, and XBee Transceivers that will create a Personal Area Network (PAN) for wireless serial communication. By the end of the episode, we will be able to turn a potentiometer on one arduino, and have it adjust a motor connected to a another arduino – all wirelessly! I suggest you pick up one of these Sparkfun XBee USB Breakout Boards to get your XBee addresses set up. Enjoy the video :)

EDIT:
Serial.flush() has changed in Arduino 1.0.  You can use the following instead:
while(Serial.available()>0) Serial.read();

You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.

387 comments

  1. Hey Jeremy,

    Is it possible to use 1 xbee as a transmitter to send signals to 2 xbees as receivers?

    I would like to have 2 analog joysticks connected to a single arduino uno and have each joystick control a motor on two seperate arduino unos.

    Thanks
    Zack

      1. hey jeremy im amaze with your tutorials..

        you are awesome

        but i would like to ask can you post some of your codes if any..
        wireless communication using 5812a UHF and arduino atmega 328
        just a simple lighting up an led
        data will send to another arduino from an arduino to light up an led..

  2. Hi Jeremy

    I have a question can I use the Xbee to transmit a video
    and witch cam I need for that ?

    MFG Jasper (14)

    PS: Sorry for my bad english I come from Germany and I like your tutorials :-)

  3. Hey Jeremy,

    I have a question. I have an arduino uno and a Bluetooth adapter RN-42. Using the arduino, I would like the bluetooth adapter to instantiate a connection request to a bluetooth device (cell phone) using its MAC address. Can you please help me on how do I go about doing this? what kind of commands are available? and are there any libraries that I can use?

    Your help will be greatly appreciated. And btw, your tutorials are awesome!!

  4. hey.
    i have 2 arduino uno( one for sending the voice signal and the other have ping) and want to interface it wirelessly. my idea is that i want to control my robot with voice wirelessly and also i want to receive the output from the sensor on the second arduino to the first

  5. For an excellent introduction to creating wireless sensor networks using the Arduino and Xbee radios, I highly, highly recommend “Building Wireless Sensor Networks: With ZigBee, XBee, Arduino, and Processing” by Robert Faludi. I found it to be an excellent resource that starts at square one and takes you all the way to sending sensor data across the internet. I am a HUGE fan of this book.

  6. hey!!
    i am working on cloud robotics, i want to program wirelessly the arduino mega 2560 using zigbee.
    While i am using zigbee adapter and zigbee shield for arduino. I am unable to synchronize both the zigbee pc adapter and arduino zigbee shield, please help me and give me all details how to configure and synchronise.

  7. hey can u help me…i wanna make a robot with L293D can u send me the program for controlling the robot with a joystick n i m using xbees…i wanna put a push button for switchin on/off a headlight…plz plz help me…i m stuck up with my school project…can u send me d program on my email……i looooooove ur tutorials….. :D

  8. Thank you Jermy,
    Can you do a video on wifly controlling 2 DC motors and 1 stepper motor and how to set it up? Thanks a lot.

  9. Hi Jeremy,

    Loving the tutorials. I am just getting into Arduino and have just got to your wireless comms tutorial and have found that the xbee parts are deprecated. Would it be possible to get a tutorial which covers the same stuff but uses the new Arduino Wireless Shield?

    Thanks,
    Alex

  10. Hi Jeremy
    Loving the tutorials, but do you by any change make a Tutorial about RS485 between 2 arduino´s?

    Thanks
    Mads

  11. How would I manage to send more than just 0-9 data? Lets say 0-1023?
    I would do anything to figure this part out. I just don’t understand atoi conversion and I would like a easy way to do it.

  12. The links on the parts list for the Xbee transceiver don’t work and a search at newark or sparkfun turns up no matched for a “Abee transceiver”. :-( I don’t know what to get! New links?

    Thanks much.

    1. Hi Jeremy,
      I would like to know if I could have 2 arduino uno micro controllers set up with xbee on each one; receive analog input data and both transmit and receive commands to each other depending on that data. Thanks for your time and love your videos dude.

  13. first, i love your tutorials. you are a very good presenter, very efficient use of time unlike so many of the utube tutorials. i am not a beginner but wanted a quick tutorial on things i am not familiar with. your tutorials saved me a lot of time learning more than i needed to.

    one little picky correction. it actually applies to the SPI tutorial but it seems to be closed for comment. you refer to the washer on the digital pot circuit. it is actually the wiper. if you were an old guy like me, you would know that the wiper is a brush contact on the resistance element of a conventional type pot.

  14. I tried the XBEE V03 sheild. However the XBEE does not work. When I tried to debug it, i found that, the moment i put the XBEE sheild on the arduino, all the pin of arduino were pulled LOW. Could you please tell how to solve it?

  15. hey,
    I barely started with arduino. Went through your videos on the basics and gave an attempt to read digital data from Isensor from Analgog devices. I am using Arduino to read out data from ADIS16488(3 acc,3 magnetometer,3 gyro) sensor. I have a basic idea about SPI and gave an atempt in reading the product id of the device ,but it didn’t work. COuld you please correct me and also help me out with this.#
    This is my logic fro SPI. I have to 2 16 bit cycles before I can read data. I send the first 16 bit into two halves of 8 bit using SPI.transfer command and receive the output(16 bits) into two halves again when I am using the SPI.transfer again. I have attached the code below for verification. Would be really great if you could correct my mistake or suggest a solution

    #include
    const int CS=53;
    const int prodid= 0x7E00;
    const int yaw=0x4A00;
    void setup()
    {
    Serial.begin(9600); // to print the output on the screen
    SPI.begin();
    SPI.setBitOrder(MSBFIRST); // Setting the parameters according to data sheet
    SPI.setDataMode(SPI_MODE3);
    SPI.setClockDivider(SPI_CLOCK_DIV4); // operating at 4KHZ
    pinMode(CS,OUTPUT);
    digitalWrite(CS,HIGH);
    }

    void loop()
    {

    digitalWrite(CS,LOW);
    int angzh = SPI.transfer(highByte(prodid));// MSB of prod id :: 0x7E00;
    int angzl = SPI.transfer(lowByte(prodid)); // 8 don’t care bits

    unsigned int anglez=angzh+angzl; //

    Serial.println(“\n Yaw angle is :”);

    Serial.println(anglez);

    int prodh = SPI.transfer(highByte(yaw));// MSB of angleout from z axis

    int prodl = SPI.transfer(lowByte(yaw)); // 8 don’t care bits

    unsigned int prodid=prodh+prodl; // this value is supposed to 16488 in decimal .Sum of highbyte
    and low byte.. Not sure how to combine two 8 bits into one
    16 bits.

    Serial.println(“\n Prod id is :”);

    Serial.println(prodid);

    digitalWrite(CS,HIGH);

    delay(1000);

    }

  16. Hi Jeremy,
    I’m handling cell phone controlled robot& need the method involved in modelling an mobile controlled Robot….pls would be great if u help me through..

  17. Hi Jeremy

    I have watched your tutorials to get started with arduino stuff. Much beneficial. I am facing soe problem in communication between Xbees. I want to use 1 arduino board with XBee Shield and XBee on transceiver(just require one way communication) while 1 XBee with sparkfum connected to PC through USB. My receiver is connected to usb port with sparkfun…I will be processing my com port data by initializing com further in VB. But here When I confirm configuration through X-CTU by connecting XBees respectively to sparkfun to check my configuration, it woks fine for both of XBees but when I tried to check through X-CTU by connecting XBee->XBee Shield ->arduino->USB, it not connects. I guess transmission Part is not working. can you please guide me?

  18. Nice tutorial Jeremy. I followed it some months ago when I first started my low-power wireless network project. I was able to make things work almost on the first try! However, I finally switched to panStamps since they are smaller than the arduino/xbee stuff and their API approach really fitted my project.

    Congratulations Jeremy!

  19. Hi Jeremy

    I’m fairly new to Arduino but want to create a network of Arduinos that communicate with one that acts as a controller/recorder (possibly with an SD-Card and probably with an LCD). All the Xbee tutorials that I have found understandable have one Arduino talking to one other and in one direction – I need two-way communication.

    If we call the remote Arduinos – nodes? – A & B (I realise they will be something like 1001 and 1002) and the controller Z (probably 1000), then Z needs to trigger A and if A receives an external input within one second it sends a “1” back to the controller which logs it as “A,1”, if it gets a response within two seconds it sends a “2” and the controller logs “A,2”, if it receives no input within two seconds it sends “0” and the controller logs “A,0” – the process then repeats with B and the results are logged as “B,1”, “B,2”, “B,3” – and so on for any reasonable number of remotes.

    Is there any chance that you could:-

    a) explain how to do this
    b) produce an extended tutorial to show Xbees operating as a real network rather than just one to one, unidirectional.

    From what I’ve seen on the net, many people are confused by Xbees at even this basic level of real-world usage.

    Hoping you can help me and others

  20. Hi Jeremy,

    Excellent tutorial, the best I’ve seen! I do have a question however. I’m unable to get the two xbees communicating with each other. The “sender” is sending code that I can view on the serial monitor as expected, however I’m not seeing the activity light come on indicting that the xbee is sending a signal wirelessly. I’m using breakout boards rather than shields, connected directly to my aduinos. I’m wondering if I don’t have everything wired as needed. Both xbees have ground, +5v, RX and TX connected, and nothing else. Do I need to connect DTR, CTS, RTS or reset to make them work? If so, what do I wire them to on the arduino? (I’m using these Adafruit xbee adapters. http://www.ladyada.net/make/xbee/

    The xbees were disconnected while programming the atmega chips, the atmega chips were disconnected during programming of the xbees, and everything seemed to go smoothly.

    Thanks in advance!
    Jared

    1. You should only need the four pins you described. I actually just recently used the same adafruit XBee breakouts in the way you are describing without an issue. If you unplug the XBees from your computer, then plug them back in, and check ATID, ATDL, and ATMY, did they retain their values? It’s possible your changes were not saved to the non-volatile memory.

  21. Hey Jeremy! i just downloaded these sketches you posted and im having a little trouble. i programmed the xbee radios correctly with the panid and such but the code for the arduino is what is getting to me. i am transmitting the analog input signal of a potentiometer from 0 to 180 (mapped) to the other xbee. When i open the serial monitor on the TX xbee it is correct… 0 to 180… but when i open the serial monitor for the RX xbee it gives me values that start from 10 then jump to 49 then 57.. ?? its craziness and the servo only abides by those numbers… keep in mind i did not subtract the char value 0 because when i do i get negative numbers and that doesnt work either… please help!! i tried your EXACT sketches and they didnt work with it… what am i doing wrong? thanks for the help man!! i need this for my robotic arm even in science olypiad.. im inly 15 :PP

    1. I did science olympiad back in middle school! Hope you’re having fun. What version of the Arduino IDE are you using? This tutorial was done with 0022, I believe, and 1.0 changed a few things that might be giving you issues.

      1. yea im using arduino 1! and i changed the serial.flush command.. is there anything else i have to change? Thanks SOOO much, if this works im going to be one happy dude.

  22. Hey! great videotutorials… i’ve a lot of fun putting things together, I have a question for you…. I have an Xbee Pro 60mW that have a communication radius of almost a mile, and an Xbee 1mW that have a communication radius of almost 330 Ft., can I use the Xbee pro as a transmitter and the Xbee 1mW as a receiver with the same comm radius of the Xbee Pro (around 1 mile)? Both are Series 1

    Thanks for your help :) and Greetings From Colombia!

    (Sorry about my english.. still work in progress)

  23. I want to send data from 1 arduino uno through keypad to another arduino uno to get it displayed on LCD.
    Could it be done using Xbee shield and if,please suggest ways or enclose any links.

  24. Do you know Erlang (language)?
    I’d be happy to get explain how to transmit and receive through the XBEE by Erlang (language).

    regards, adir

    1. Sorry, I don’t know Erlang language………actually I am just a beginner n have started with Arduino …….so if u could help me with Arduino language then it would be of great help……

  25. First of thanks for this great tutorial series. It helped literally a lot to me!! I am currently trying to make sensor network using this little arduinos and wireless communication using xbees. But the thing i want to ask you is how it is possible to arduino to talk to pc and xbee at same time ?? like suppose there will be 5 nodes among this 1 will be connected to pc to get data from all 5. These 5 will communicate using xbees wirelessly now if I want to get some data on PC then can I get it ?? means our serial communication will be used for communicating with xbee then how can I pass data to computer ?

    Thanks again :)

  26. Hi Jeremy

    I wanted to know if you would do a tutorial about the basic of setting up of wireless communication via SPI radio’s like NRF24L01 2,4ghz radios.

    From what needs to be in the program and why to , how to form the array/string to send data over the radio

  27. Hello

    I have a problem with Arduino Uno … !

    when I tried to make a wireless connection between the arduino and my pc to perform this task

    #include

    void setup()

    {

    Serial.begin(9600);

    }

    void loop()

    {

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

    int value=Serial.read()-‘0’;

    if(value==1) Serial.println(“ON”);

    else if(value==0) Serial.println(“off”);

    else Serial.println(“Invalid Input”);

    Serial.flush();

    }

    when I opened the COM (( serial monitor )) and send , I did not get any thing !!!

  28. hello bro, I got final project to do, so I want to ask of how to communicate 2 arduino(Uno R3) wirelessly by using Xbee. For your information, one arduino is for motor output and the another arduino is for sensor.
    I guess ,the hardware I may need is 2 of Xbee and 2 of shield, is it? Lastly , can u tell me about the configuration of programming from A to Z. Sorry , because a lot of thing I don’t know about arduino..I’m just a beginner. I hope u will reply me as soon as possible.TQ

  29. Hello,

    There is any other way to do this? this is really expencive…

    i saw some thing called HT6014 HT6034 etc…i dont know what this is and how to use then but i like the price :) u can make a tutorial like tutorial 9 cheaper way..

  30. Is possible to connect arduino to wireless home router and send data trought ftp service to a webserver?

    Thx

  31. hey jeremy,
    i have a doubt … in your pot program.. u changed the 255 to 9 right …wat will happen if we use 255 itself…. will it be difficult to receive these values in the other Xbee ?
    nice video by the way !

    1. I did that because it is easier to deal with receiving a single character. You can do multiple-character transmission too, it just requires some extra math.

  32. Recently I got two x-bee’s of config: XB24-AWI-001 & shields : http://www.bhashatech.com/shields/73-xbee-shield.html . I programmed it with putty (As shown in your video blogs on wireless serial communications). When i plugged in the shiled with X-bees on them , hey were blinking (does that mean communication was setup??)

    After I programmed the two arduinos for a simple program (As given in Analog examples of arduino IDE) in this way:

    transmitter side program:

    int potPin = A0; // select the input pin for the potentiometer
    int potValue = 0; // variable to store the value coming from the sensor
    void setup()
    {
    Serial.begin(9600);
    }
    void loop()
    {
    // read the value from the potentiometer:
    potValue = analogRead(potPin);
    Serial.println(potValue);
    }

    receiver side program:

    int ledPin = 13;
    int data = 0;
    void setup()
    {
    Serial.begin(9600);
    // declare the ledPin as an OUTPUT:
    pinMode(ledPin, OUTPUT);
    }
    void loop()
    {
    // read the value from the sensor:
    data = Serial.read() ;
    // turn the ledPin on
    digitalWrite(ledPin, HIGH);
    // stop the program for milliseconds:
    delay(data);
    // turn the ledPin off:
    digitalWrite(ledPin, LOW);
    // stop the program for for milliseconds:
    delay(data);
    Serial.flush();
    }

    The pin 13 LED was just glowing , but not blinking as i change the pot values .
    I don’t get it , I checked the jumpers (they are in X-bee mode not in USB ones). Is it a hard-ware problem or there is something wrong in the code???

  33. A few questions for you:
    First,after you program the xbees with putty, does it matter which one goes on the “pot” shield and “motor” shield?
    Second, On my Arduino with the pot, the TX led is lighting up, so shouldn’t the RX led on the Arduino with the motor light up indicating communication?
    Third, If i use the serial monitor in for the motor Arduino, should there be values being read?

    My servo is just chattering a bit and will not move. I serial monitor the pot and I am getting the values 0-9 when i rotate the pot.

  34. Hi Jeremy,

    i really need some help with this Xbee arduino shield….
    i have this almost exactly the same model of shield like yours in the video, i can’t seem to comm. it with X-CTU software which to program it. now i’m so clueless on where, or how to start with this Xbee. can you please give opinions on how to solve this? there is no led flashing on the shield at all when i plug it on to my laptop. im using the same mathode u mention earlier in the vid. which is program the Xbee with arduino. but keep on failling. help!

  35. Hallo jeremy i have a question when i write in Putty ATMY1000 i became an error can you help me, i found nothing about this.

    Thank you

  36. OK for anyone tripping over the “serial.flush” thing, go back to the zigbee sketch that is transmitting and change Serial.println(val); to Serial.print(val);. it was the “carriage return” that was screwing things up for me. please let me know if this helps anyone.

  37. Hey Jeremy thanks a lot for the tutorial videos posted. I had a question for you. I am using i2c communication between my arduino uno, basically i have 5 slave arduinos and 1 master. Can i use this to communicate wirelessly between them while still using i2c.

  38. Hi Jeremy,
    First I need to say, your tutorials are great, and since I am new to the arduino world they are helping immensely. I am stumped and almost embarrassed to ask this question since it’s probably something simple that I am overlooking.

    My Project.
    I am building a robot that is fairly simple and I have been able to build sketches for most of it.
    1. It only has to move forward and reverse. I have set up a potentiometer so that center is null, CW rotation will move the robot forward accelerating and CCW rotation will move the robot in reverse accelerating, using a superdroid robots PWM motor controller.
    2. I need a switch to turn a 12VDC light on and off. Pretty straight forward with a relay.
    3. I need to move a 12VDC actuator up and down. Again pretty easy Working with an H-bridge motor driver.
    4. I need to hit a switch and have the robot stop and remove all power to the light. An emergency shut off, if you will.
    That is pretty much it, at this time…these projects have a habit of growing out of control, as you probably know, so I’m sure I’ll be adding more as I learn.

    The Problem
    I need to control the robot wirelessly. I have two xbee pro 2.4Ghz radio’s and have been able to make them communicate and flash the LED. I watched your Tutorial #9 and that is good for turning a servo. I didn’t have any luck modifying the code from #9 to operate my motor controller so I decided to start from square one. I loaded the sketch for switch 3 from tutorial #4 and now I can’t figure out how to send that command to the remote Arduino to light that LED. So as you can see I am still trying to bust out of “Square One”
    A little help with the serial communication might get me inching forward.
    Thanks
    Oh yes….I wanted to mention something about the Eagle-Tutorial-Series you did…As I was watching #1, I was like, “whoaaa, what board was that he said he designed with this program”? I had to go back and pause…”What the heck is a Makerbot”?….Needless to say, I now own one now!! Good Job.
    Chris

  39. hey jeremy,
    just wanted some info on how to interface the arduino with the GSM module… i’ll b pleased if u can leave a source code fr it..
    thnkx..
    u r awsm..!!

  40. hi.. I really like your tutorial… thanks a lot..
    I have a question, I’m using Arduino Uno. I need to determine the transmission time between two x-bees. how can I calculate that?

  41. Hi Jeremy,

    love your tutorials, they’re very helpful and easy to follow.

    Ive started a project lately, its a fairly popular one, the “solar tracker”. I was just wondering would it be possible to have it communicating with a second arduino wirelessly even though it is running the servos and taking analogue input from the LDRs. would it be possible to have an lcd on the other arduino displaying something from the servo setup.

    I would really appreciate your advice or any suggestions that you might have on this project.

    Thanks

    Chris

    1. Yes, that’s possible. You probably are not using all the processing power that is available to you. You might consider using timer interrupts to accomplish this.

  42. I’m having a problem with this hookup, but could you use a arduino leonardo in place of one of the uno’s?
    thanks in advance,
    Lukas

  43. hey Jeremy
    could u please help with this code.
    i have used this code at the receiving side to blink an led but i’m getting junk values in the serial monitor.

    int p=2;
    void setup()
    {
    //Start Serial
    Serial.begin(9600);
    pinMode(p,OUTPUT);
    }

    void loop()
    {

    while( Serial.available() == 0);
    int data = Serial.read()-‘0’;

    if(data>5)
    {
    digitalWrite(p,HIGH);
    }
    else
    {
    digitalWrite(p,LOW);
    }

    Serial.println(data);

    }

    when data>5 the led is blinking but there is error values in the serial monitor like this ;
    6
    -35
    -38
    6
    -35
    -38
    6
    -35
    -38
    6
    -35
    -38
    6

  44. Hello Jeremy,

    Thanks for the great tutorials…I am a beginner and I am learning so much! My question is would it be possible to configure a setup where instead of using a potentiometer, you used a toggle switch with each position of the switch representing a specific degree for the servo (i.e. 0 degrees and 90 degrees)? If so, would it also be possible to hook up more servo/arduino/XBee combinations and control each of the servos to do exactly the same thing (at the same time) with that one toggle switch? Thanks again for all your hard work.

  45. Hello Jeremy, great tutorials. I am just starting out with arduinos and was wondering what the difference is between the Arduino XBEE Shields that you list on your parts list and the Arduino wireless SD shield. I wanted to start your tutorial but I am not sure about the XBEE or the wireless SD shield. the Arduino.cc site said that the XBEE shields were deprecated and they are pointing me to the new wireless SD shields for the xbee modules. Thanks for any information that you can provide on this. I just want to make sure before I buy them. thanks again.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement