Using Arduino to Control the Position, Speed, and Direction of a Micro Servo Motor
In the world of robotics, automation, and interactive projects, few components are as ubiquitous and versatile as the micro servo motor. These tiny, powerful devices are the hidden muscles behind countless creations—from robotic arms and camera gimbals to animatronic props and automated pet feeders. For makers, students, and engineers, the ability to precisely command a servo's position, dictate its speed, and control its direction is a fundamental skill. And there's no better, more accessible platform to learn this on than Arduino. This guide will take you from unboxing your first micro servo to mastering smooth, controlled motion, all through the magic of code and a simple microcontroller.
What Makes a Micro Servo Motor Special?
Before we dive into the wiring and code, it's crucial to understand what sets a micro servo apart from its larger siblings and other motor types.
Size, Weight, and Power
True to its name, a micro servo is compact, often weighing between 5 to 25 grams. This makes it ideal for applications where space and weight are at a premium, such as drones, small robots, or wearable tech. Despite their size, they pack a surprising amount of torque for their scale, typically measured in kg-cm (kilogram-centimeter).
Internal Mechanics and Control Logic
Unlike a standard DC motor that spins continuously, a servo motor is designed for precise angular control. Inside its plastic or metal casing, you'll find: * A small DC motor * A gear train to reduce speed and increase torque * A potentiometer (variable resistor) attached to the output shaft * A control circuit board
This potentiometer is the key. It provides position feedback to the control circuit, allowing the servo to know exactly where its output shaft is. This closed-loop system is what enables such accurate positioning.
The Pulse Width Modulation (PWM) Language
Micro servos don't understand simple "on/off" or voltage levels. They communicate via Pulse Width Modulation (PWM). The Arduino sends a repeating pulse of electricity. The duration of that pulse, or its width, tells the servo what angle to move to. * A 1.5 millisecond (ms) pulse typically commands the servo to its neutral position (often 90 degrees). * A 1.0 ms pulse typically commands it to the minimum angle (often 0 degrees). * A 2.0 ms pulse typically commands it to the maximum angle (often 180 degrees).
This signal repeats every 20ms (a frequency of 50Hz). It's this precise, timed language we'll use to command our servo.
Gathering Your Arsenal: Components and Setup
To follow along, you'll need a modest collection of parts. Most of these are included in common Arduino starter kits.
Essential Hardware
- An Arduino Board: An Uno, Nano, or Leonardo is perfect for this.
- A Micro Servo Motor: The SG90 is the quintessential, incredibly affordable micro servo and will be our reference model.
- Jumper Wires: A few male-to-male wires, preferably with a female end for connecting to the servo.
- A Breadboard: Useful for intermediate circuits, though not strictly necessary for a basic servo connection.
- A Power Supply: For a single micro servo, the Arduino's 5V USB power is sufficient. For multiple servos or under load, an external 5-6V power source for the servos is recommended to avoid overloading the Arduino's voltage regulator.
The Fundamental Circuit: Making the Connections
The wiring for a basic servo test is beautifully simple. A micro servo like the SG90 has three wires, usually color-coded: * Brown or Black: Ground (GND). Connect this to the Arduino's GND pin. * Red: Power (VCC). Connect this to the Arduino's 5V pin. * Orange or Yellow: Signal (PWM). Connect this to a digital PWM pin on the Arduino (marked with a ~, such as pin 9, 10, or 11 on the Uno).
⚠️ Critical Safety Note: Always double-check your wiring before powering on. Connecting power backwards can instantly damage your servo.
The Foundation: Basic Position Control with Servo.h
Arduino simplifies servo control immensely through its built-in Servo.h library. This library handles the complex timing of the PWM signals for you.
Writing Your First Sweep Sketch
Let's create the "Hello, World!" of servo projects: making it sweep back and forth.
cpp
include <Servo.h> // Include the Servo Library
Servo myServo; // Create a servo object to control it int servoPin = 9; // Pin connected to the servo signal wire
void setup() { myServo.attach(servoPin); // Attaches the servo on pin 9 to the servo object }
void loop() { // Move from 0 degrees to 180 degrees, one degree at a time for (int pos = 0; pos <= 180; pos += 1) { myServo.write(pos); // Command the servo to move to 'pos' degrees delay(15); // Wait 15ms for the servo to reach the position } // Now move back from 180 degrees to 0 degrees for (int pos = 180; pos >= 0; pos -= 1) { myServo.write(pos); delay(15); } }
How the Servo.write() Function Works
When you call myServo.write(90), the library translates the angle (90) into the appropriate pulse width (~1.5ms) and begins sending that signal repeatedly to the servo. The servo's internal electronics compare the commanded position (from the pulse) with the actual position (from the potentiometer) and drive the motor in the correct direction until they match.
Beyond Basics: Advanced Control of Speed and Direction
While Servo.write() is simple, it gives you direct control over only the final position, not the journey to get there. The servo moves at its maximum internal speed. For more nuanced motion, we need different techniques.
Technique 1: Controlled Speed Through Incremental Movement
The most common method for speed control is to break a large movement into many small steps, with a delay between each step.
cpp
include <Servo.h>
Servo myServo; int servoPin = 9; int targetAngle = 150; int currentAngle = 0; int stepDelay = 30; // Higher delay = slower speed
void setup() { myServo.attach(servoPin); myServo.write(currentAngle); // Start at 0 degrees delay(1000); }
void loop() { // Move slowly from currentAngle to targetAngle if (currentAngle < targetAngle) { currentAngle++; // Increment the angle by 1 degree myServo.write(currentAngle); delay(stepDelay); // Control speed with this delay }
// (Add logic here to change targetAngle, etc.) } Manipulating stepDelay is your primary speed control. A smaller delay makes the motion faster and jerkier; a larger delay makes it slower and smoother.
Technique 2: Precision Timing with writeMicroseconds()
For expert-level control, especially with servos that have a non-standard range, you can bypass the angle abstraction and command the pulse width directly.
cpp void loop() { // Move to minimum angle (~0 degrees) myServo.writeMicroseconds(1000); delay(1000); // Move to neutral (~90 degrees) myServo.writeMicroseconds(1500); delay(1000); // Move to maximum angle (~180 degrees) myServo.writeMicroseconds(2000); delay(1000); } This method is invaluable for calibrating servos or driving continuous rotation servos (where 1500us is "stop," 1300us is full speed in one direction, and 1700us is full speed in the other).
Understanding and Controlling Direction
"Direction" for a standard micro servo is inherent in the angle you command. * myServo.write(0); // Direction is "clockwise end-stop." * myServo.write(180); // Direction is "counter-clockwise end-stop." * Moving from 30 to 120 means the direction is counter-clockwise (if viewed from the front).
For true, continuous rotation in either direction, you need a specific type: a Continuous Rotation (CR) Micro Servo. It's mechanically modified to lack the potentiometer's stop. You use the same PWM signal, but it's interpreted as: * ~1500 µs: Stop rotation. * <1500 µs (e.g., 1300 µs):** rotate clockwise at a speed proportional to how far the pulse is from 1500. * **>1500 µs (e.g., 1700 µs): Rotate counter-clockwise at a proportional speed.
Project Integration: Building a Micro Servo Pan-Tilt Head
Let's apply these concepts in a practical mini-project: creating a simple pan-tilt mechanism for a sensor or small camera using two micro servos.
Hardware Assembly
- Mount one servo (the tilt servo) onto a servo horn or a small bracket.
- Attach this assembly to the horn of a second, larger servo (the pan servo).
- Connect the pan servo to Arduino pin 9 and the tilt servo to pin 10.
- Use a capacitor (e.g., 100µF) across the servo power leads if you experience jitter or board resets, as servos can cause power supply noise.
The Control Code
cpp
include <Servo.h>
Servo panServo; Servo tiltServo;
int panAngle = 90; // Center position for pan int tiltAngle = 90; // Center position for tilt int panPin = 9; int tiltPin = 10;
void setup() { panServo.attach(panPin); tiltServo.attach(tiltPin); centerAll(); // Move to a known center position }
void loop() { // Example: Slow automated scan slowScan(60, 120); // Scan pan between 60 and 120 degrees // A manual control loop using serial input or sensors could go here }
void centerAll() { panServo.write(90); tiltServo.write(90); delay(500); }
void slowScan(int startAngle, int endAngle) { for (int a = startAngle; a <= endAngle; a++) { panServo.write(a); delay(50); // Determines panning speed } for (int a = endAngle; a >= startAngle; a--) { panServo.write(a); delay(50); } }
Troubleshooting Common Micro Servo Issues
Even with simple components, you might encounter hurdles. Here’s how to overcome the most common ones.
The Jittery Servo
- Symptom: The servo vibrates, buzzes, or shakes when it should be still.
- Causes & Fixes:
- Electrical Noise: Use a capacitor (100-470µF electrolytic) across the servo's power and ground wires, close to the servo.
- Power Supply Overload: Power the servo from an external 5V/6V source (like a battery pack or dedicated regulator), not the Arduino's 5V pin. Keep the Arduino grounds connected.
- Software Jitter: Ensure your code isn't sending rapidly changing
write()commands. Usedelay()to give the servo time to settle.
The Non-Responsive Servo
- Symptom: The servo doesn't move at all.
- Checklist:
- Wiring: Verify all three connections (Power, Ground, Signal).
- Power: Is the Arduino powered? Is the servo's red wire connected to 5V?
- Correct Pin: Is the signal wire connected to a PWM-capable pin (and is it the one declared in
attach())? - Mechanical Block: Is the servo horn physically obstructed?
The Erratic or Limited Movement
- Symptom: The servo only moves in a small arc or moves to the wrong angles.
- Solutions:
- Calibrate: Use
writeMicroseconds()to find the true minimum (1000-1300µs) and maximum (1700-2000µs) pulses for your specific servo model. Not all servos are exactly 0-180 degrees. - Gear Damage: Inexpensive micro servos have plastic gears that can strip under excessive force. Listen for grinding sounds. This requires physical replacement.
- Calibrate: Use
Optimizing Your Design: Tips and Best Practices
- Decouple Power Supplies: For any project with more than one micro servo or one under load, use a separate battery or power supply for the servos. Connect its ground to the Arduino ground. This prevents brownouts and resets.
- Mind the Load: Micro servos have limited torque. Attach lightweight arms (3D-printed or balsa wood) and ensure loads are balanced. Avoid long lever arms.
- Smooth Motion is King: For professional-looking projects, always use incremental movement with delays instead of instant jumps to new positions. Consider implementing easing functions (like sinusoidal or logarithmic) for even more natural motion.
- Explore Libraries: For advanced sequences and choreographed multi-servo movements, look into libraries like
AccelStepper(which also handles servos) orPCA9685PWM driver libraries if you move to controlling many servos (8, 16, or more) without overloading your Arduino.
Copyright Statement:
Author: Micro Servo Motor
Source: Micro Servo Motor
The copyright of this article belongs to the author. Reproduction is not allowed without permission.
Recommended Blog
- Building a Servo-Controlled Arm with Arduino and Micro Servos
- How to Connect a Micro Servo Motor to Arduino MKR Vidor 4000
- How to Connect a Micro Servo Motor to Arduino MKR IoT Bundle
- How to Connect a Micro Servo Motor to Arduino MKR FOX 1200
- Using Arduino to Control the Rotation Angle, Speed, and Direction of a Micro Servo Motor
- How to Connect a Micro Servo Motor to Arduino MKR WAN 1310
- Using Arduino to Control the Speed and Direction of a Micro Servo Motor
- Using Arduino to Control the Rotation Angle, Speed, and Direction of a Micro Servo Motor
- Using Potentiometers to Control Micro Servo Motors with Arduino
- How to Calibrate Your Micro Servo Motor with Arduino
About Us
- Lucas Bennett
- Welcome to my blog!
Hot Blog
- The Role of Micro Servo Motors in Smart Retail Systems
- The Role of Micro Servo Motors in Smart Home Devices
- The Importance of Gear Materials in Servo Motor Performance Under Varying Accelerations
- Advances in Signal Processing for Micro Servo Motors
- The Relationship Between Signal Width and Motor Angle
- Future Micro Servo Types: Trends & Emerging Technologies
- BEGE's Micro Servo Motors: Engineered for Smooth and Stable Camera Movements
- Micro Servo MOSFET Drivers: Improving Efficiency in Drone Circuits
- Micro Servo Motors in Underwater Robotics: Challenges and Opportunities
- How to Build a Remote-Controlled Car with 4G LTE Control
Latest Blog
- How Specification of Startup Surge Current Impacts Power Supply Design
- Diagnosing and Fixing RC Car Motor Mount Issues
- Using Arduino to Control the Position, Speed, and Direction of a Micro Servo Motor
- Micro Servos with Temperature Sensors / Thermal Protection
- The Role of Micro Servo Motors in Industrial Automation
- The Role of PCB Design in Power Electronics
- Micro Servo Motors in Automated Painting Systems
- The Role of Micro Servo Motors in Industrial Automation
- Vector's Micro Servo Motors: Compact Power for Your Projects
- BEGE's Micro Servo Motors: Engineered for High-Demand Applications
- Understanding the Role of Gear Materials in Servo Motor Performance Under Varying Waveforms
- Micro Servo Solutions for Automated Lighting Shades
- Implementing Servo Motors in Raspberry Pi-Based Robotics Projects
- Micro Servo Braking vs Motor Braking in RC Car Applications
- Lightweight Brackets and Linkages for Micro Servo Use in Drones
- Best Micro Servo Motors for Camera Gimbals: A Price Guide
- The Engineering Behind Micro Servo Motor Feedback Systems
- The Impact of Gear Materials on Servo Motor Load Distribution
- Low-Voltage Micro Servos: When and Where to Use Them
- How to Build a Micro Servo Robotic Arm for a Tech Conference