小编
Published2025-10-15
Introduction to SG90 Servo Motor and Its Uses
Servo motors are one of the most commonly used components in robotics, automation, and various DIY projects. Among the different types of servo motors, the SG90 servo motor is one of the most popular due to its small size, affordable price, and precise control. When paired with an Arduino, the SG90 can be used for various applications, from robotic arms to camera panning systems. In this article, we will walk you through how to connect the SG90 servo motor to an Arduino board and control it using simple coding techniques.
Understanding the SG90 Servo Motor
The SG90 is a micro servo motor that typically operates on 4.8-6V DC voltage and can rotate to specific angles with precision. It's a standard 3-wire servo with three connections:
Power (Red): Provides voltage to the motor.
Ground (Black or Brown): Provides the reference ground for the motor.
Signal (Yellow or Orange): Receives the PWM (Pulse Width Modulation) signal from the Arduino, which determines the position of the motor.
What makes the SG90 unique is its ability to precisely control angular movements. It typically rotates between 0 to 180 degrees, and you can set the angle based on the input signal from the Arduino.
Why Use SG90 with Arduino?
The SG90 servo is ideal for beginners due to its affordability, simplicity, and ease of use with Arduino. Here are a few reasons why you might want to pair the SG90 with your Arduino:
Cost-Effective: SG90 servos are inexpensive and accessible, making them a great option for hobbyists or for learning purposes.
Precision Control: The servo allows for precise control of its position, making it ideal for robotic applications or automated systems.
Compact Size: Its small size means it can fit into a wide range of projects, even those with space constraints.
Whether you're working on a robotic arm, a camera mount, or even a self-moving car, the SG90 can help you achieve accurate movement with minimal cost.
Connecting the SG90 Servo Motor to an Arduino Board
Now that we understand the basic functionality of the SG90, let’s move on to the practical part: connecting it to an Arduino board. Here’s how you can set up the hardware.
Step 1: Gather Your Materials
Before starting, make sure you have the following materials:
Arduino board (Uno, Nano, or any compatible board)
Breadboard (optional, for ease of connections)
Step 2: Wiring the Connections
The SG90 has three pins that need to be connected to the Arduino. These pins are: Power (Red), Ground (Black/Brown), and Signal (Yellow/Orange). Here's the connection:
Red wire (Power): Connect this to the 5V pin on the Arduino.
Black or Brown wire (Ground): Connect this to one of the GND pins on the Arduino.
Yellow or Orange wire (Signal): Connect this to a PWM-capable pin on the Arduino, such as pin 9 (Note: the signal pin requires a PWM signal to control the motor’s position).
In most cases, connecting the servo’s power directly to the Arduino’s 5V pin will work well, but if you are using multiple servos or need more power, consider using an external power supply.
Step 3: Writing the Code to Control the SG90
Once the hardware is set up, you need to write the code to control the servo's movement. The Arduino IDE provides a built-in library called Servo.h that makes controlling servos very simple.
Here’s a basic code to move the SG90 servo to different angles:
Servo myServo; // Create a servo object
myServo.attach(9); // Attach the servo to pin 9
myServo.write(0); // Move servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move servo to 180 degrees
delay(1000); // Wait for 1 second
This code moves the servo to three different positions (0°, 90°, and 180°), pausing for 1 second between each movement.
After writing the code in the Arduino IDE, connect your Arduino to your computer via USB and click on Upload. Once the code is uploaded successfully, your SG90 servo will start rotating to the specified angles. You should see the motor turning to 0°, then 90°, and finally 180° in a repeating cycle.
Advanced Control and Troubleshooting
While controlling the SG90 servo with basic code is fun and informative, you can take your project a step further with more advanced control techniques and troubleshooting tips. In this section, we’ll explore how to adjust the speed of the servo, use external power, and handle common issues.
Controlling the Speed of the SG90 Servo
By default, the servo moves instantly to the desired angle. However, you might want to make the servo move more smoothly and gradually. This can be achieved by adding a small delay between each incremental change in the angle.
Here’s how you can modify your code to gradually move the servo from one angle to another:
myServo.attach(9); // Attach the servo to pin 9
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle); // Gradually move to the new angle
delay(15); // Wait 15ms to let the servo reach the position
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle); // Gradually move back to the original angle
delay(15); // Wait 15ms to let the servo reach the position
In this code, we use a for loop to move the servo from 0° to 180° and back, gradually changing the angle by 1 degree at a time. The delay(15) function ensures the servo has time to reach each position, making the movement smooth.
Using External Power for Multiple Servos
If you're planning to use multiple servos or need more power for your SG90, it's recommended to use an external power supply. Arduino boards can only supply a limited amount of current (around 500mA for most boards). Connecting multiple servos directly to the Arduino’s 5V pin could cause it to overload.
To use an external power supply:
Connect the positive (red) wire of the power supply to the power rail on the breadboard.
Connect the ground (black) wire of the power supply to the ground rail of the breadboard.
Ensure that the Arduino ground (GND) is connected to the breadboard ground.
This way, the servos will be powered independently of the Arduino, preventing power issues.
Common Issues and Troubleshooting
While working with SG90 servos and Arduino, you may encounter some common problems. Here are a few issues and tips on how to fix them:
Servo Not Moving: Check your connections carefully. Ensure the power, ground, and signal wires are properly connected to both the servo and the Arduino.
Servo Moving Erratically: If the servo is moving erratically, it might be receiving noisy signals. Try adding a capacitor (10µF) across the power and ground pins of the servo to filter noise.
Servo Not Reaching Desired Angles: The SG90 has limited torque, which can cause it to struggle with heavy loads. Make sure the servo isn’t under too much mechanical stress or weight.
By troubleshooting these issues, you can ensure that your servo operates smoothly and reliably in your Arduino projects.
With these techniques, you now have the basics and more advanced skills to incorporate the SG90 servo motor into a wide range of Arduino-based projects. Whether you are just starting or looking to expand your knowledge, the SG90 combined with Arduino offers a world of possibilities!
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.