小编
Published2025-10-15
Sure! Here's the requested soft article divided into two parts with the specified format:
Understanding the SG90 Servo Motor and Arduino
Servo motors are an essential component in many robotics and hobbyist projects. When it comes to small-scale projects, the SG90 servo motor is a favorite. This inexpensive and versatile servo motor is perfect for beginners looking to delve into robotics or for those who need to control precise movements with minimal effort. In this guide, we'll explore how to control the SG90 using Arduino, breaking down the wiring, coding, and key concepts needed to use this motor in your projects.

What is an SG90 Servo Motor?
The SG90 is a small, lightweight servo motor that offers high precision for its size and cost. It operates on a simple control principle: an input signal determines the position of the motor's shaft. This type of motor is known as a "hobby servo" and is most commonly used in robotics, model airplanes, and automated systems.
The SG90 motor has a rotation range of approximately 180 degrees. This means that it can rotate from 0 degrees (its starting position) to 180 degrees, making it highly adaptable to different mechanical tasks. Typically, you would use this servo for tasks requiring precise rotational control, such as adjusting the angle of a camera, controlling a robotic arm, or driving a steering mechanism in a car model.
Components Needed for the Project
To get started with controlling the SG90 servo motor, you'll need a few essential components:
SG90 Servo Motor – The heart of the project, this small motor will rotate based on the signals sent by your Arduino.
Arduino Board – You can use any Arduino model, such as the Arduino Uno or Arduino Nano, which will send signals to the servo.
Jumper Wires – These are used to connect the components.
Breadboard (Optional) – A breadboard can help you create a clean and organized circuit, although it's not strictly necessary for this basic project.
Power Supply – The servo will typically be powered from the Arduino board’s 5V pin, but if you're using many servos, an external power supply may be needed.
Servo motors like the SG90 are controlled using Pulse Width Modulation (PWM), a technique where a signal is sent to the motor with varying pulse widths. The servo measures the length of each pulse and moves to a corresponding position. For example, a pulse width of 1 millisecond may correspond to the 0-degree position, while a pulse of 2 milliseconds might set the servo at 180 degrees.
Understanding PWM is key to mastering servo control with Arduino. The Arduino will generate these signals, and the SG90 will respond accordingly by rotating its shaft. This interaction between the Arduino and the servo is the foundation of controlling the motor.
Wiring the SG90 to Arduino
Connecting the SG90 servo motor to an Arduino board is simple:
Connect the servo's power (red) wire to the 5V pin on the Arduino.
Connect the servo's ground (brown or black) wire to the GND pin on the Arduino.
Connect the signal (orange or yellow) wire to a PWM-enabled pin on the Arduino (usually pin 9 or 10).
Once everything is connected, you’re ready to move on to the programming part. But first, let's take a deeper dive into the PWM signals that control the servo.
Pulse Width Modulation is a method of controlling the power delivered to a device by varying the width of the pulses. For a servo motor, PWM controls the angle of the motor's shaft by adjusting the duration of each pulse. In Arduino programming, this is done using the Servo library, which simplifies controlling the motor.
With the PWM signals, the Arduino board sends a series of pulses to the servo. Each pulse's duration determines the position of the servo. By adjusting the pulse duration, we can precisely control the motor's position. The Servo library helps generate these PWM signals in an easy-to-use format, removing the complexity of calculating pulse lengths manually.
In the next part of this guide, we'll dive into the step-by-step Arduino code needed to control your SG90 servo motor.
Writing the Arduino Code to Control the SG90 Servo Motor
Now that we've covered the basic wiring and operation of the SG90 servo motor, it's time to write the code that will allow the Arduino to control the servo's position. The code we'll use is simple yet powerful, leveraging the built-in Servo library that comes with the Arduino IDE.
Step 1: Setting Up the Arduino IDE
To begin, you'll need the Arduino IDE (Integrated Development Environment) installed on your computer. This is where you'll write, compile, and upload your code to the Arduino board. If you haven’t done so yet, download the IDE from the official Arduino website and install it on your system.
Once you have the IDE open, create a new sketch (a program in Arduino parlance) and save it with a name that makes sense for your project, such as "SG90ServoControl."
Step 2: Including the Servo Library
The first step in writing your code is to include the Servo library. This library comes pre-installed with the Arduino IDE and allows you to control servos without needing to manually generate PWM signals.
#include // Include the Servo library
Step 3: Defining the Servo Object
Next, you'll define a Servo object in your code. This object will represent the servo motor and be used to send commands to it.
Servo myServo; // Create a Servo object
Step 4: Setting Up the Servo in the setup() Function
In the setup() function, you’ll initialize the servo motor by attaching it to a specific pin on the Arduino board. This pin will send the PWM signals to the servo.
myServo.attach(9); // Attach the servo to pin 9 (adjust if using a different pin)
The attach() function links the servo to the pin you've chosen, allowing the Arduino to control it.
Step 5: Controlling the Servo in the loop() Function
Now it's time to control the servo’s position. The write() function of the Servo library allows you to set the servo's position. The argument passed to write() should be a value between 0 and 180, representing the angle of the servo shaft.
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
In the loop() function, we use the write() function to set the servo's position. The servo will rotate to the given angle (0, 90, and 180 degrees in this case), and then the program waits for 1 second using the delay() function before moving to the next position. This cycle will repeat indefinitely.
Step 6: Uploading the Code to Arduino
Once you've written the code, it's time to upload it to the Arduino board. Connect your Arduino to your computer via USB, and click the "Upload" button in the Arduino IDE. The code will be compiled and sent to the Arduino board.
After uploading the code, your SG90 servo motor should start rotating between the positions 0, 90, and 180 degrees, with a 1-second delay between each movement.
Step 7: Expanding the Project
The basic servo control example provided here is just the beginning. You can expand this project in many ways, such as adding multiple servos, controlling the servo with sensors (like a potentiometer), or even using a Bluetooth or Wi-Fi module to control the servo wirelessly.
By experimenting with different components and code modifications, you'll quickly learn more about controlling not just servos but all types of motors and actuators with Arduino.
With these steps, you've learned how to wire and code the SG90 servo motor with Arduino. From here, the possibilities are endless—whether you're building a robotic arm, a model car, or an automated system, controlling servo motors opens up many exciting opportunities in electronics and robotics.
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 Kpower's product specialist to recommend suitable motor or gearbox for your product.