How to Create a Simple Servo Sweep Program with Arduino

How to Connect a Micro Servo Motor to Arduino / Visits:9

The micro servo motor is one of the most satisfying components you can add to an Arduino project. It is small, affordable, widely available, and capable of producing precise angular motion with just a few lines of code. Whether you are building a robotic arm, a camera panning mechanism, an automated door latch, or a tiny animatronic figure, the micro servo is often the first actuator that makes a project feel alive.

Among the classic beginner projects in the Arduino world, the servo sweep is a rite of passage. It demonstrates how to control position, how to use a library, how to wire an external component safely, and how to think in terms of motion rather than simple on/off behavior. In this guide, you will build a simple servo sweep program from scratch, understand how it works, and learn how to customize it for real-world use.

Why the Micro Servo Motor Is Such a Popular Choice

Small Size, Big Impact

The micro servo motor, often modeled after the SG90 or MG90S, weighs only a few grams and measures roughly the size of a matchbox. That tiny footprint makes it ideal for projects where space is limited. You can mount it inside a small robot chassis, attach it to a lightweight sensor platform, or embed it in a costume or prop without adding noticeable bulk.

Despite its size, a micro servo can deliver enough torque for many hobby applications. It can move small levers, rotate lightweight brackets, and position sensors with repeatable accuracy. This combination of small size and useful performance is exactly why it remains a best seller in electronics kits.

Built-In Position Control

Unlike a standard DC motor that simply spins when power is applied, a servo motor contains internal circuitry that interprets a control signal as a target angle. In most hobby servos, that angle ranges from 0 to 180 degrees. The servo compares its current position with the requested position and drives its internal motor until the two match. This closed-loop behavior is what makes servos so convenient. You do not need to add an encoder or write a PID controller just to move a small arm to a specific angle.

The Arduino Servo Library Makes It Easy

Arduino includes a built-in Servo library that handles the timing-sensitive pulse generation for you. Instead of manually toggling a pin with microsecond precision, you can simply call myservo.write(90) and the library takes care of the rest. This abstraction is a huge reason why the servo sweep is such a common first project. It lets you focus on the behavior you want rather than the low-level signal details.

What You Will Need

Before writing code, gather the following parts:

  • An Arduino board such as an Uno, Nano, or Leonardo
  • One micro servo motor, such as an SG90 or MG90S
  • A breadboard and jumper wires
  • A separate 5V power supply for the servo, if you plan to move larger loads or multiple servos
  • A USB cable to program the Arduino

For a single unloaded micro servo, you can often power it directly from the Arduino 5V pin during quick experiments. However, it is better practice to use an external 5V supply, especially if the servo will encounter resistance or if you are using more than one. Servos can draw sudden bursts of current, and those bursts can cause the Arduino to brown out or reset.

Understanding the Servo Signal

Pulse Width Modulation Is Not Quite the Same Here

Many people first encounter PWM when dimming an LED. A servo also uses pulses, but the meaning is different. For a typical hobby servo, a pulse of about 1 millisecond corresponds to 0 degrees, a pulse of about 1.5 milliseconds corresponds to 90 degrees, and a pulse of about 2 milliseconds corresponds to 180 degrees. These pulses repeat roughly every 20 milliseconds.

The Servo library converts your angle value into the correct pulse width. That is why you can write a simple value like 45 or 135 and trust the servo to move to the right place.

Power and Ground Matter

A servo has three wires: power, ground, and signal. The colors vary by manufacturer, but a common arrangement is red for power, brown or black for ground, and orange, yellow, or white for signal. Connect power to a reliable 5V source, ground to the Arduino ground, and signal to a digital pin. If you use an external supply, you must connect the external supply ground to the Arduino ground. Without a common ground, the signal has no reference and the servo may twitch unpredictably or not move at all.

Wiring the Circuit

For this project, assume you are using a single SG90 micro servo and an Arduino Uno.

  1. Connect the servo red wire to the 5V pin on the Arduino for a quick test, or to the positive terminal of an external 5V supply.
  2. Connect the servo brown or black wire to the Arduino GND pin, and to the external supply ground if you are using one.
  3. Connect the servo orange, yellow, or white signal wire to digital pin 9 on the Arduino.

Pin 9 is a good choice because it is one of the pins supported by the Servo library on the Uno. You can use other digital pins, but keep in mind that the library has practical limits on how many servos it can control at once, especially on smaller boards.

Writing the Servo Sweep Program

Starting with the Library

Open the Arduino IDE and create a new sketch. The first step is to include the Servo library and create a servo object.

cpp

include <Servo.h>

Servo myServo;

The Servo object represents your motor. You can create more than one if you have multiple servos, but for now one is enough.

Setting Up the Pin

In the setup() function, attach the servo object to the pin you wired.

cpp void setup() { myServo.attach(9); }

The attach() function tells the library which pin will send pulses to the servo. After this call, the library begins managing the timing in the background.

Creating the Sweep Motion

Now comes the fun part. In the loop() function, move the servo from 0 to 180 degrees in small steps, then back down again.

cpp void loop() { for (int angle = 0; angle <= 180; angle += 1) { myServo.write(angle); delay(15); }

for (int angle = 180; angle >= 0; angle -= 1) { myServo.write(angle); delay(15); } }

This is the classic sweep. The first loop increases the angle one degree at a time. The second loop decreases it. The delay(15) gives the servo time to reach each position before the next command arrives. If you remove the delay entirely, the servo may buzz, jitter, or skip, because it cannot physically keep up with instant jumps across the full range.

The Complete Sketch

Here is the full program in one place:

cpp

include <Servo.h>

Servo myServo;

void setup() { myServo.attach(9); }

void loop() { for (int angle = 0; angle <= 180; angle += 1) { myServo.write(angle); delay(15); }

for (int angle = 180; angle >= 0; angle -= 1) { myServo.write(angle); delay(15); } }

Upload this to your Arduino, and you should see the micro servo arm glide back and forth. If it does not move, check your wiring, confirm that the signal wire is on pin 9, and make sure the servo has power.

Tuning the Sweep for Smoother Motion

Adjusting the Delay

The delay value controls how fast the sweep runs. A delay of 15 milliseconds per degree produces a full sweep in about 2.7 seconds. If you want a slower, more dramatic motion, increase the delay to 25 or 30 milliseconds. If you want a faster snap, reduce it to 5 or 10 milliseconds. Just remember that every servo has a maximum speed. Asking it to move faster than it can will cause buzzing and incomplete motion.

Changing the Range

Not every project needs the full 180 degrees. A camera pan might only need 60 degrees of movement. A small flag waver might need 30. You can change the start and end values in the loops to limit the range.

cpp for (int angle = 45; angle <= 135; angle += 1) { myServo.write(angle); delay(20); }

This creates a gentle back-and-forth motion in the center of the servo's range, which is often smoother and quieter than slamming into the mechanical limits.

Using Smaller Steps for Extra Smoothness

Instead of one-degree steps, you can use half-degree steps if your servo and library support it. The standard Servo library uses integer angles, so half degrees are not directly available. However, you can achieve a similar effect by using a smaller delay and a larger number of intermediate positions with a different library, or by writing your own pulse control. For most beginners, one-degree steps with a reasonable delay are more than enough.

Common Problems and How to Fix Them

The Servo Twitches but Does Not Move

This usually means the power supply is inadequate or the ground is not shared. If you are powering the servo from the Arduino 5V pin, try an external 5V supply. Make sure the external ground and Arduino ground are connected.

The Servo Moves to One Side and Stays There

Check the signal wire. If it is connected to the wrong pin, or if the code attaches to a different pin than the one you wired, the servo will not receive valid commands. Also confirm that you are using a pin supported by the Servo library.

The Arduino Resets When the Servo Starts

This is a classic brownout symptom. The servo draws a sudden current spike, the voltage drops, and the Arduino resets. Use a separate power supply for the servo, add a large capacitor across the servo power and ground, or both.

The Servo Buzzes at the Ends of the Sweep

Many inexpensive micro servos cannot quite reach 0 or 180 degrees without straining. If you hear buzzing at the extremes, reduce the range slightly. For example, sweep from 5 to 175 degrees instead of 0 to 180.

Expanding the Project

Once you have the basic sweep working, you can turn it into something more interesting.

Add a Potentiometer for Manual Control

Connect a potentiometer to an analog pin, read its value, and map that value to 0 through 180 degrees. Then write that angle to the servo. This gives you a simple manual pan control.

Use a Button to Trigger a Sweep

Instead of sweeping forever, you can make the servo perform one sweep when a button is pressed. This is useful for a waving hand, a gate that opens once, or a pointer that moves to a target and returns.

Control Multiple Servos

You can create multiple Servo objects and attach them to different pins. On an Arduino Uno, the library can handle up to 12 servos, but powering them all from the board is not realistic. Use a separate 5V supply with enough current for all of them.

Combine with Sensors

A micro servo becomes much more useful when it responds to the world. Add an ultrasonic sensor and sweep it back and forth to create a radar-like scanner. Add a light sensor and move a small solar panel toward the brightest direction. Add a temperature sensor and open a vent when things get hot. The sweep is just the beginning.

Final Thoughts on the Micro Servo Sweep

The micro servo motor is a remarkable little device. It brings motion to projects that would otherwise be static, and it does so with a simplicity that makes it accessible to beginners. The Arduino servo sweep program is the perfect introduction to that world. It teaches you how to include a library, how to wire an actuator, how to think about power, and how to create smooth motion with just a few loops.

Once you understand the sweep, you have a foundation for robotics, animatronics, automation, and countless other projects. The code is short, the wiring is simple, and the result is immediately visible. That combination is exactly why the micro servo remains one of the most popular components in the Arduino ecosystem, and why the sweep program is still one of the best first projects you can build.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/how-to-connect-a-micro-servo-motor-to-arduino/simple-servo-sweep-arduino.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags