Servo motors are commonly used in robotics for precise control. In this tutorial, we'll show you how to wirelessly use a servo motor with an Arduino UNO and an Android device via Bluetooth. We've already used Arduino to control servos; this time, we'll use Arduino and Bluetooth to control a servo motor.
Required materials
Arduino UNO
HC-05 or HC-06 Bluetooth module
Servo motor
Roboremo app from the Play Store
breadboard
Connecting wire
HC-06 Bluetooth Module
Bluetooth can operate in the following two modes:
Command mode
Operating mode
In command mode, we'll be able to configure Bluetooth properties such as the Bluetooth signal name, passcode, and operating baud rate. Operating mode is where we can send and receive data between the PIC microcontroller and the Bluetooth module. Therefore, in this tutorial, we'll only explore operating mode. Command mode will retain the default settings. The device name will be HC-05 (I'm using HC-06), and the passcode will be 0000 or 1234. Most importantly, the default baud rate for all Bluetooth modules is 9600.
The module is powered by a 5V supply, and the signal pins operate at 3.3V, therefore the module itself has a 3.3V regulator. So, we don't need to worry about that. Of the six pins, only four will be used in operating mode. The pin connection table is shown below.
| S.No | Pins on HC-05/HC-06 | Pin names on the MCU | Pin numbering in PIC |
| 1 | Vcc | Vdd | 31 Holy Needles |
| 2 | Vcc | Ged | 32 German needles |
| 3 | Texas | RC6/Tx/CK | 25,000 needles |
| 4 | take over | RC7/Rx/DT | 26,000 needles |
| 5 | state | CNC | CNC |
| 6 | English (Enable) | CNC | CNC |
Circuit diagram
The circuit diagram for this Arduino Bluetooth servo motor control project is as follows:
Configure the Roboremo application to control the server:
Step 1: Download the Roboremo app from the Android Play Store and install it on your smartphone. After installation, you will see the app window as shown in Figure 1. By clicking the "Menu" button, you will see the window shown in Figure 2 below:
Step 2: - Then click the Connect button, and you will see the window shown in Figure 3 below. You must then select "Bluetooth RFCOMM" and you will be able to connect the HC-06 Bluetooth module to the Android application "Roboremo".
Step 3: - After connecting to the HC-06 Bluetooth module, return to the window in Figure 2, and then click "Edit UI" to create a user interface as needed.
When you click "Edit UI", you will see the window shown in Figure 1 again. Then click anywhere on the screen and you will see the application window shown in Figure 4. Then select "Button" to get the button structure.
Step 4: - After selecting the button, you will see a button structure on the screen for editing. You can adjust the size of the structure and move it anywhere on the screen. Now, to set the value sent when clicked via Bluetooth, you have "Set Press Action" (as shown in Figure 6) and type the value you want to send from that specific button. For example, we are sending "1" by pressing the "Start" button in the Roboremo Android app to rotate the servo.
You can check all the values in the table provided below and click the different buttons to send.
Step 5: - Finally, we have a user interface to control the servo motor using a smartphone.
Code and instructions
Finally, the complete code for controlling a servo motor via Bluetooth using an Arduino is provided.
Arduino has a servo motor library that handles all the PWM-related stuff to rotate the servo. You just need to input the angle you want to rotate, and there's a function `servo1.write(angle);` which will rotate the servo to the desired angle.
Therefore, we first define the servo motor library here, and the software serial library is used to define the Rx and Tx pins.
#include
#include
In the code below, we are initializing the Arduino pins Rx and Tx, defining variables for servos and other things.
Servo myServo; int TxD = 11; int RxD = 10; int servoposiTIon; int servopos; int new1; SoftwareSerial bluetooth(TxD, RxD);
Now, set all variables and components to the initial stage. Here, we connect the servo to the 9k Arduino pins and set the servo's initial position to 0 degrees. The baud rate for serial and Bluetooth communication is also set to 9600.
void setup() {
int pos = 0;
myServo.attach(9);
myServo.write(0);
Serial.begin(9600); // start serial communication at 9600bps
bluetooth.begin(9600);
}
In the no-loop function, the Arduino will continuously check the input value and rotate the servo according to the value received from the smartphone. All values will be received using serial communication.
If the value is 0, the servo will rotate to 0 degrees. Similarly, if we send 45, 90, 135, and 180 degrees from the Bluetooth application, the servo will rotate to 45, 90, 135, and 180 degrees respectively.
void loop() {
if (bluetooth.available()){
String value = bluetooth.readString();
servoposiTIon = value.toInt();
if (value.toInt() == 0){
Serial.println(servoposiTIon);
myServo.write(0);
}
if (value.toInt() == 45){
Serial.println(servoposition);
myServo.write(45);
}
if (value.toInt() == 90){
Serial.println(servoposition);
myServo.write(90);
}
if (value.toInt() == 135){
Serial.println(servoposition);
myServo.write(135);
}
if (value.toInt() == 180){
Serial.println(servoposition);
myServo.write(180);
}
If we send the value "1" by pressing the start button, the servo will rotate continuously until the stop button is pressed. Here, we send "2" when the stop button is pressed, the Arduino will read the button, it will break the while loop and stop the servo.
while(value.toInt()==1){
if (bluetooth.available())
{
value = bluetooth.readString();
Serial.println(value);
if (value.toInt()==2)
{Serial.println("YYY"); break; }
}
servopos++;
delay(30);
Serial.println(servopos);
myServo.write(servopos);
if (servopos == 180 )
{servopos=0;break;}
}
}
}
Servo motor control using Bluetooth:
In this project, we use the Android application "Roboremo" to control a servo motor. Within the application's interface, we created five buttons to control the servo motor, as described earlier. The table below shows the function of each button:
| S.No. | Button Name | Send value | describe |
| 1. | start | 1 | This button is used to start rotating the servo from 0⁰ to 180⁰. |
| 2. | stop | 2 | This button is used to stop the servo at any time. |
| 3. | 0⁰ | 0 | This button is used to rotate the servo to 0⁰. |
| 4. | 90⁰ | 90 | This button is used to rotate the servo to 90°. |
| 5. | 180⁰ | 180 | This button is used to rotate the servo to 180°. |
Therefore, by pressing these buttons on the Roboremo Android app, data is sent via Bluetooth from the smartphone to the HC-06 Bluetooth module. The data from the HC-06 module is received by the Arduino, which rotates the servo at the angle defined in the code of the specific button. We also coded angles of 45° and 135°, but due to the limitations of the Roboremo app, which only allows you to create 5 buttons, we skipped two.
Therefore, this is how you can use Bluetooth to send data from your smartphone to an Arduino to wirelessly control a servo.
#include
#include
Servo myServo;
int TxD = 11;
int RxD = 10;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);
void setup() {
int pos = 0;
myServo.attach(9);
myServo.write(0);
Serial.begin(9600); // start serial communication at 9600bps
bluetooth.begin(9600);
}
void loop() {
if (bluetooth.available())
{
String value = bluetooth.readString();
servoposition = value.toInt();
if (value.toInt() == 0)
{
Serial.println(servoposition);
myServo.write(0);
}
if (value.toInt() == 45)
{
Serial.println(servoposition);
myServo.write(45);
}
if (value.toInt() == 90)
{
Serial.println(servoposition);
myServo.write(90);
}
if (value.toInt() == 135)
{
Serial.println(servoposition);
myServo.write(135);
}
if (value.toInt() == 180)
{
Serial.println(servoposition);
myServo.write(180);
}
while(value.toInt()==1){
if (bluetooth.available())
{
value = bluetooth.readString();
Serial.println(value);
if (value.toInt()==2)
{Serial.println("YYY"); break; }
}
servopos++;
delay(30);
Serial.println(servopos);
myServo.write(servopos);
if (servopos == 180 )
{servopos=0;break;}