Arduino Bluetooth Servo

Control Servo Motor via Bluetooth

Arduino Bluetooth Servo

This page explains how to remote control a servo motor using Arduino and Android phone with RoboRemo app.

Materials needed:
- Arduino UNO (or similar) + USB cable for it
- DC adapter for Arduino
- BlueSMiRF module
- servo motor
- wires
- Android phone with RoboRemo app installed

Introduction

A Servo motor is a special type of motor that can be precisely controlled to move to a specific angle. It is widely used in robotics. It has 3 pins: Ground (GND), Power (usually +5V) and signal input.
The signal input type is usually known as Servo PWM signal. PWM = Pulse Width Modulation. The signal consists of periodic pulses, one pulse every 20ms. Each pulse has a variable width of 1ms to 2ms. That width carries the information. A pulse means that pin goes HIGH (+5V) for the duration of that pulse. The remaining time the pin stays LOW (0V).
The Servo motor has a microcontroller inside, that measures the pulse width and moves the motor to the corresponding angle. 1ms -> 0°, 1.5ms -> 90°, 2ms -> 180°. These numerical values together with the 20ms period are standard values. They can slightly differ from one Servo motor to another.

Steps:

1.
Upload the code to your Arduino. Below are 3 varians of the code.
Code variant 1 - control with a slider:

// Control servo motor over Bluetooth using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1

// Servo       Arduino
// GND ------- GND
// VCC ------- VCC
// signal ---- pin4

#define bluetooth Serial
#include <Servo.h>

Servo myServo;

char cmd[100];
int cmdIndex;


void exeCmd() {
  
  // "servo" is the servo motor id
  
  if(cmd[0]=='s' &&
     cmd[1]=='e' &&
     cmd[2]=='r' &&
     cmd[3]=='v' &&
     cmd[4]=='o' &&
     cmd[5]==' ') {
       
       int val = 0;
       for(int i=6; cmd[i]!=0; i++) {
         val = val*10 + (cmd[i]-'0');
       }
       // if cmd is "servo 1234", val will be 1234
          
       myServo.writeMicroseconds(val);      
     }   
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  
  myServo.attach(4, 1000, 2000);
  
  cmdIndex = 0;
}


void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
  }
}


Code variant 2 - control with phone's accelerometer:

// Control servo motor with phone's accelerometer using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1

// Servo       Arduino
// GND ------- GND
// VCC ------- VCC
// signal ---- pin4

#define bluetooth Serial
#include <Servo.h>

Servo myServo;

char cmd[100];
int cmdIndex;

void exeCmd() {
  
  if(cmd[0]=='x' && cmd[1]==' ') {
    
    // value starts at cmd[2]
    // atof converts string to float
    
    float val = atof(cmd+2);
    
    val = val/9.8;
    val = val*500;
    
    int servoMicroseconds = 1500+val;
    
    myServo.writeMicroseconds(servoMicroseconds);      
  }   
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  
  myServo.attach(4, 1000, 2000);
  
  cmdIndex = 0;
}

void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
  }
}


Code variant 3 - control 2 servos with 2 sliders:

// Control 2 servo motors over Bluetooth using RoboRemo app
// www.roboremo.com

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1

// Servos       Arduino
// GND ------- GND
// VCC ------- VCC
// signal1 ---- pin7
// signal2 ---- pin4

#define bluetooth Serial
#include <Servo.h>

Servo myServo1, myServo2;

char cmd[100];
int cmdIndex;

boolean cmdStartsWith(char *st) {
  for(int i=0; ; i++) {
    if(st[i]==0) return true;
    if(cmd[i]==0) return false;
    if(cmd[i]!=st[i]) return false;;
  }
  return false;
}

void exeCmd() {
  
  // "servo1" is the id for servo motor 1
  // "servo2" is the id for servo motor 2
  
  if( cmdStartsWith("servo1 ") ) { // example: if cmd is "servo1 1500"
    int val = atoi(cmd+7);  // val will be 1500
                            // cmd+7, because value comes after "servo1 " which is 7 characters
    myServo1.writeMicroseconds(val);      
  } 
 
  if( cmdStartsWith("servo2 ") ) { 
    int val = atoi(cmd+7); 
    myServo2.writeMicroseconds(val);      
  } 
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  
  myServo1.attach(7, 1000, 2000);
  myServo2.attach(4, 1000, 2000);
  
  cmdIndex = 0;
}

void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
  }
}

2.
Connect the Bluetooth module and the servo motor:

arduino-bluetooth-servo-schematic.jpg


In this example we use Hardware Serial, because The Software Serial library is conflicting with Servo library.
But Arduino UNO also uses Hardware Serial for programming, so when you program the Arduino, you need to disconnect the Bluetooth module, and to run the circuit, you need to disconnect the USB cable and connect the Arduino DC power supply.

3.
Connect your Arduino to the DC power supply.

4.
Configure the RoboRemo interface and enjoy :)