Home > Industry Insights >Servo
TECHNICAL SUPPORT

Product Support

sg90 micro servo motor code arduino

Published 2026-01-19

Get your micro servo: an easy journey with SG90 and Arduino

Has this happened to you too? I excitedly started a small project, such as making a robotic arm move, or installing an automatic door for my house, but ended up getting stuck at the most basic link - that little servo just didn't obey. The program is uploaded and the cables are connected correctly, but it either shakes or doesn't move at all. At this time, you are staring at the Arduino board and the quiet SG90 micro servo next to it, and you may be a little confused about where to start.

Don't worry, many people have encountered this little trouble. The servo, especially a common model like the SG90, is a very reliable little guy. The problem often lies in the way we “communicate” with it. Today, we will just talk casually about how to make the SG90 and Arduino board communicate smoothly and turn your ideas into a moving reality.

What exactly is the servo "listening" to?

We have to know what signal the servo is waiting for. It is not like an ordinary motor that rotates when it is powered on. It has a small circuit inside that is always waiting for a specific pulse signal from a controller (such as an Arduino). The width of this pulse determines the angle at which the servo axis rotates. You can think of it as receiving a very short command, the length of the command corresponds to "turn 30 degrees to the left" or "turn 90 degrees to the right."

The generally expected pulse period of SG90 is about 20 milliseconds, and the pulse width is between 0.5 milliseconds and 2.5 milliseconds, corresponding to an angle change of 0 to 180 degrees. If the signal is wrong, it either doesn't understand it or it behaves strangely.

Why doesn't my code make it move?

There are several possible reasons. Perhaps the simplest wiring issue: a servo has three wires - power (usually red), ground (usually brown or black), and signal (usually orange or yellow). The power and ground wires need to be connected correctly, and the signal wires should be connected to the correct digital pins on the Arduino.

Furthermore, the power supply may not be powerful enough. The USB port or 5V pin of the Arduino board sometimes cannot provide enough current to the board and the servo at the same time, especially when the servo is started or loaded. At this time, the servo may be unable to rotate, or the Arduino board may restart unexpectedly. An independent 5V power supply for the servo is often the key to solving the problem.

Then comes the code. Using ArduinoservoThe library is very convenient and can be controlled with just a few lines of code. But has the library been imported correctly? Was the servo object created? Are the pin numbers defined correctly? These details are like typing a wrong letter and the entire command becomes invalid.

Here is a simple code to let it say hello first

Having talked so much, let’s just look at a small example. Suppose we connect the SG90 signal line to pin 9 of Arduino.

#include <servo.h> //Introduce the servo libraryservomyServo; // Create a servo object, call it myServo int servoPin = 9; // Define the pin to which the signal line is connected void setup() { myServo.attach(servoPin); // Tell Arduino which pin the servo is connected to } void loop() { myServo.write(0); // Let the servo rotate to the 0 degree position delay(1000); // Wait for 1 second myServo.write(90); // Let the servo rotate to the 90-degree middle position delay(1000); myServo.write(180); // Let the servo rotate to the 180-degree position delay(1000); }

Upload this code, and if all goes well, you should see the servo begin to leisurely swing back and forth between these three positions. It's like shaking its hand and establishing a basic connection.

Make movements smoother and more imaginative

The basic swing works, but you may feel that its movement is a bit abrupt and would like it to be slower and more graceful. At this time, the write() function reaches the specified angle instantly. We can use another idea: change the angle value little by little.

#include  Servo myServo; int pos = 0; // Use a variable to store the current angle void setup() { myServo.attach(9); } void loop() { // Slowly increase from 0 degrees to 180 degrees for (pos = 0; pos <= 180; pos += 1) { myServo.write(pos); delay(15); // Delay a little after each change to control the speed } delay(1000); // Then slowly decrease from 180 degrees to 0 degrees for (pos = 180; pos >= 0; pos -= 1) { myServo.write(pos); delay(15); } delay(1000); }

Now, the servo will scan back and forth smoothly. You can adjust the value in delay(15). The larger the number, the slower the action. With this programming, you can have it simulate a radar scan, slowly opening and closing fan blades, or any effect that requires gentle movement.

Choose the right partner and half the battle is done

Once you get comfortable with it, you might start doing more projects. At this time, a stable and reliable steering gear is very important. There are many SG90 models on the market, but the experience can vary greatly. Some will start to vibrate after a few uses and the noise will become louder; some will have inaccurate angles when a little force is applied.

After all, the micro-servo is a precise electromechanical component. The material of the gear inside, the efficiency of the motor, and the stability of the control circuit jointly determine its response speed, torque, and durability. When choosing, you can pay more attention to products that pay more attention to details, such as the smoothness of the gears, the quietness of the motor when running, and whether it can maintain consistent accuracy in repeated tests.

A good component is like a tacit partner. You give instructions and it executes accurately, eliminating a lot of troubles in debugging and troubleshooting, allowing you to focus more time and creativity on the interesting parts of the project itself.

Give it a try, the fun is in the process

Controlling an SG90 servo, from making it move, to precise control, to realizing complex action sequences, is a process that is full of fun in itself. It's like a small door into the larger world of robotics or autonomous devices. It's natural to encounter signal problems, power problems, or code stuck, and solving them is part of learning.

I hope these scattered sharings can help you start playing with this little device more easily. When your code successfully drives it to perform the desired action for the first time, that small sense of accomplishment is one of the most fascinating moments of hands-on production. I wish you have fun and create more moving little miracles.

Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China. 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. 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 Time:2026-01-19

Powering The Future

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

Mail to Kpower
Submit Inquiry
+86 0769 8399 3238
 
kpowerMap