小编
Published2025-10-15
Introduction: The Magic of Servo Motors and Arduino
Imagine transforming a simple mini-motor into the heartbeat of your robotic project. Whether you're an experienced maker or just starting out, understanding how to control servo motors like the SG90 with Arduino opens up a universe of possibilities. These small, inexpensive components pack a surprising punch—offering precise movement, ease of control, and endless opportunities for creativity.
The SG90 servo motor, often called a “micro servo,” has become an industry favorite for hobbyists. Its compact size, affordability, and reliable performance make it ideal for a variety of projects—robotic arms, camera gimbals, remote-controlled vehicles, and more. Pairing it with an Arduino microcontroller enables you to craft highly responsive systems capable of complex movements.
This guide begins with the basics: what is the SG90 servo? How does it work? How to connect it properly to an Arduino? Then, we’ll explore foundational code snippets to make your servo move smoothly to different positions. Later, we'll step into more advanced control techniques, including continuous rotation, speed management, and feedback systems. Along the way, you’ll find inspiration to upgrade and customize your projects.
The SG90 is a miniature, lightweight servo motor that provides rotational movement typically within a 180-degree arc—although some can turn up to 360 degrees with modifications. Its core features include:
Voltage Range: Usually operates between 4.8V and 6V, making it compatible with most Arduino boards. Torque: Offers about 1.2 kg·cm (13.2 oz·in) at 4.8V—ideal for light-duty applications like model arms or small robots. Size: Compact (23.5mm x 12.2mm x 29mm), fitting snugly into tight spaces. Control: Uses PWM (Pulse Width Modulation) signals to position the servo’s shaft at a specified angle, generally between 0° and 180°.
Its affordability and straightforward operation have made it a staple in the maker community.
The Heart of Control: How Does a Servo Work?
Servo motors like the SG90 operate with a feedback control system. They contain a small motor, gears, a potentiometer, and an integrated control circuit. When you send a PWM signal to the servo, it commands the servo to rotate to a specific position:
A 1-millisecond pulse (roughly) corresponds to 0°. A 2-millisecond pulse corresponds to 180°. Pulses in between translate to intermediate angles.
The internal circuitry compares the input signal with the position of the potentiometer. It then adjusts the motor’s direction and speed to achieve that single target position, maintaining it until a new command arrives.
To get started, you'll need:
An Arduino board (Uno, Mega, Nano, etc.) An SG90 servo motor Jumper wires A power supply (preferably 5V)
Connect the servo's red wire to Arduino's 5V Connect the servo's black/brown wire to GND Connect the servo's yellow/orange wire (signal) to a digital PWM pin, commonly Pin 9
Note: For more stability, especially when powering multiple servos, use an external power supply for the servo, and connect grounds together.
The Core Arduino Code: Moving Your First Servo
Let’s start with a simple code snippet to make the SG90 go from 0° to 180° and back.
#include Servo myServo; // create a servo object void setup() { myServo.attach(9); // attaches the servo on pin 9 } void loop() { for (int pos = 0; pos <= 180; pos += 1) { // go from 0° to 180° myServo.write(pos); delay(15); // waits 15ms for the servo to reach the position } for (int pos = 180; pos >= 0; pos -= 1) { // go back from 180° to 0° myServo.write(pos); delay(15); } }
This simple loop smoothly rotates the servo back and forth. Modifying the angles, delay, or code structure allows for endless customization.
Fine-Tuning and Troubleshooting
To get the most consistent movements:
Ensure your power supply provides enough current—servos can draw significant current, especially when under load. Use a separate power supply for multiple servos. Keep signal wires short to prevent noise. Use the myServo.detach() method to disconnect the servo if you want to move it by manual means later.
What's Next? Moving Beyond Basic Control
Once comfortable with basic movements, you can explore:
Servo feedback: Reading the current position with sensors Speed control: Adjusting how fast your servo reaches its position Continuous rotation: Modifying the servo to spin endlessly like a motor (requires hardware modifications) Sensor integration: Using potentiometers, IR sensors, or accelerometers for responsive control
This foundation sets you up for more complex projects—think robotic arms, automated curtains, or artistic installations.
Stay tuned for Part 2, where we delve into advanced control techniques, coding intricacies, troubleshooting tips, and inspiring project ideas to push your servo and Arduino skills even further.
Part 2 will continue from here to deepen your understanding and expand your project horizons.
Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.