With the increasing demand for remote control systems in various fields such as robotics, automation, and IoT, this project focuses on developing a simple and effective method to control servo motor wirelessly. Servo motors are widely used in robotics for precise angular movement. By leveraging Bluetooth communication and a mobile app, users can conveniently control the position of servo motor from their smartphones.
Components Used
- Arduino Uno: The main microcontroller to control the servo motor.
- HC-05 Bluetooth Module: Facilitates wireless communication between the Arduino and the mobile device.
- Servo Motor: An actuator allowing for precise angular positioning.
- MIT App Inventor: A platform to create a mobile application for controlling the servo motors.
Hardware Setup
-
Wiring the Components:
-
HC-05 Module:
- VCC to Arduino 5V
- GND to Arduino GND
- TX (HC-05) to RX (Arduino pin 0)
- RX (HC-05) to TX (Arduino pin 1) with a voltage divider.
-
Servo Motors:
- Control pins connected to Arduino PWM-capable pins (e.g., Pins 9).
- VCC and GND connected appropriately to power the servos.
-
HC-05 Module:
-
Arduino Code:
- The Arduino code initializes the servo motor and listens for Bluetooth commands to control their angles based on received input.
Mobile Application Development
-
Designing the User Interface:
- Created three sliders, each corresponding to a servo motor.
- Added labels to display the current angle values of the servos.
- Integrated a Bluetooth client to manage connections.
-
Programming Logic:
- Implemented event handling to send the angle values of the slider to the Arduino when adjusted.
- Slider sends a formatted command containing the servo number and desired angle.
Code Explanation
#include <Servo.h>
- This line includes the Servo library, a built-in Arduino library designed to simplify controlling servo motors. By including this library, we gain access to a set of commands and functions that allow us to control the position of a servo motor with ease.
Servo servo1; // Create Servo object for the motor
-
Here, we declare an instance of the
Servo
class, calledservo1
. This object allows us to control a single servo motor. Through this instance, we can access functions likeattach()
,write()
, and others provided by the Servo library.
String command; // Variable to store the received command
-
We declare a variable
command
of typeString
. This variable will temporarily store the data received from the Bluetooth module (angle values), allowing us to process and convert it into an integer for servo control.String
is a flexible data type that can handle sequences of characters, making it ideal for receiving commands over Bluetooth.
void setup() {
Serial.begin(9600); // Initialize Bluetooth communication
servo1.attach(9); // Attach Servo to pin 9
}
-
Serial.begin(9600);
: This line initializes serial communication at a baud rate of 9600 bps (bits per second). TheSerial
object represents the Arduino’s communication interface, which allows us to send and receive data. Since the HC-05 Bluetooth module uses serial communication to send data, setting the baud rate to 9600 (the default for HC-05) allows for compatible data exchange. -
servo1.attach(9);
: This line associates the servo motor (via theservo1
object) with Arduino pin 9. Pin 9 is a PWM-capable pin, allowing us to send signals that the servo motor can interpret as positioning instructions. Theattach()
function from the Servo library also initializes the motor for control, preparing it to receive angle values.
void loop() {
if (Serial.available() > 0) { // Check if data is available to read
command = Serial.readStringUntil('\n'); // Read the incoming command until newline
controlServo(command); // Control the servo based on the command
}
}
This is the main loop()
function where the program continually checks for data input from the Bluetooth module.
-
if (Serial.available() > 0)
: This line checks if there’s any data waiting to be read on the Serial port (i.e., from the Bluetooth module).Serial.available()
returns the number of bytes available to read, so when it’s greater than zero, we know data has been received. -
command = Serial.readStringUntil('\n');
: Here, we useSerial.readStringUntil('\n')
to read the incoming data from the Bluetooth module until it encounters a newline character ('\n'
). The Bluetooth module sends the angle data as a string, and the newline character serves as the “end” of this message. The entire command is stored in thecommand
variable. -
controlServo(command);
: This line calls a custom function,controlServo()
, passing thecommand
string as an argument. This function will process the command and set the servo to the specified angle.
void controlServo(String cmd) {
int angle = cmd.toInt(); // Convert command to integer for angle
angle = constrain(angle, 0, 180); // Ensure angle is between 0 and 180
servo1.write(angle); // Move servo to the specified angle
}
This custom controlServo()
function is designed to process the command
string and set the servo motor to the desired angle.
-
int angle = cmd.toInt();: The cmd.toInt() function converts the string (e.g., "90") into an integer (e.g., 90). This integer value now represents the angle for the servo motor.
- angle = constrain(angle, 0, 180);: Servo motors have an angular range from 0 to 180 degrees, so we use constrain() to ensure that the value is limited within this range. If the angle value exceeds 180 or is below 0, constrain() will set it to the nearest boundary (180 or 0, respectively).
- servo1.write(angle);: This command sends the angle value to the servo motor. The write() function from the Servo library takes an integer as input and moves the servo motor to that angle. For instance, if angle is 90, the motor will rotate to a 90-degree position. The motor holds its position until the next command is received.
Explanation of Block Code
The project successfully controlled a single servo motor via Bluetooth using an Arduino Uno and a mobile application developed in MIT App Inventor. Specific outcomes observed included:
Results
The project successfully controlled a single servo motor via Bluetooth using an Arduino Uno and a mobile application developed in MIT App Inventor. Specific outcomes observed included:
- Control Accuracy: The servo motor accurately responded to angle commands sent from the mobile app, demonstrating precise control within the 0 to 180-degree range.
- User Interface Functionality: The slider provided an intuitive interface for easy adjustments of the servo angle, and the real-time display of the servo angle on the app ensured users could confirm the motor's current position.
- Stable Bluetooth Communication: The HC-05 Bluetooth module maintained a reliable connection with the mobile device, allowing for uninterrupted control over the servo motor.