Learn how to control servo motors using an Android application in a mobile device, an Arduino UNO, and HC-05 Bluetooth module.
A servo motor is a closed-loop system that uses position feedback to control its motion and final position.
RC servo motor works on the same principal. It contains a small DC motor connected to the output shaft through the gears.
The output shaft drives a servo arm and is also connected to a potentiomete (pot).
The potentiometer provides position feedback to the servo control unit where the current position of the motor is compared to the target position.
According to the error, the control unit corrects the actual position of the motor so that it matches the target position.
A servo motor is controlled by sending a series of pulses through the signal line. The frequency of the control signal should be 50Hz or a pulse should occur every 20ms. The width of pulse determines angular position of the servo and these type of servos can usually rotate 180 degrees
The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90-degree position (often called as the neutral position). If the pulse is shorter than 1.5 milliseconds, then the motor will turn the shaft closer to 0 degrees. If the pulse is longer than 1.5 milliseconds, the shaft turns closer to 180 degrees.
Circuit Diagram:
First, make the connections for the servo motors with the Arduino.
Connect the black wire of both the servo motors with the GND of Arduino Connect the orange wire of both the motors to the 5V of Arduino Connect the Orange wire of the first motor to pin 9 of Arduino
Subscribe and Download code.
First, Test Servo motor sweep function without Bluetooth suing following arduino Code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
The output result:
After that, make the connections for the Bluetooth module with the Arduino.
Connect the VCC of the Bluetooth module to 5V of Arduino Connect the GND of Bluetooth module to the GND of Arduino Connect the TX of Bluetooth module to pin Rx of Arduino Connect the RX of Bluetooth module to pin Tx of Arduino
Subscribe and Download code.
Arduino Code for Bluetooth Control:
#include <Servo.h>
Servo myservo;
const int Pin = 9; // myservo pin3 PWM
char Text;
String Spilt;
String angle;
int pos = 0; // variable to store the servo position
int k1;
void setup() {
Serial.begin(9600);
pinMode (Pin, OUTPUT);
myservo.attach(Pin);
}
void loop() {
if(Serial.available())
{
Text = Serial.read();
Spilt = Spilt + Text;
if (Text == '*') {
Serial.println(Spilt);
Spilt = Spilt.substring(0, Spilt.length() - 1); // Delete last char *
k1 = Spilt.indexOf('*');
angle = Spilt.substring(0, k1);
myservo.write(angle.toInt());
delay(15);
Spilt = "";
}
}
}
Once the program is uploaded, reconnect the TX and RX, and make sure the phone is paired with the appropriate Bluetooth module. The steps to connect to an Android phone follow:
Power up the Bluetooth module and go to settings on the Android device.
Pair the device. On some phones, this step must be performed twice for the phone to pair. Make sure to have the PIN codes handy (the default for most Bluetooth Mates is “1234”) > turn ON the power for both devices > search for the module in the Android “Settings” app under Bluetooth Icon.
Once the device is paired to the phone, open the BT Servo app.
Use the List to connect to and find the correct device name contains text HC-05.
Download app:
Servo 180:
servo 360:
Download .aia file
Subscribe and Download code.
Commentaires