Required Components
- 1 x Arduino (e.g., Uno)
- 1 x Servo Motor
- Breadboard and jumper wires
- Serial communication device (e.g., USB-to-TTL converter or second Arduino)
Wiring Instructions
- Connect the signal pin of the servo to pin 9 of the Arduino.
- Connect the power (VCC) pin of the servo to the 5V pin on the Arduino.
- Connect the ground (GND) pin of the servo to the GND pin on the Arduino.
- Connect the serial communication device to the Arduino using the TX and RX pins (pin 0 and 1 on the Arduino for UART).
Code Explanation
This code allows you to control the position of a servo motor via serial commands. The Arduino listens for commands sent over the UART connection, which are simple text commands. For example, sending "90" moves the servo to 90 degrees.
Key Functions
Serial.begin()
: Initializes serial communication at a given baud rate.Serial.available()
: Checks if data has been received over the serial port.Serial.readStringUntil()
: Reads the incoming data until a specified terminator (e.g., newline).myServo.write()
: Sets the angle of the servo (from 0 to 180 degrees).map()
: Converts a value from one range (e.g., input from serial) to another range (servo angle).
Arduino Code
The following code listens for servo angle commands via UART and moves the servo accordingly:
// Include the Servo library
#include
Servo myServo; // Create a servo object
int servoPin = 9; // Pin where the servo is connected
void setup() {
// Attach the servo to pin 9
myServo.attach(servoPin);
// Start serial communication at 9600 baud
Serial.begin(9600);
Serial.println("Servo Control via UART Initialized. Send angle (0-180).");
}
void loop() {
// Check if data is available to read
if (Serial.available()) {
// Read the incoming string until newline ('\n') and convert to integer
String command = Serial.readStringUntil('\n');
int angle = command.toInt(); // Convert string to integer (angle)
// Ensure the angle is within the valid range (0-180)
if (angle >= 0 && angle <= 180) {
// Move the servo to the specified angle
myServo.write(angle);
Serial.print("Moving servo to ");
Serial.print(angle);
Serial.println(" degrees");
} else {
// Invalid angle command
Serial.println("Invalid command! Please enter a value between 0 and 180.");
}
}
}
Upload and Test
- Connect your Arduino to your computer using the USB cable.
- Open the Arduino IDE and copy the code above into the editor.
- Select the correct board and port in the IDE.
- Click the upload button to send the code to the Arduino.
- Open the serial monitor and set the baud rate to 9600.
- Type an angle value (between 0 and 180) into the serial monitor and press Enter. The servo should move to the corresponding position.
Optional: Use Another Device for Serial Communication
If you want to control the servo using another device (like a second Arduino or a Bluetooth module), make sure the TX and RX pins are connected properly. You can modify the baud rate or communication logic as needed.
Conclusion
In this experiment, you learned how to control a servo motor via UART commands using Arduino. Serial communication is a powerful way to send commands to a microcontroller remotely, whether you're using a second Arduino, a computer, or another serial-capable device. You can expand this idea to control multiple servos or even other devices like motors or LEDs.