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

Mastering the SG90 Servo Motor with Arduino: A Comprehensive Guide for Beginners

小编

Published2025-10-15

Introduction to SG90 Servo Motor and Arduino

The world of electronics and robotics has seen a rapid surge in interest, particularly with the rise of accessible platforms like Arduino. One of the most popular components used in these projects is the SG90 servo motor. This small but powerful motor plays a crucial role in various applications, from robotics to remote control systems. In this article, we’ll walk you through how to control the SG90 servo motor using Arduino, explore the basic wiring setup, and understand the programming concepts involved.

What is an SG90 Servo Motor?

The SG90 servo motor is a small, lightweight, and affordable motor that is commonly used in beginner-level robotics and DIY electronics projects. Despite its compact size, the SG90 can rotate in a precise manner, typically through an angle of 0° to 180°. This ability to rotate accurately makes it ideal for tasks such as steering a robot, controlling camera angles, or even adjusting the position of a robotic arm.

What makes the SG90 special is its built-in control circuit that allows you to control the motor’s rotation angle. Unlike a standard DC motor, which rotates continuously when powered, a servo motor moves only to a specific position determined by a control signal, often in the form of Pulse Width Modulation (PWM).

How Does the SG90 Servo Motor Work?

The SG90 servo motor is controlled by providing it with PWM signals. These signals consist of pulses with varying widths. The width of the pulse dictates the position of the motor. For example, a pulse width of 1.5 milliseconds typically positions the servo at the center (90°), while a 1-millisecond pulse moves it to 0°, and a 2-millisecond pulse moves it to 180°.

The Arduino board, which is an open-source microcontroller platform, is perfect for generating these PWM signals. By connecting the SG90 to the Arduino, you can easily control the servo’s position through simple code.

Components Required

Before we dive into the code, let’s make sure you have all the necessary components to get started with your SG90 servo motor project:

SG90 Servo Motor: The star of the show, this small motor will provide precise movement for your project.

Arduino Board: An Arduino Uno or any compatible board will work well for controlling the servo.

Jumper Wires: Used to make connections between the Arduino and the servo motor.

Breadboard (optional): For prototyping your circuit and making connections more organized.

External Power Supply (optional): Although the Arduino can power the servo, a dedicated external power supply may be necessary for larger or multiple servos.

Wiring the SG90 Servo Motor to Arduino

The wiring for controlling an SG90 servo motor with an Arduino is straightforward. The servo has three wires: Power (Red), Ground (Brown or Black), and Signal (Orange or Yellow). Here’s how to wire the motor:

Power (Red): Connect this wire to the 5V pin on the Arduino board. This provides the motor with the required power.

Ground (Black/Brown): Connect this wire to one of the GND (Ground) pins on the Arduino.

Signal (Yellow/Orange): Connect the signal wire to any of the digital pins on the Arduino. For example, you can use pin 9.

Now, you’re ready to write the code to control the servo motor.

Writing Arduino Code to Control the SG90 Servo Motor

Once the hardware is set up, the next step is to write the code that will allow you to control the servo motor’s position. The Arduino IDE (Integrated Development Environment) is the software used to write, compile, and upload the code to the Arduino board.

Basic Servo Motor Code

The Arduino IDE has a built-in library called Servo.h that makes controlling the servo motor incredibly easy. To begin, you need to include this library at the top of your code, which will provide the necessary functions to control the servo.

Here’s a simple code snippet to get you started:

#include

Servo myServo; // Create a Servo object

void setup() {

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

}

void loop() {

myServo.write(0); // Move the servo to 0°

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

myServo.write(180); // Move the servo to 180°

delay(1000); // Wait for 1 second

}

This basic example does the following:

Setup: It attaches the servo motor to pin 9 on the Arduino.

Loop: It moves the servo to three different positions: 0°, 90°, and 180°, with a 1-second delay between each move.

Understanding the Code

Servo Library: The Servo.h library provides the functions attach() and write() to control the servo. The attach() function links the servo to a specific pin, while the write() function sends a pulse to the motor to move it to a specific angle.

myServo.write(): The write() function takes an angle (from 0 to 180) and moves the servo to that position. In the example above, the servo moves to 0°, 90°, and 180° one after the other.

delay(): The delay() function pauses the execution of the program for the specified number of milliseconds. In this case, it waits for 1000 milliseconds (or 1 second) between movements.

Experimenting with Servo Positions

Now that you have the basic code, you can experiment by changing the angles and delay times. For instance, you could create a function to make the servo rotate back and forth continuously or even use a potentiometer to control the servo position dynamically.

Here’s an example of continuous back-and-forth movement:

#include

Servo myServo;

void setup() {

myServo.attach(9);

}

void loop() {

for (int angle = 0; angle <= 180; angle++) {

myServo.write(angle); // Gradually move from 0° to 180°

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

}

for (int angle = 180; angle >= 0; angle--) {

myServo.write(angle); // Gradually move from 180° to 0°

delay(15); // Wait for 15ms

}

}

This code will cause the servo to sweep from 0° to 180° and back to 0° continuously. You can adjust the speed of the sweep by changing the delay() value.

Troubleshooting Common Issues

Servo Not Moving: Double-check your wiring connections, especially the power and ground connections. Ensure that the servo’s signal wire is connected to the correct digital pin.

Servo Sticking: If the servo seems to get stuck or not move smoothly, it could be due to insufficient power. While Arduino can power a single servo, multiple servos or high-power motors may require an external power supply.

Incorrect Movement: If the servo moves too far or not far enough, you may need to adjust the angle values passed to myServo.write(). Keep in mind that not all servos have the same range, so experiment with different values.

In the next part of this guide, we will explore more advanced topics like controlling multiple servos, using a potentiometer to control the servo position, and building a practical project with the SG90 servo motor and Arduino. Stay tuned for more!

Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.

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.