Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

Mastering the Servo Motor SG90 with Arduino: A Beginner’s Guide to Precise Control and Automation

小编

Published2025-10-15

Introduction to Servo Motors and SG90 in Arduino Projects

In the world of robotics and automation, precise movement is essential, and the servo motor is one of the most reliable components for this task. Whether you're building a robotic arm, a camera gimbal, or an automated plant watering system, the ability to control the angle of a servo motor is crucial. Among the various types of servo motors, the SG90 is one of the most popular choices, especially for beginner projects, due to its affordability, small size, and ease of use.

The SG90 is a micro servo motor that operates on a 5V power supply and offers precise control over its rotation. With an angular range of 0 to 180 degrees, this servo motor can be used for a wide variety of applications in robotics, automation, and electronics. It is commonly used in projects involving simple movement or position adjustments, like turning a steering wheel or controlling the position of a camera.

In this article, we will explore how to control the SG90 servo motor using an Arduino. Arduino, a popular open-source platform, is ideal for beginners looking to explore the world of electronics and automation. By the end of this guide, you’ll be able to write your own Arduino code to control the servo motor with ease, allowing you to create projects that respond to input or move automatically.

What is a Servo Motor?

Before we dive into the specifics of the SG90, it’s important to understand the basics of servo motors. A servo motor is a small, versatile motor used to achieve precise angular motion. Unlike standard DC motors that spin continuously, servo motors can only rotate within a specific range—typically 0 to 180 degrees.

Servos are usually controlled by sending a Pulse Width Modulation (PWM) signal to the motor. The width of the pulse determines the angle of the motor. For instance, a shorter pulse might turn the servo to 0 degrees, while a longer pulse moves it to 180 degrees.

There are various types of servo motors, but the SG90 is particularly well-suited for small projects that require limited rotation and precision. It can be powered directly from the Arduino board, which makes it highly convenient for hobbyists and students alike.

Components You’ll Need for This Project

To get started with controlling the SG90 servo motor, you’ll need the following components:

Arduino Board: An Arduino Uno is ideal for this project, but any Arduino board should work.

SG90 Servo Motor: The star of the project—small, lightweight, and precise.

Jumper Wires: These will connect your Arduino board to the servo.

Breadboard (Optional): You may use this to help organize your components, though it’s not strictly necessary for this simple setup.

Power Supply: The Arduino can power the servo, but if you need more torque, a separate power supply might be required.

Wiring the SG90 Servo to Arduino

The wiring of the SG90 servo motor is simple. The servo has three pins: Power (VCC), Ground (GND), and Signal (PWM). Here’s how you can connect them:

VCC (Power Pin): Connect this pin to the 5V output on the Arduino.

GND (Ground Pin): Connect this pin to one of the GND pins on the Arduino.

Signal (PWM Pin): Connect this pin to a PWM-capable digital pin on the Arduino, such as Pin 9.

Now, you’re ready to move on to writing the code that will control the SG90 servo motor.

Writing the Arduino Code to Control the SG90 Servo Motor

With your components connected, it’s time to write the code. Arduino uses a programming language based on C++, and there are built-in libraries that make controlling the servo motor incredibly simple.

Step 1: Including the Servo Library

The first step in writing the code is to include the Servo library, which contains the necessary functions for controlling the servo motor. This library is built into the Arduino IDE, so you don’t need to install anything extra.

To include the Servo library, add the following line at the top of your code:

#include

Step 2: Declaring the Servo Object

Next, declare a Servo object that will represent your motor. This object will allow you to control the position of the servo.

Servo myServo;

You can name the object whatever you like, but myServo is a common convention. This object will be used to control the servo throughout the rest of the program.

Step 3: Initializing the Servo in the Setup Function

In Arduino, the setup() function runs once when the program starts. This is where you’ll initialize the servo motor and tell it which Arduino pin it’s connected to.

void setup() {

myServo.attach(9); // Connect the servo to pin 9

}

Here, we are attaching the servo to digital pin 9 on the Arduino board. This tells the Arduino which pin to send the PWM signal to.

Step 4: Moving the Servo to a Specific Angle

The next step is to control the servo’s movement. You can use the write() function to set the angle of the servo. The write() function takes a value between 0 and 180, representing the angle to which the servo should move.

Here’s an example of moving the servo to 90 degrees:

void loop() {

myServo.write(90); // Move the servo to 90 degrees

delay(1000); // Wait for 1 second

}

In this code, the servo will move to 90 degrees and then stay there for one second (1000 milliseconds). The delay() function is used to pause the program before moving on to the next instruction. You can adjust the value inside write() to set the servo to different angles, such as 0, 45, or 180 degrees.

Step 5: Sweeping the Servo Back and Forth

To make your servo move back and forth, you can use a loop that gradually changes the servo angle. This creates a sweeping motion.

Here’s an example:

void loop() {

for (int pos = 0; pos <= 180; pos++) { // Sweep from 0 to 180 degrees

myServo.write(pos);

delay(15); // Wait for the servo to reach the position

}

for (int pos = 180; pos >= 0; pos--) { // Sweep from 180 to 0 degrees

myServo.write(pos);

delay(15); // Wait for the servo to reach the position

}

}

In this code, the servo will sweep from 0 to 180 degrees and then back from 180 to 0 degrees, with a slight delay between each movement. This creates a smooth back-and-forth motion that you can use in various applications.

Step 6: Troubleshooting and Tips

While the SG90 is an easy servo to use, there are a few common issues you might encounter:

Servo Not Moving: Double-check your wiring and ensure the servo is correctly connected to the power, ground, and signal pins.

Servo Jittering: If the servo is jittering or moving erratically, it could be due to insufficient power. Try powering the servo from an external power supply instead of the Arduino.

Servo Stalls: If the servo isn’t moving to the correct position, it may be overloaded. Make sure the servo is not trying to push against an obstruction or move beyond its mechanical limits.

With the above steps, you now have the foundational knowledge to start using the SG90 servo motor in your Arduino projects. You can experiment with different movements, incorporate sensors for automated control, or integrate multiple servos to create more complex robotic systems.

This two-part guide has equipped you with the knowledge to integrate the SG90 servo motor with Arduino. By mastering this basic component, you’re now ready to embark on exciting projects, from simple robotic arms to more advanced automation systems!

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.