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:
Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.













December 24, 2012 at 4:35 am
Greetings jeremy! Im having my thesis right now, and i choose temperature control as my topic and peltier or thermoelectric is my choice, so the thing is i want to control it wirelessly. I found that it can be control by arduino but its not wireless. Do you think it is possible to control it via Xbee base on this video? hoping for your thoughts about this. Thanks a lot.
p.s. sorry for my bad english :)
December 24, 2012 at 6:41 am
There does seem to be a lot of confusion in many people’s minds on the topic of “wireless communication” with Arduino.
Essentially, the “wireless” means exactly that – it replaces the wire, the cable that connects the Arduino to the PC…
If you can ‘talk’ to the PC via USB or Serial (to the IDE Serial Monitor, a Processing program or something written in another language) then you can replace the cable with a “wireless link” of some kind.
X-bee seems to have become a ‘standard’ in its own right, and it appears sometimes as if it is the only way to achieve the goal – but there are what appear to be easier and simpler solutions to ‘wireless communication’ that are not as well known, like the modules from RF-Digital and their “Wireless Inventor’s Shield” (see http://www.opensourcerf.com/rfd21815-wireless-inventors-shield-for-arduino.html) or, if you need long range, their “RFDANT” (the modules and the RFDANT are 3.3V – so beware, you’ll need to use ‘level-shifting’ for the signals and the 3.3V line from the Arduino for power).
And don’t overlook Blootooth and WiFi – they are both “Wireless Communication” methods – as is Infra-Red…
Due to problems with Customs where I am, I haven’t been able to try the “Wireless Inventor’s Shield” yet but Jeremy, being on the same continent, may be able to do so much more easily – any thoughts Jeremy?
Duncan (in Spain)
December 26, 2012 at 7:53 pm
Yeah, I have a couple of other wireless technologies that I’m hoping to try out…
December 25, 2012 at 11:46 pm
Jeremy,
are just gr8,
could you please tell me how can i plug in my 9v battery to make it COMPLETELY wireless(i dont want the usb cable)
please tell me as soon as possible
would be better if you can post a short video
thanks in advance!!!
December 26, 2012 at 7:54 pm
Just connect it to the Vin pin and ground of the Arduino.
December 28, 2012 at 8:52 am
Hello Jeremy
I want to ask about making a bidirectional communication between 2 arduinos.using 2 Xbee’s. Here my biggest issue is how to make them talk one at the time , and how to make them know when to listen and when to transmit.
I tried so far with a software serial port named Xbee , by using
if ( Xbee.available == 0 ) {
\\ trasmitting code } and
if ( Xbee.available>0) {
switch ( Xbee.read() \\receiving code }
one both arduinos ( one used for a remote , and one for a robot ) , but trouble is : they don’t seem to synchronize , and one way trasmissions are lost ( those comming from the robot althought his xbee’s is working properly)
Then i thought that an emulated serial port is not as good as the original hardware port and i connected both xbee’s to the arduino’s RX -TX , Is this guess true ?
Now i’m able to send data to the robot and to receive a feedback but that’s all , i can’t make the remote enter transmission mode again unless i reset the arduino
I don’t know what functions or structures a should use to achieve this
Thank you for your time
January 1, 2013 at 1:45 pm
Hello
I have 2 xbees, one sitting on explorer board and the other connected to a dsPIC chip. I initialized the clock and set peripherals and assigned the RX and TX pins correctly but I am not able to send or receive!!! I am going nuts… This same code I used with a different xbee (regular) but the xbee I have now is xbee pro S2. I configured it with XCTU and all but in XCTU terminal I only see the characters sent from the PC xbee… But the xbee on the PIC won’t receive from PC and I am sending from it to PC but XCTU terminal on PC doesn’t show anything received… The RSSI light should blink I believe when they send and receive but I don’t know why this is happening! Is there a special setting for xbee pro that I am missing?
I was thinking maybe the baud rate isn’t the same on both but I checked my calculations many times and they both are set at 9600… Any ideas?
Thank you.
January 2, 2013 at 1:24 pm
Hi Jeremy,
Thanks again for this presentation… I could finally make everything work:)
Do you know how I could make the same with a higher resolution (like 180)… 9 is not sufficient for what I’m hoping to do… It it is much more complicated to read strings of 3-4 characters:(… Any suggestion?
Thanks & Happy new year!
January 4, 2013 at 3:45 pm
Yes, use this to read until a newline (‘\n’): http://arduino.cc/en/Serial/ReadBytesUntil
Then use the atoi() function to convert it to an integer.
January 14, 2013 at 2:26 pm
Hi Jeremy,
Great video! I am wondering if these instructions will work for Mega ADK’s instead of the Unos? Do you know if the xbee radios would need to be the same type? Thanks!
-Micah
January 16, 2013 at 2:20 pm
hi jeremy..very nice tutorial.am working on my project ” rc plane “..hence i need to have a wireless communication to control servo motor and am using xbee pro 900..i have two mega 1280 and 2560 ..i just did lotz of test to see if the communication works well on the xbee and yes it was…on the transmitter @ is fine but on the receiver something happening wrong..something wrong with the code!!
the transmitter is sending the information perfectly…but when i connect the receiver module to one of the arduino and serial print on my pc…this are the values i am getting :
60
0
0
60
0
0
60
0
0
etc..i don’t know were the zero are coming from.the 60 is the value that changes according to the pot trim..while if i connect to the xbee to xbee adapter and serial print am getting the values 0-9 perfect..and the servo freaks.just moving small from 0-50 while freaking.. i did your changes on as said above….thanks let me know!:)
January 16, 2013 at 5:13 pm
did you try changing ” Serial.println(val);” to “Serial.print(val);” on the sketch that is transmitting? .
January 17, 2013 at 5:41 am
hello Michael..it now works..very happy. thanks lot for the reply :)))))))
February 28, 2013 at 7:07 pm
Hi Michael,
Ive been searching for days for a solution to that problem with the servo, tried everything and everywhere, then came across ur message to another user. I changed it to print and it works perfect.
Nice 1.
thanks a million
January 19, 2013 at 7:27 am
Hi, Jeremy. I want to ask that whether a normal RF Transmitter can replace the Xbee ?
I have Receiver Module like this
http://zabdz.en.alibaba.com/product/378562420-209746009/Wireless_Receiving_Module_ZRW8K_.html?domain_name=zabdz.en.alibaba.com
and Transmitter Module like this
http://www.goodluckbuy.com/chj-fst-1000m-bx-r315a-module.html
January 22, 2013 at 2:12 pm
Hello Jeremy,
I’m new to arduino and want to send two variables data from one arduino to another via wireless serial port. what should I write in the code to get this done?
Thanks,
January 25, 2013 at 8:54 pm
Hi Jeremy,
The XBees in the tutorial are Series1 or Series2?If they are Series2, doesn’t one need to be a coordinator?
January 28, 2013 at 7:51 pm
The ones in the video are Series one, and are just acting as a wireless serial pass-thru
February 25, 2013 at 1:31 pm
hey Jeremy
what will be the difference if i use series 2 Xbee rather than series 1, will i be able to set it up and run a DC motor using this same tutorial.
i am asking this because most series 1 Xbees are out of stock
please reply ASAP
thanks a lot!!!
January 26, 2013 at 9:50 pm
Hey Jeremy,
Great blog and great instructions! I was wondering if you could post similar tutorial videos for MSP430 Launchpad or EZ430-RF2500. I saw your weather station video and I know that it’s been a while since you finished that project up but if you could post some beginner tutorials on how to program EZ430-RF2500 and/or MSP430 Launchpad, it will be greatly appreciated! Or, even if you could post on how your programmed your EZ430-RF2500, that would be great as well! Thanks for your aid and keep up the great work!
January 27, 2013 at 1:38 pm
Hi Jeremy, I had puzzled about how to accomplish a task I was designing for and I stumbled onto your arduino series on YouTube and I think you just may have solved my problem. THANKS!
1) I am examining the episode 9 parts list and programs. I am finding the links pointing to the Xbee shield and the Xbee transcievers are ending up at a “product not found” search page. Could you possibly check into that for us newbs?
2) Could I be so intrusive as to send you a brief description of what I am trying to do and get your input as to whether or not I am on the right track and your observations on it? Not sure if that would be acceptable in this venue or not.
Thanks, Michael
January 30, 2013 at 3:09 pm
THANK YOU!!!
January 31, 2013 at 2:53 am
very niec~!!!fantasy~!here i have a nice shield http://www.linkspritedirect.com/product_info.php?ref=1&products_id=171&affiliate_banner_id=1~!!good day~
February 3, 2013 at 8:00 am
hi jeremy,you are awesome.Do you have any idea how to transmit more than one analog input (e.g analog 0-4)with the same 0-9 clicks to control each independent servo on the receiver??it looks a bit tricky ..please try to help me out. thanks :)
February 4, 2013 at 8:32 am
Hi Jeremy, my wife keeps leaving her cellfone behind, I want to setup an pan network between her phone and maybe a neckless or watch something like this with a distance setting option that will activate an alarm, the question is do you know where I can get information on how to create this leash between two devices? I know you can buy this on the market for example Zomm but I would like to make my own one.
Hope you can help me.
Italo New Zealand
February 5, 2013 at 5:44 pm
You can probably do this with a zigbee radio, but it may be larger than you’re hoping.
February 7, 2013 at 3:27 am
Hello Jeremy!
Awesome tutorials!! Your tutorials have helped me and my team in our senior project!
I have a question. How do you connect the XBee (we are using a XBee WiFi Module) to an Arduino Mega 1280? I mean, which pin of the XBee goes where in the Arduino? We will get the XBee shield pretty soon, but we have to start working on it.
Thank you very much!
February 8, 2013 at 2:47 am
Hey! First off, great tutorial! I have two questions:
1) With the xbee shield on, is it possible to attach a motor shield over it? For example, if I had one arduino controlling a robot car and I wanted it to convey the signals of where to turn to a second car following it, is that possible?
February 8, 2013 at 5:12 pm
Connect the RX pin on one to the TX on the other and vice versa. Conenct the Xbee Vcc to 3.3V and the ground to the arduino ground.
February 14, 2013 at 2:28 pm
Hello Jeremy!
Please help me choose! I want to create wireless connection, but i’m can’t choose device to solve that problem? I want a stable signal near metal objects such as steel structures on cement plants?
February 18, 2013 at 12:14 pm
Use wires if you are very concerned about interference.
February 16, 2013 at 1:19 am
Hey Jeremy
You’re tutorials are amazing, they have helped me a lot in my project!!!!
I am intending to do the same thing as you did in the video, but with the addition of a motor shield. I have two questions;
1) will i have to use two motor shields to communicate between the two xbees ? (i guess no!!!!)
2) Is it possible to use the motor shield when the Xbee shield is still stacked on the arduino uno
( This question was asked before, but i did not understand the answer——->
” Connect the RX pin on one to the TX on the other and vice versa. Conenct the Xbee Vcc to 3.3V and the ground to the arduino ground. ” )
thanks
Allen
February 18, 2013 at 12:13 pm
Yes, you can stack the two shields if you have long pass-thru header pins
February 20, 2013 at 7:57 pm
Hi Jeremy, nice tutorial. I have one problem though; Everything works for a bit and my servo moves about 90 degrees (when I turn my POT 180 degrees) and it randomly stops, buzzes, and shakes and won’t respond to my POT anymore. Is it something with the serial.flush mentioned before? I changed it to the updated version mentioned in the edit( while(Serial.available()>0) Serial.read(); ) and other than that, my code is basically identical to yours. Any tips?
Thanks!
March 9, 2013 at 9:33 am
hi
how will the codes change if i want to control two servos using two different pots, i tried doing it but was not able to get it. I am posting both codes below please could anybody please help .
sender
int potPin_0 = 0;
int potPin_1 = 1;
int potPin_2 = 2;
void setup()
{
//Create Serial Object (9600 Baud)
Serial.begin(9600);
}
void loop()
{
int val_0 = map(analogRead(potPin_0), 0, 1023, 0, 9);
Serial.print(val_0);
delay(50);
int val_1 = map(analogRead(potPin_1), 0, 1023, 0, 9);
Serial.print(val_1);
delay(50);
int val_2 = map(analogRead(potPin_2), 0, 1023, 0, 9);
Serial.print(val_2);
delay(50);
}
receiver end
#include
//Define Pins
int servoPin_0 = 8;
int servoPin_1 = 9;
int servoPin_2 = 10;
//Create Servo Object
Servo orangeServo;
Servo wristServo;
Servo gripperServo;
void setup()
{
//Start Serial
Serial.begin(9600);
//Attaches the Servo to our object
orangeServo.attach(servoPin_0);
wristServo.attach(servoPin_1);
gripperServo.attach(servoPin_2);
delay(500);
}
void loop()
{
while( Serial.available() == 0);
int data_0 = Serial.read() -’0′;
int pos_0 = map(data_0, 0, 9, 0, 180);
pos_0 = constrain(pos_0, 0, 180);
//Turn the servo
Serial.print(pos_0);
orangeServo.write(pos_0);
while(Serial.available()>0);
Serial.read();
while( Serial.available() == 0);
int data_1 = Serial.read() -’0′;
int pos_1 = map(data_1, 0, 9, 0, 180);
pos_1 = constrain(pos_1, 0, 180);
//Turn the servo
Serial.print(pos_1);
wristServo.write(pos_1);
while(Serial.available()>0);
Serial.read();
while( Serial.available() == 0);
int data_2 = Serial.read() -’0′;
int pos_2 = map(data_2, 0, 9, 0, 180);
pos_2 = constrain(pos_2, 0, 180);
//Turn the servo
Serial.print(pos_2);
gripperServo.write(pos_2);
while(Serial.available()>0);
Serial.read();
}
the problem i am facing currently is that when i move one pot all the servos turn .
could somebody please explain why this happens
thanks
March 12, 2013 at 2:00 pm
I want to create code that will send characters from the keypad through the xbee to the other xbee. Then the ardiuno will take in the signal and control a DC motor. Now i am not sure if the way i programmed this will work. I don’t know if the character i send will be received as a character. I have not tested the code yet because I am waiting for my USB explorer to give the xbees the ID’s in order for them to communicate. I can copy my code down if you want to see. Any answers you may have would be greatly appreciated.
March 18, 2013 at 1:07 am
As you eluded to, you should test it out to see how it works. If you send it as a character, it will be received as a character.
March 14, 2013 at 1:31 am
Hi Jeremy. GREAT tutorials by the way, thank you for your effort.
if the arduino uno is communicating with a sensor board by UART, can it also communicate this data to another unit using xbee? Or does the UNO need to only talk to one or the other using UART. I think the mega allows multiple uart ports but not sure if it’s doable with UNO.
March 15, 2013 at 3:22 am
Hi Jeremy,
Your tutorials have been extremely handy. I only started to use the Arduino two months ago, pretty interesting stuff. Currently, I have been able to establish wireless communications to display some of my outputs on the serial monitor. About a week ago, I decided to step it up by using a serial graphic LCD to display my outputs for a Voltage reading circuit using a Potentiometer, two arduinos, two Xbee shields, and a serial LCD, but it doesn’t seem to work.
My code to get the analogue reading on PinAO on the first arduino (transmitter) seems to be OK, because I have used it for other circuits. But the code for the GLCD circuit on the digital pin 4 on the second arduino (receiver) seems to be giving me a glitch.
Can you offer a guide on what I can possibly do?
I am posting the codes for the first arduino and second arduino in the footnote below:
I will also forward a table of the values I’m looking forward to reading on the LCD to you. Many thanks and accept the assurances of my best regards.
First Arduino (Potentiometer Circuit) CODE:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int incomingByte;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
if (incomingByte == ’1′){
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print(outputValue, DEC);
}
}
}
Second Arduino (GLCD Circuit):
#include
#include
// this array defines values for after the decimal place
// note that lut[1] (625) is in fact .0625
const unsigned int lut[16] = {
0, 625, 125, 1875, 25, 3125, 375, 4375, 5, 5625, 625, 6875, 75, 8125, 875, 9375};
int outputValue = 0; // value output to the PWM (analog out)
int beforeDP = 0; // value befroe the decimal point
unsigned int afterDP = 0;
unsigned int valueaDP = 0;
int temp = 0;
serGLCD myGLCD(4); // GLCD connected to pin 4
void setup() {
myGLCD.setbrightness(50); /// set backlight duty cycle to 50%
// initialize serial communication:
Serial.begin(9600);
delay(5000); /// delay to allow Zbee to setup
Serial.print(’1′);
}
void loop() {
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
outputValue = Serial.read();
// get before decimal place value
if (outputValue < 50) {
beforeDP = 0;
calculate_afterDP();
}
// I'm stuck on getabout the rest of the numbers before the decimal place.
//………..
// print this to LCD
myGLCD.setCursor(0,63); // set cursor to right position
myGLCD.print("ADC value: ");
myGLCD.print(outputValue, DEC);
// remove any digits on screen that are no longer necessary by adding 2 spaces
// for numbers less than 10, and 1 space for numbers less than 100.
if (outputValue < 10) myGLCD.print(" ");
else if (outputValue < 100) myGLCD.print(" ");
myGLCD.setCursor(0,53); // set cursor to right position
myGLCD.print("Voltage: ");
myGLCD.print(beforeDP, DEC);
myGLCD.print(".");
// get afterDP value
valueaDP = lut[afterDP];
// now print it to the screen
//………..
// send message to get another reading
delay(500);
Serial.print('1');
}
}
void calculate_afterDP() {
// I couldn't figure this out work this out – can be calculated from beforeDP value
// and received outputValue.
// ………
}
Pingback: Xbee télécommande (partie 1) | Atelier JCMAtelier JCM
March 19, 2013 at 7:58 am
Hi Jeremy, i’m using usb-serial converter for configuring xbee radio. I have two xbee series 2 radios. I opened putty and entered +++, it returned OK. But When i entered ATMY 1000 returned ERROR000 something like that. But all the other future commands works fine such as ATDL 1001, WR etc.. What’s the problem?
March 26, 2013 at 12:52 pm
Hi Jeremy,
Great tutorials you have on both the Arduino and Eagle, both are a huge help!
I’ve got one large question when it comes to sending data that is over an integer of 9. Since the Serial.read() function reads one Char at a time, how would you suggest sending say, 8 bytes of data (ie 8 int’s with a range of 255). I assume I’d want to make the packet size smaller and simply send one byte per xbee packet, splitting the data from the stop bit on the receiving end.
I’ve been researching this for days and am running against a wall, any suggestions/tips would be very welcome.
Thanks.
Matt
March 27, 2013 at 8:46 pm
Hi Jeremy,
I really like your tutorials, they are very useful and you’re AWESOME!
So far I’m doing some experiment, I would like to use piano keyboard to control the LED and maybe using bluetooth to do the remote control.
I would like to ask how can make it possible, I need to MIDI right? and what materials would you reccomend? Could you give me some suggestions?
Thank you so much!
Chin
April 8, 2013 at 3:11 am
hye..I have two Xbees and two Arduinos coded to control angle of servo motor. How could I code both of them . I think we need a sender and receiver code. On the receiver ,I’m already have the receiver code, but I have no idea for sender code. I’m a total beginner. Thanks!
Here is my receiver code.
#include
Servo myservo;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
}
void loop()
{
while (Serial.available() > 0) {
pos = Serial.parseInt();
}
myservo.write(pos);
delay (100);
}
April 8, 2013 at 11:48 pm
Hi Jeremy. I’m trying to pass a serial input from the serial monitor from one arduino to another arduino but it doesn’t seem to work. I have pasted my Tx Rx codes below. Also, is there a way to log the incoming data into any database?
Tx:
void setup()
{
Serial.begin(9600); //Setting baud rate
}
void loop()
{
while (Serial.available() == 0); //Checks to see if arduino is receiving something from computer
int val = Serial.read() – ’0′; //read i/p from computer in integer terms taking care of ASCII
Serial.print(val);
delay(50);
}
Rx:
int ledPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() – ’0′;
if (val == 1)
{
digitalWrite(ledPin,HIGH);
}
else if (val == 0)
{
digitalWrite(ledPin,LOW);
}
else
{
digitalWrite(ledPin,LOW);
}
Serial.flush();
}
April 10, 2013 at 9:29 pm
How would I modify this code to enable wireless communication between arduinos like you did in tutorial 09. This code is used to have a wii nunchuk accelerometer control two servos.
#include
// comment out the below for Arduino 0011 and above, uncomment for older
#include
#include
uint8_t outbuf[6]; // array to store arduino output
int cnt = 0;
int ledPin = 13;
int servoPin = 11; // Control pin for servo motor
int pulseWidth = 0; // Amount to pulse the servo
long lastPulse = 0; // the time in millisecs of the last pulse
int refreshTime = 20; // the time in millisecs needed in between pulses
int minPulse = 700; // minimum pulse width
#define pwbuffsize 4
int pwbuff[pwbuffsize]; // buffer for smoothing pulseWidths
int pwbuffpos = 0; // position in pwbuff
void setup(){
Serial.begin(19200);
Wire.begin (); // join i2c bus with address 0×52
nunchuck_init (); // send the initilization handshake
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulseWidth = minPulse; // Set the motor position to the minimum
Serial.print (“Finished setup\n”);
}
void nunchuck_init(){
Wire.beginTransmission (0×52); // transmit to device 0×52
Wire.write (0×40); // sends memory address
Wire.write(byte (0×00)); // sends sent a zero.
Wire.endTransmission (); // stop transmitting
}
void send_zero(){
Wire.beginTransmission (0×52); // transmit to device 0×52
Wire.write(byte (0×00)); // sends one byte
Wire.endTransmission (); // stop transmitting
}
int t = 0; // when it gets to 25, read nunchuck
void loop(){
t++;
if( t == 25 ){
t = 0;
Wire.requestFrom (0×52, 6); // request data from nunchuck
while (Wire.available ()){
// receive byte as an integer
outbuf[cnt] = nunchuk_decode_byte (Wire.read ());
digitalWrite (ledPin, HIGH); // sets the LED on
cnt++;
}
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
printNunchuckData(); // uncomment this for debug
//update servo pulseWidth
float tilt = outbuf[4]; // z-axis, in this case ranges from ~75 – ~185
tilt = (tilt – 70) * 1.5; // convert to degrees angle, approximately
pulseWidth = (tilt * 9) + minPulse; // convert angle to microseconds
pwbuff[pwbuffpos] = pulseWidth; // save for averaging
if( ++pwbuffpos == pwbuffsize ) pwbuffpos = 0;
pulseWidth=0; // reset so we can
for( int p=0; p= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}
/*Print the input data we have recieved
accel data is 10 bits long
so we read 8 bits, then we have to add
on the last 2 bits. That is why I
multiply them by 2 * 2
*/
int i=0;
void printNunchuckData(){
int joy_x_axis = outbuf[0];
int joy_y_axis = outbuf[1];
int accel_x_axis = outbuf[2]; // * 2 * 2;
int accel_y_axis = outbuf[3]; // * 2 * 2;
int accel_z_axis = outbuf[4]; // * 2 * 2;
int z_button = 0;
int c_button = 0;
/*byte outbuf[5] contains bits for z and c buttons
it also contains the least significant bits for the accelerometer data
so we have to check each bit of byte outbuf[5]
*/
if ((outbuf[5] >> 0) & 1)
z_button = 1;
if ((outbuf[5] >> 1) & 1)
c_button = 1;
if ((outbuf[5] >> 2) & 1)
accel_x_axis += 2;
if ((outbuf[5] >> 3) & 1)
accel_x_axis += 1;
if ((outbuf[5] >> 4) & 1)
accel_y_axis += 2;
if ((outbuf[5] >> 5) & 1)
accel_y_axis += 1;
if ((outbuf[5] >> 6) & 1)
accel_z_axis += 2;
if ((outbuf[5] >> 7) & 1)
accel_z_axis += 1;
Serial.print (i,DEC);
Serial.print (“\t”);
Serial.print (joy_x_axis, DEC);
Serial.print (“\t”);
Serial.print (joy_y_axis, DEC);
Serial.print (“\t”);
Serial.print (accel_x_axis, DEC);
Serial.print (“\t”);
Serial.print (accel_y_axis, DEC);
Serial.print (“\t”);
Serial.print (accel_z_axis, DEC);
Serial.print (“\t”);
Serial.print (z_button, DEC);
Serial.print (” “);
Serial.print (c_button, DEC);
Serial.print (“\r\n”);
i++;
}
/*Encode data to format that most wiimote drivers except
only needed if you use one of the regular wiimote drivers
*/
char nunchuk_decode_byte (char x){
x = (x ^ 0×17) + 0×17;
return x;
}
April 16, 2013 at 5:40 am
Hi Jeremy:
Thanks.
Followed all the steps. Get an OK when I type in +++, then when I type ATMY1000 I get an error.
What am I doing wrong?
Thanks,
Bela
April 26, 2013 at 11:35 am
What’s the error? You can also try X-CTU if you use windows.
April 19, 2013 at 11:00 pm
Hi Jeremy, i have few question to ask…
After configure the 2 Xbee modules to communicate each other, we need to program the arduino board to transmit and receive data?
Or using Xbee shield will automatic transmit and receive data between Xbee without any programming?
Actually i am a mechanical student and my fyp was using few sensors to capture the RC car behavior….
I using sensor GY-521 MPU6050 and i only know how to capture the raw data but not really know about calibration and conversion to get 0 reading when stationary and get the unit in g (for acceleration) and deg/s (for gyro), pls help me….thanks….
April 25, 2013 at 10:52 am
Jeremy can you teach me how to send and receive a message using 2 computers and 2 transceivers i just need basic codes . Its just like a chat box .
Pingback: Tutorials for Arduino Getting Acquainted with Arduino - Arduino for ProjectsArduino for Projects
April 27, 2013 at 2:11 am
Can u provide the codes for home automation for light controls on aurduino..??
May 2, 2013 at 6:45 pm
Luv all your tutorials. My project is to stick an xbee inside an AC power receptacle, and use that to switch lights on and off. One option is to use an X10 module, but there’s got to be an easier way!
What would you suggest?
Regards.
May 4, 2013 at 6:02 pm
Hi Jeremy,
thanks for your great xbee tutorial, I tried it with 2 xbee serie 1 but without success. Is it important to have the same shields ?
May 5, 2013 at 7:25 pm
Hey Jeremy, I have a project where I need ten smartLED costumes to run light patterns in sync. Can one xbee communicate to the 9 other xbees so the lighting programs run in sync with each other?
May 12, 2013 at 1:08 pm
Hi Jeremy,
awesome tutorials…keep up the good stuff.
I am facing some problems with servo motor control using XBee and arduino.
I have written a Java program that reads the Logitech gamepad input and transmits it via a Xbee USB explorer connected to my laptop. I’m using the gamepad to send simple character commands to control the servo. For example, if I press the right joystick up, i send ‘A’, for right joystick down i send a ‘C’ and vice versa. I hooked up another Xbee connected to an arduino and i’m using the serial monitor to print the commands as they come in to verify that the commands are being transmitted correctly. Please see this code below:
#include
SoftwareSerial xbee(2, 3); //Rx, Tx
void setup() {
Serial.begin(9600);
xbee.begin(115200);
}
void loop() {
if(xbee.available()) {
Serial.print((char) xbee.read());
}
if(Serial.available()) {
xbee.print((char) Serial.read());
}
xbee.flush();
delay(100);
}
After having verified that the commands are being read correctly by the receiver Xbee, I wrote this code to control a servo:
#include
#include
#include
SoftwareSerial xbee(2, 3); //Rx/Tx
Servo servo; //define servo
int servoPos = 90;
char ch;
void setup() {
servo.attach(9); //drive servo using digital pin 11 of arduino
Serial.begin(9600);
xbee.begin(115200);
}
void loop() {
if(xbee.available() > 0) {
ch = xbee.read();
}
switch(ch) {
case ‘A’:
servo.write(0);
break;
case ‘B’:
servo.write(90);
break;
case ‘C’:
servo.write(180);
break;
default:
servo.write(90);
break;
}
delay(100);
}
The problem I’m facing is that the servo seems to turn fine in one direction and starts twitching, and when i don’t send in any command, I default the servo to turn 90 degrees. Here also, the servo is not exactly positioned at 90 degrees.
The servo movement is not smooth and it’s quite erratic, in the sense, the servo seems to turn to some angle randomly.
Do you see any problem with the code?
As a test when i replaced the xbee serial with the commands transmitted via a serial monitor, the servo works just fine as seen below:
#include
#include
Servo servo; //define servo
int servoPos = 90;
char ch;
void setup() {
servo.attach(9); //drive servo using digital pin 11 of arduino
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0) {
ch = Serial.read();
}
switch(ch) {
case ‘A’:
servo.write(0);
break;
case ‘B’:
servo.write(90);
break;
case ‘C’:
servo.write(180);
break;
default:
servo.write(90);
break;
}
delay(100);
}
I’m not sure why it’s not working with the xbee. It would be great if you can help me with this.
Thanks,
Sumair