Servo motors are largely used in robotics for precise control. Anyone – well, almost anyone – has mobile devices with Bluetooth connection capabilities. In this tutorial, you will find how to wirelessly control a servo motor with an Arduino UNO and an Android device via a Bluetooth connection. In the end, you will be more connected, you will make things easier and control servo motors at the touch of a touchscreen.

In this article, I will introduce you to Bluetooth connections with the HC-06 Bluetooth module, Arduino UNO, and the SG90 servo motor. Also, you need extra resources for this tutorial such as an Android smartphone with Bluetooth capabilities and an application to send commands from smartphone to the Bluetooth module.

In the first part of the tutorial, I will show you how to hook up the HC-06 Bluetooth module to the Arduino. In the second part, you can find the source code to enter in AT command mode of the HC-06 Bluetooth module. Here you’ll set the name of your device, password, and the baud rate of the HC-06 module. Finally, in the last part, you will see how to setup the Android application and how to program Arduino to turn the servo motor at the touch of a button.

Requirements

Connect the right pieces and make them communicate with each other. This is the plan of this project. If you already have all the below parts or a part of them, you are lucky. The whole project will cost you nothing, or a few dollars in case you will buy only the missing parts. Otherwise, you have to spend tens of dollars to buy the servo motor, the Bluetooth module, the development board and few other accessories. To have a clear view of the costs, in the right side of each part and accessory used in this project is a link to an online store. Here are the parts:

1 X Arduino UNO (Amazon) – the Bluetooth module is compatible with almost any Arduino model, but all the code and schematics in this tutorial are for UNO.

1 X HC-06 (Amazon) – this is a slave Bluetooth module very easy to use with Arduino using serial communication.

1 X SG90 Servo Motor (Amazon) – this is probably the most popular servo motor in the DIY community.

7 X male to male jumper wires (Amazon).

1 X breadboard (Amazon).

1 X Android smartphone (Amazon).

Setting Up the Hardware

In this section, I will show you how to wire the Arduino UNO and the HC-06 Bluetooth module.

To use the HC-06 module, simply connect the VCC pin to the 3.3V output on the Arduino, the GND pin to any of Arduino GND pins, then connect the TX pin of the Bluetooth module to pin 10 of Arduino UNO and RX pin of Bluetooth to pin 11 of Arduino.

For servo motor, connect the brown wire to any of Arduino GND pins, the red wire from the SG90 servo to the 5V output of the Arduino, and the orange wire from the servo motor to digital pin 9 of Arduino.

Arduino Sketch and AT Commands

If the Bluetooth module is being used for the first time, you have to interrogate it to change some of the settings. The settings are changed with so-called AT commands.

The HC-06 module allows you to change a limited number of settings. You can change the name of the device, the PIN, and the baud rate.

You have to run the below AT commands in the IDE used with Arduino. These commands show you the version of firmware installed on the HC Bluetooth module, change the PIN, change the name of the module, and set the baud rate at 9600.

/** * @file How To Control a Servo Motor With a Bluetooth Module, Arduino and Android * @author Calin Dragos for intorobotics.com * @version V1.0 * @date 13.12.2016 * @description This is an Arduino sketch to setup the HC-06 Bluetooth module */ #include #define ROBOT_NAME "Intorobotics" #define BLUETOOTH_SPEED 9600 //This is the default baud rate that HC-06 uses SoftwareSerial mySerial(10, 11); // TX | RX // Connect the HC-06 TX to Arduino pin 10 RX. // Connect the HC-06 RX to Arduino pin 11 TX. void setup() { Serial.begin(9600); Serial.println("Starting the configurations!"); mySerial.begin(BLUETOOTH_SPEED); delay(1000); // Should respond with OK Serial.print("AT test command is: "); mySerial.print("AT"); waitResponse(); Serial.println("----------------"); // Should respond with its version Serial.print("AT version is: "); mySerial.print("AT+VERSION"); waitResponse(); Serial.println("----------------"); // Set pin Serial.print("Set pin: "); mySerial.print("AT+PIN1234"); waitResponse(); Serial.println("----------------"); // Set the name to ROBOT_NAME Serial.print("Set the name: "); String rnc = String("AT+NAME") + String(ROBOT_NAME); mySerial.print(rnc); waitResponse(); //Set baudrate to 9600 //AT+BAUD1 OK1200 Sets the baud rate to 1200 //AT+BAUD2 OK2400 Sets the baud rate to 2400 //AT+BAUD3 OK4800 Sets the baud rate to 4800 //AT+BAUD4 OK9600 Sets the baud rate to 9600 //AT+BAUD5 OK19200 Sets the baud rate to 19200 //AT+BAUD6 OK38400 Sets the baud rate to 38400 //AT+BAUD7 OK57600 Sets the baud rate to 57600 //AT+BAUD8 OK115200 Sets the baud rate to 115200 //AT+BAUD9 OK230400 Sets the baud rate to 230400 //AT+BAUDA OK460800 Sets the baud rate to 460800 //AT+BAUDB OK921600 Sets the baud rate to 921600 //AT+BAUDC OK1382400 Sets the baud rate to 1382400 Serial.println("----------------"); // Set baud rate to 9600 Serial.print("Set baud rate: "); mySerial.print("AT+BAUD4"); waitResponse(); Serial.println("The configurations are done!"); } void loop() { } void waitResponse() { delay(2000); while (mySerial.available()) { Serial.write(mySerial.read()); } Serial.write("

"); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 /** * @file How To Control a Servo Motor With a Bluetooth Module, Arduino and Android * @author Calin Dragos for intorobotics.com * @version V1.0 * @date 13.12.2016 * @description This is an Arduino sketch to setup the HC-06 Bluetooth module */ #include #define ROBOT_NAME "Intorobotics" #define BLUETOOTH_SPEED 9600 //This is the default baud rate that HC-06 uses SoftwareSerial mySerial ( 10 , 11 ) ; // TX | RX // Connect the HC-06 TX to Arduino pin 10 RX. // Connect the HC-06 RX to Arduino pin 11 TX. void setup ( ) { Serial . begin ( 9600 ) ; Serial . println ( "Starting the configurations!" ) ; mySerial . begin ( BLUETOOTH_SPEED ) ; delay ( 1000 ) ; // Should respond with OK Serial . print ( "AT test command is: " ) ; mySerial . print ( "AT" ) ; waitResponse ( ) ; Serial . println ( "----------------" ) ; // Should respond with its version Serial . print ( "AT version is: " ) ; mySerial . print ( "AT+VERSION" ) ; waitResponse ( ) ; Serial . println ( "----------------" ) ; // Set pin Serial . print ( "Set pin: " ) ; mySerial . print ( "AT+PIN1234" ) ; waitResponse ( ) ; Serial . println ( "----------------" ) ; // Set the name to ROBOT_NAME Serial . print ( "Set the name: " ) ; String rnc = String ( "AT+NAME" ) + String ( ROBOT_NAME ) ; mySerial . print ( rnc ) ; waitResponse ( ) ; //Set baudrate to 9600 //AT+BAUD1 OK1200 Sets the baud rate to 1200 //AT+BAUD2 OK2400 Sets the baud rate to 2400 //AT+BAUD3 OK4800 Sets the baud rate to 4800 //AT+BAUD4 OK9600 Sets the baud rate to 9600 //AT+BAUD5 OK19200 Sets the baud rate to 19200 //AT+BAUD6 OK38400 Sets the baud rate to 38400 //AT+BAUD7 OK57600 Sets the baud rate to 57600 //AT+BAUD8 OK115200 Sets the baud rate to 115200 //AT+BAUD9 OK230400 Sets the baud rate to 230400 //AT+BAUDA OK460800 Sets the baud rate to 460800 //AT+BAUDB OK921600 Sets the baud rate to 921600 //AT+BAUDC OK1382400 Sets the baud rate to 1382400 Serial . println ( "----------------" ) ; // Set baud rate to 9600 Serial . print ( "Set baud rate: " ) ; mySerial . print ( "AT+BAUD4" ) ; waitResponse ( ) ; Serial . println ( "The configurations are done!" ) ; } void loop ( ) { } void waitResponse ( ) { delay ( 2000 ) ; while ( mySerial . available ( ) ) { Serial . write ( mySerial . read ( ) ) ; } Serial . write ( "

" ) ; }

The Android Application and the Arduino Sketch

We’re very close to wirelessly control the SG90 servo motor with an Android smartphone. This is usually done by using an Android application that enables the Bluetooth features of the device.

From many applications that enable the Bluetooth features on Android devices, I choose the Arduino Bluetooth Controller application because is free and easy to use.

Before running the Android application, you make sure that the HC-06 Bluetooth module is up and running.

After the application is installed on your device, you have to scan for devices, enter the PIN number set with the script above, and connect the Bluetooth module.

For this tutorial, I use the “Controller Mode” for commands.

The interface layout provides 10 buttons specifically designed to send continuously commands while pressed. For now, we only use two of the buttons: one button to send “1”, and another to send “2”. So, use the settings of the application to set the value “1” and value “2” for two of the buttons. These values will be received by the Bluetooth module and used in the Arduino sketch to control the servo motor.

After the Android application setup is finished, we have to turn back to the Arduino and upload the code to control the servo motor. Below is the Arduino sketch to turn the servo motor at a specific position.

/** * @file How To Control a Servo Motor With a Bluetooth Module, Arduino and Android * @author Calin Dragos for intorobotics.com * @version V1.0 * @date 13.12.2016 * @description This is an Arduino sketch to control the servo motor SG90 with the Bluetooth module HC-06 and the Arduino Bluetooth Controller application */ #include <SoftwareSerial.h> #include <Servo.h> SoftwareSerial mySerial(10, 11); // RX | TX Servo servo; int servoPin = 9; int servoAngle = 0; // servo position in degrees char command; void setup() { Serial.begin(9600); mySerial.begin(9600); Serial.println("You're connected via Bluetooth"); servo.attach(servoPin); } void loop() { if (mySerial.available()) { command=(mySerial.read()); if (command=='1') { Serial.println("Servo motor to 10 degrees"); servo.write(10); delay(500); } else if (command=='2') { Serial.println("Servo motor to 120 degrees"); servo.write(120); delay(500); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 /** * @file How To Control a Servo Motor With a Bluetooth Module, Arduino and Android * @author Calin Dragos for intorobotics.com * @version V1.0 * @date 13.12.2016 * @description This is an Arduino sketch to control the servo motor SG90 with the Bluetooth module HC-06 and the Arduino Bluetooth Controller application */ #include <SoftwareSerial.h> #include <Servo.h> SoftwareSerial mySerial ( 10 , 11 ) ; // RX | TX Servo servo ; int servoPin = 9 ; int servoAngle = 0 ; // servo position in degrees char command ; void setup ( ) { Serial . begin ( 9600 ) ; mySerial . begin ( 9600 ) ; Serial . println ( "You're connected via Bluetooth" ) ; servo . attach ( servoPin ) ; } void loop ( ) { if ( mySerial . available ( ) ) { command = ( mySerial . read ( ) ) ; if ( command == '1' ) { Serial . println ( "Servo motor to 10 degrees" ) ; servo . write ( 10 ) ; delay ( 500 ) ; } else if ( command == '2' ) { Serial . println ( "Servo motor to 120 degrees" ) ; servo . write ( 120 ) ; delay ( 500 ) ; } } }

If you follow the above steps, things just work!

Conclusion

The key piece of this tutorial is the Bluetooth module. These modules are found under various names and features. Only a servo motor connected to an Arduino board is not interested enough to keep you interested for a long time. But, thinking forward, you can do a lot of brilliant things only starting from this tutorial. You can control a robot arm with a twist of the smartphone, control a camera attached to a pan and tilt system, and more.