My Cart

How to Remotely Control a Servo Using XBee Connectivity Kit

Difficulty Level: Expert -

1. Introduction

This tutorial allows the user to move a servo to different degrees depending on the data sent by the XBee gateway.

Ingredients:

    - 1 x XBee Connectivity Kit:
    • 1x Platform
    • 2x XBee
    • 1x XBee Shield
    • 1x XBee gateway
    • 1x Servo
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the XBee Shield, consult the main tutorial.

Step 1: Connection

Connect the XBee shield to Arduino and then connect XBee module or you can connect it directly to Raspberry Pi connection bridge. Connect the servo as you can see in the next diagram.

Connect Black/Brown wire of the servo to GND, Red wire to 3.3 volt and the remaining wire, Yellow/Orange, to digital pin 9.

On the other hand you must connect a gateway with a XBee module to a PC.

Step 2: The Code

Arduino:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos MartĂ­nez
 */
 
#include <Servo.h>

char buffer[50];
int num[3];
int i=0;
int angle;
Servo servo;

void setup() {

  Serial.begin(9600);
  servo.attach(9);

}

void loop() {

  while(Serial.available()>0){
    buffer[i] = Serial.read();
    i++;
  }
  i=0;
  while(buffer[i]!=0){
    num[i] = buffer[i]-48;
    i++;
  }
  if(i==3) {
    angle = num[0]*100+num[1]*10+num[2];
  }
  else if (i==2) {
    angle = num[0]*10+num[1];
  }
  else if (i==1) {
    angle = num[0];
  }

  servo.write(angle);
  delay(10);
  memset(buffer,0,50);
}


        

Raspberry Pi:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos MartĂ­nez
 */
 
//Include arduPi library
#include "arduPi.h"


void servoWrite(int servo, int angle);



char buffer[50];
int num[3];
int i=0;

int angle;   // variable to hold the angle for the servo motor
int servo = 9;
unsigned int us;

void setup() {
	
  pinMode(servo,OUTPUT);
  Serial.begin(9600);

}

void loop() {

  while(Serial.available()>0){
    buffer[i] = Serial.read();
    i++;
  }
  i=0;
  while(buffer[i]!=0){
    num[i] = buffer[i]-48;
    i++;
  }
  if(i==3) angle = num[0]*100+num[1]*10+num[2];
  else if (i==2) angle = num[0]*10+num[1];
  else if (i==1) angle = num[0];
  servoWrite(servo, angle);
  memset(buffer,0,50);

}

void servoWrite(int servo, int angle)
{
  printf("degrees: %i\n",angle);
  for(int i=0; i<20; i++){
    us = (angle*11) + 450;      // Convert angle to microseconds
    digitalWrite(servo, HIGH);
    delayMicroseconds(us);
    digitalWrite(servo, LOW);
    delay(50);                  // Refresh cycle of servo
  }               
}

int main (){
  setup();
  while(1){
    loop();
  }
  return (0);
}


        

Intel Galileo:

Code:
/*  
 *  XBEE Module 
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  a
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */	

#include <Servo.h>

char buffer[50];
int num[3];
int i=0;
int angle;
Servo servo;

void setup() {

  Serial1.begin(9600);
  servo.attach(9);

}

void loop() {

  while(Serial1.available()>0){
    buffer[i] = Serial1.read();
    i++;
  }
  i=0;
  while(buffer[i]!=0){
    num[i] = buffer[i]-48;
    i++;
  }
  if(i==3) {
    angle = num[0]*100+num[1]*10+num[2];
  }
  else if (i==2) {
    angle = num[0]*10+num[1];
  }
  else if (i==1) {
    angle = num[0];
  }

  servo.write(angle);
  delay(10);
  memset(buffer,0,50);
}