小编
Published2025-10-15
Understanding SG90 Servo Motors and Arduino Uno
The SG90 servo motor is one of the most popular and affordable servo motors in the electronics and DIY robotics world. It’s widely used in projects where precise rotational movement is required, such as robotic arms, camera gimbals, or simple mechanical projects. But how exactly does this tiny powerhouse work, and how can you control it using an Arduino Uno? In this guide, we’ll break down the key concepts and show you how to make it all come to life.
Before diving into the specifics of using an SG90 servo motor, let’s first understand what a servo motor is. A servo motor is a small motor equipped with a built-in feedback system that allows it to rotate to a specific position based on the signal it receives. Unlike regular motors, which rotate continuously when powered, a servo motor can rotate within a defined range—usually 0° to 180° for the SG90. This precise control is what makes servo motors so useful in applications where accuracy is crucial.
The SG90 servo motor is a micro-sized servo, which means it’s small, lightweight, and perfect for compact robotics projects. Despite its small size, it is capable of delivering decent torque (around 1.2 kg·cm) at 5V, making it ideal for tasks like turning, tilting, or even steering.
The Role of Arduino Uno in Servo Motor Control
Arduino Uno, a popular microcontroller board, is the perfect match for controlling servo motors like the SG90. The Arduino Uno provides a simple platform for programming and controlling various electronic components, including motors, sensors, LEDs, and more. It’s the brain behind the operation, allowing you to send control signals to the servo motor and specify its position.
Arduino uses Pulse Width Modulation (PWM) to control servo motors. PWM is a technique in which the signal sent to the motor is a series of high and low pulses, with the width (or duration) of the pulse determining the motor’s position. For example, a pulse width of around 1 millisecond will make the servo rotate to 0°, while 2 milliseconds will rotate it to 180°.
Setting Up Your Components
Before we dive into the code, let’s take a look at how to physically connect the SG90 servo motor to your Arduino Uno.
1 x External power source (optional, if your Arduino cannot provide enough current)
Wiring the SG90 Servo to Arduino:
Power Pin (Red wire on SG90): Connect this to the 5V pin on the Arduino Uno. The SG90 operates at 5V, and it’s important to provide it with enough power for smooth operation.
Ground Pin (Black or Brown wire on SG90): Connect this to the GND (ground) pin on the Arduino Uno to complete the circuit.
Control Pin (Yellow or Orange wire on SG90): This is the signal pin that will receive the PWM signal from the Arduino. Connect this wire to one of the PWM-enabled digital pins on your Arduino. Commonly, pin 9 is used, but any other PWM pin (pins 3, 5, 6, 10, or 11) will also work.
Optional: External Power Source
If you're running multiple servos or your Arduino board is powered through USB (which might not provide enough current for the motor), you may need an external power supply. In such cases, connect the servo’s power wire to the 5V output of the external power supply and ensure the power supply shares a common ground with the Arduino.
Writing the Code to Control the SG90 Servo Motor
Now that you have the hardware set up, let’s get to the exciting part—writing the code! If you’ve worked with Arduino before, you’ll find controlling a servo motor quite simple, thanks to the Servo library that comes pre-installed in the Arduino IDE.
Step-by-Step Code Breakdown
Include the Servo Library:
The first step is to include the Servo library. This library makes controlling servos easy by handling the timing and PWM signals for you.
Next, we create a Servo object that we can use to control the motor. Let’s call it myServo for simplicity.
Now, we define which Arduino pin will send the control signal to the servo. In this case, we’ll use pin 9.
Set Up the Servo in the setup() Function:
In the setup() function, we initialize the servo by attaching it to the defined pin. This prepares the motor to receive commands.
myServo.attach(servoPin);
Moving the Servo in the loop() Function:
The main action happens in the loop() function, where you can send commands to the servo to move it to specific angles. The write() function is used to specify the angle (0° to 180°).
// Rotate the servo to 0°
delay(1000); // Wait for 1 second
// Rotate the servo to 90°
myServo.write(90);
delay(1000); // Wait for 1 second
// Rotate the servo to 180°
myServo.write(180);
delay(1000); // Wait for 1 second
myServo.attach(servoPin): This tells Arduino that the servo is connected to the pin defined earlier (pin 9).
myServo.write(angle): This command moves the servo to a specific angle, where angle can range from 0° to 180°.
delay(time): After moving the servo, the delay() function waits for a specified time (in milliseconds) to allow the servo to reach the target position.
Now that the servo moves back and forth between 0°, 90°, and 180°, you can experiment with different angles and timings. You can also make your servo perform more complex tasks by integrating sensors, inputs (like potentiometers or buttons), or even other motors.
Servo not moving? Double-check your wiring and ensure the servo is powered properly. If you’re using USB power from your computer, it might not supply enough current for the servo, so consider using an external power source.
Erratic behavior? Make sure you’ve included enough delay time after moving the servo to give it time to reach the target position before sending a new command.
This guide introduces you to the basics of controlling an SG90 servo motor with Arduino Uno, from wiring to writing code. In part 2, we will delve deeper into more advanced control techniques, including adding sensors to automate movement and creating complex servo-based systems. Stay tuned!
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.