This tutorial allows the user to move a servo to different degrees depending on the data sent by the XBee gateway.
Ingredients:
Preparation Time: 30 minutes
Buy nowThis 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.
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.
Arduino:
/* * 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:
/* * 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:
/* * 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); }
If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.
Know more at:
Get the Starter Kits at: