Introduction
This tutorial focuses on servo motor control with an Arduino, a key skill for achieving precise angular positioning in electromechanical projects. Servo motors are specialized actuators that use internal feedback to maintain specific angles, making them ideal for applications like robotic arms or camera gimbals. In this experiment, you’ll learn how to connect a servo motor to an Arduino, program it to move to desired positions using the Servo library, and test its accuracy. This guide is perfect for hobbyists and students exploring motion control.
Servo motors operate using Pulse-Width Modulation (PWM), where the pulse duration dictates the angle. By the end of this tutorial, you’ll have a working setup to control a servo motor’s position with ease and precision.
Components Required
To control a servo motor with Arduino, you’ll need the following components:
- Microcontroller (e.g., Arduino Uno): Sends PWM signals to control the servo’s position.
- Servo Motor (e.g., SG90): A small, affordable servo with a 0-180° range, common for Arduino projects.
- Power Supply (e.g., 5V via Arduino or external source): Powers the servo, ideally separate for larger servos.
- Connecting Wires: Jumper wires for secure connections.
- Breadboard: For prototyping the circuit without soldering.
- Optional: Potentiometer (e.g., 10kΩ): Allows manual angle adjustment during testing.
Schematic
The schematic shows how to wire the servo motor to the Arduino for position control:
Basic connection overview: - Connect the servo’s signal wire (usually orange or yellow) to an Arduino PWM pin (e.g., D9). - Wire the servo’s power wire (red) to the Arduino’s 5V pin or an external 5V supply. - Attach the servo’s ground wire (brown or black) to the Arduino’s GND, ensuring a common ground if using an external supply. - If using a potentiometer, connect its wiper to an analog pin (e.g., A0), with the other pins to 5V and GND.
Note: Small servos like the SG90 can be powered directly from the Arduino’s 5V pin, but larger servos or multiple units require an external power source to avoid overloading the Arduino.
Steps
Follow these steps to build and test your servo motor control setup:
- Assemble the Circuit: Connect the servo’s signal wire to D9, power to 5V, and ground to GND on the Arduino.
- Power Connections: Use the Arduino’s 5V output for a small servo, or connect an external 5V supply for stability with larger models.
- Upload the Code: Load the Arduino sketch (see "Code Example" below) using the Servo library to set positions.
- Test Positioning: Run the code to move the servo to specific angles (e.g., 0°, 90°, 180°) and verify accuracy.
- Manual Control (Optional): Add a potentiometer to A0 and modify the code to adjust the servo angle dynamically.
- Troubleshoot (if needed): Check connections and power if the servo jitters or doesn’t move as expected.
Code Example
Here’s an Arduino sketch using the Servo library to control a servo motor’s position:
#include
Servo myServo; // Create a Servo object
void setup() {
myServo.attach(9); // Attach the servo to pin D9
Serial.begin(9600); // For debugging
}
void setServoAngle(int angle) { // Angle: 0-180 degrees
Serial.print("Setting angle to: ");
Serial.println(angle);
myServo.write(angle); // Move servo to specified angle
delay(500); // Wait for servo to reach position
}
void loop() {
setServoAngle(0); // Move to 0 degrees
delay(1000); // Wait 1 second
setServoAngle(90); // Move to 90 degrees
delay(1000);
setServoAngle(180); // Move to 180 degrees
delay(1000);
}
This code moves the servo to 0°, 90°, and 180° in a loop. For manual control, replace fixed angles with `map(analogRead(A0), 0, 1023, 0, 180)` using a potentiometer.
Applications
Servo motor control with Arduino has practical uses in various real-world scenarios, including:
- Automation Systems: Adjusts flaps or valves in HVAC or irrigation setups.
- Prototyping for Robotics: Positions robotic arms, legs, or sensor mounts.
- Educational Experiments: Teaches PWM, servo mechanics, and microcontroller programming.
- Hobby Projects: Drives animatronics, RC models, or camera gimbals.
- Home Automation: Controls smart mirrors or automated pet feeders.