IoTSimulator

Servo: How It Works, Wiring, and Example Code

A micro servo motor used for position control in Arduino projects.
M
Muhammad Ichsan
Servo

This article is a guide about the Servo. We will explain how it works, show how to wire it to an Arduino, and walk through a simple sweep example you can reuse in your own projects.

Servos are useful when a project needs controlled movement rather than simple on/off behavior.

Description

Servo product view
Servo product view

A servo motor is a small actuator that moves to a chosen angle. Instead of spinning forever like a DC motor, it usually aims for a position between 0 and 180 degrees and then holds that position.

That makes it ideal for projects that need accurate movement, such as a pointer, a door latch, a robot joint, or a camera mount. The Arduino sends a position command, and the servo converts that command into a visible physical motion.

Features

Here are the main things to know about the Servo:

FeatureWhat it means
Position controlThe servo moves to a target angle instead of spinning freely.
Simple command styleThe Arduino only needs one signal pin and the Servo library.
Three-wire connectionMost servos use power, ground, and one control line.
Useful rangeTypical sweep range is about 0 to 180 degrees.
Interactive motionGreat for arms, levers, pointers, and small mechanisms.
Holding torqueCan stay at a chosen angle instead of relaxing immediately.

The important part is that the response is visible and predictable. Press a button, move a slider, or change a sensor value, and the servo reacts in a way the user can actually see.

How Does It Work?

How a servo moves to an angle

A servo receives a timed control pulse from the Arduino. Inside the servo, a small controller reads that pulse and moves the motor until the output shaft reaches the requested angle. That is why a servo can hold a position instead of just free-spinning.

In a beginner project, the important idea is simple: the angle you write becomes the angle you see. If the sketch asks for 90 degrees, the horn moves near the middle. If it asks for 0 or 180, the horn moves toward the ends.

The servo is not reading a value like an analog sensor. It is following a position command, which makes it ideal for doors, pointers, robot joints, and any project that needs controlled movement.

Arduino With Servo

This circuit preview shows the servo connected to Arduino Uno with one control line, power, and ground. It is the standard setup for learning servo motion because the wiring is simple and the movement is easy to see.

SCL
SDA
AREF
GND.1
D13
D12
D11
D10
D9
D8
D7
D6
D5
D4
D3
D2
D1
D0
IOREF
RESET
3V3
5V
GND.2
GND.3
VIN
A0
A1
A2
A3
A4
A5
GND
V+
PWM

Pin Connection

The pin map below matches the circuit preview. One signal pin tells the servo where to move, while power and ground keep the motor running safely.

Pin Connection Map
Servo Pin
PWM
Arduino Pin
D9
Explanation

Carries the control signal that sets the servo angle.

Servo Pin
V+
Arduino Pin
5V
Explanation

Provides power for the servo motor.

Servo Pin
GND
Arduino Pin
GND
Explanation

Shares the ground reference with the Arduino.

For small servos in a simple demo, the Arduino 5V pin may be enough. For larger or real-world servo setups, an external supply is often better, as long as the grounds are shared.

Code

This example sweeps the servo back and forth between a few angles. It is a classic first test because it makes the movement easy to see and confirms that the wiring is correct.

C++ Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Once the servo responds to that simple sweep, you can replace the fixed angles with sensor readings, button presses, or a menu control. That is where servos become useful in real projects instead of just demo motion.

How The Code Works, Part By Part

Let's break the sketch into smaller pieces so the flow is easier to understand and easier to modify later.

Setup

The setup block includes the Servo library and attaches the servo to pin 9. This is where the Arduino tells the library which output pin will carry the motion command.

C++ Source
1
2
3
4
5
6
7

Write An Angle

This part sends the position command. When the sketch writes 0, 90, or 180, the servo moves the horn toward that angle.

C++ Source
1
2
3
4
5
6

Repeat

The loop keeps the movement pattern running so you can clearly see that the servo is responding to code. In a real project, you would usually replace the repeating sweep with logic based on input or state.

C++ Source
1
2
3
4
5
6
7
8

Wrapping Up

The Servo is a great way to turn Arduino code into controlled movement. It is easy to wire, easy to test, and easy to reuse in robots, pointers, small arms, and other interactive projects. It gives you a direct link between code and motion, so debugging is usually straightforward.

Once you understand how the angle command works, you can make the servo respond to almost any sensor or button in your project. That makes it one of the most practical beginner actuators to learn early.

New

Wait! We're building more...

Our laboratory is currently preparing a lot of exciting new projects using Servo. Stay tuned for the upcoming massive update!

Muhammad Ichsanul Fadhil
About Writer

Muhammad Ichsanul Fadhil

"I'm a developer and hardware enthusiast with a passion for IoT. I love experimenting with new components and writing down everything I learn to help others build their own projects."

Share this article

Share it with your favorite channel.