Controlling a small motor with precision sounds like something from a robotics lab, but it's one of the most satisfying beginner projects you can build with an Arduino board. A servo motor doesn't just spin it moves to exact angles you tell it to, which opens the door to robotic arms, automated camera sliders, steering mechanisms, and all sorts of creative builds. If you've already gotten comfortable with an LED blinking project, wiring up and coding a servo is a natural next step that teaches you about PWM signals, timing, and physical computing.
What does Arduino servo motor code actually do?
A servo motor expects a specific type of electrical signal a PWM (Pulse Width Modulation) pulse that tells it where to position its shaft. Writing Arduino servo motor code means using the built-in Servo.h library to generate those pulses automatically. You call a function, set an angle between 0 and 180 degrees, and the motor moves there. Under the hood, the library handles the timing details so you don't have to manually manage pulse widths.
This is different from a regular DC motor, which just spins when you give it power. A servo has internal feedback circuitry that holds its position. That's why servos are used wherever you need controlled, repeatable motion not just continuous spinning.
What parts do you need to get started?
You don't need much to build your first servo project. Here's the basic shopping list:
- An Arduino Uno (or Nano, Mega any standard board works)
- A SG90 micro servo motor (the cheap, widely available one)
- Jumper wires (three will do for one servo)
- A USB cable to program the Arduino
- The Arduino IDE installed on your computer
The SG90 is the most common starter servo. It's small, costs under two dollars, and can lift light loads like a small flag, a sensor, or a tiny pointer. If you need more torque, the MG996R is a stronger metal-gear option, but the wiring and code are identical.
How do you wire a servo motor to an Arduino?
A standard hobby servo has three wires:
- Red power (connect to 5V on the Arduino)
- Brown or Black ground (connect to GND)
- Orange, Yellow, or White signal (connect to a PWM-capable pin, like pin 9)
For a single SG90, you can power it directly from the Arduino's 5V pin. If you're running multiple servos or a larger motor, use an external 5V power supply instead. Servos under load can draw more current than the Arduino's regulator can safely provide, which leads to brownouts and erratic board resets.
How to write your first servo motor code step by step
Start by including the library and creating a servo object. Then attach it to a pin, and use the write() function to set angles. Here's the simplest working example:
Step 1: Include the library at the top of your sketch:
#include <Servo.h>
Step 2: Create a servo object:
Servo myServo;
Step 3: In setup(), attach the servo to a pin:
myServo.attach(9);
Step 4: In loop(), move it to different positions:
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
myServo.write(180);
delay(1000);
This sweeps the servo from 0° to 90° to 180°, pausing one second at each position, then repeating. Upload this to your board and you'll see the motor arm move back and forth.
How do you make the servo sweep smoothly instead of jumping?
The code above makes the servo jump between angles. If you want a smooth sweep like a radar scanner or a pan-and-tilt camera use a for loop to increment the angle one degree at a time:
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(15);
}
The delay(15) controls the speed. A shorter delay makes it faster; a longer one slows it down. Experiment with values between 10 and 30 milliseconds to find the motion that suits your project.
What are common mistakes in Arduino servo motor code?
These are problems I've seen trip up beginners again and again:
- Forgetting to call
attach()If you skipmyServo.attach(pin)in setup, nothing happens. The write commands are silently ignored. - Using the wrong pin Any digital pin works for servo signal on most Arduino boards, but some libraries or shields expect specific pins. Pin 9 and 10 are safe bets.
- Powering multiple servos from the Arduino 5V pin Two or more servos can draw enough current to crash your board. Use a separate 5V supply and connect the grounds together.
- Setting angles outside 0–180 Values below 0 or above 180 won't break anything, but the servo just clamps to its limit. If you need continuous rotation, that's a different type of servo and a different approach.
- Not adding
delay()or timing logic Without any pause, the loop runs so fast that the servo can't keep up. You'll hear buzzing but see no real movement.
How do you control a servo with a potentiometer?
This is one of the most popular beginner projects because it gives you physical, real-time control. Wire a potentiometer to an analog input pin (like A0), read its value with analogRead(), and map it to an angle:
int potValue = analogRead(A0);
int angle = map(potValue, 0, 1023, 0, 180);
myServo.write(angle);
The map() function converts the 0–1023 analog reading to the 0–180 degree range. Now when you turn the knob, the servo follows your hand in real time. This wiring and logic pattern shows up in many home automation builds where a sensor input drives a physical actuator.
Can you control more than one servo at the same time?
Yes. The Servo library supports up to 12 servos on most Arduino boards (and up to 48 on the Mega). Just create multiple objects:
Servo servo1;
Servo servo2;
Attach each to its own pin, and control them independently in your loop. The key thing to watch is power. Each SG90 can draw around 500mA under load. Two servos on the Arduino's 5V pin will probably cause issues. Use a dedicated power supply a 5V 2A adapter works well and share the ground with the Arduino.
What real projects use Arduino servo motor code?
Servo motors appear in a huge range of builds. Here are a few that beginners can actually complete in an afternoon:
- Pan-and-tilt bracket for an ultrasonic sensor mount two servos at 90° to each other and scan an area like radar
- Automated plant watering arm a servo rotates a valve or moves a water tube to different pots on a schedule
- Robotic hand or gripper multiple servos control finger segments to grab small objects
- Mailbox flag indicator a servo raises a physical flag when a sensor detects mail has arrived
- Camera slider a servo drives a carriage along a rail for time-lapse photography
You can make your project interface look polished by choosing the right display font for any LCD or OLED screen. If you need a clean monospace style for readouts, Segment7 is a popular option for digital-style number displays.
What tips help your servo project work reliably?
- Always add a small delay between write commands servos need time to physically reach the target position. 15–50ms between moves prevents jitter.
- Use
detach()when the servo doesn't need to hold position this stops the PWM signal, reduces heat, and saves power. - Calibrate your angles not every servo's 0° is the same. Test with 0, 90, and 180 and adjust your code if the mechanical range is off.
- Secure the servo body if the motor housing can rotate, the shaft won't move the way you expect. Hot glue, screws, or a proper bracket fix this.
- Read the datasheet for your specific servo pulse width ranges can vary slightly between manufacturers, and some servos have a wider or narrower angle range than 180°.
Where should you go from here?
Once you're comfortable with basic servo control, you can combine it with sensors, other motors, and communication modules. A logical next project is adding a second servo for a pan-tilt setup, or integrating an IR remote so you can control the angle wirelessly. Many of the same patterns reading inputs, mapping values, writing outputs carry over into larger Arduino projects for home automation and robotics.
Quick-start checklist:
- Wire the servo: red to 5V, brown/black to GND, signal to pin 9
- Open the Arduino IDE and include
Servo.h - Create a servo object and call
attach(9)in setup - Use
write()with angles between 0 and 180 - Add
delay()or aforloop for smooth movement - If using more than one servo, power them externally
- Upload, test, and adjust angles to fit your physical build
Step-By-Step Arduino Code Projects for Beginners
Arduino Led Blinking Project Maker Codes for Beginners
Smart Home Arduino Projects You Can Build Today
Arduino Sensor Projects for Stem Learning
Arduino Esp32 Sensor Data Publishing Mqtt Snippet
Zephyr Rtos Ble Peripheral Configuration Code Walkthrough