Arduino

Controlling a Servo Motor with Arduino

DIY Daily 5 min read

Servos are the easiest way to add precise motion to an Arduino project. Unlike a plain DC motor that just spins, a hobby servo rotates to an exact angle — typically anywhere from 0° to 180° — and holds it. Steering in RC cars, pan-tilt camera mounts, robotic arms and automatic pet feeders all run on these little gearboxes. In this tutorial you will wire an SG90 servo safely, sweep it with the built-in Servo library, and then control the angle with a potentiometer.

How a Servo Understands the Arduino

A servo expects a pulse on its signal wire 50 times per second (every 20 ms). The pulse width encodes the position: roughly 1 ms means 0°, 1.5 ms means 90°, and 2 ms means 180°. Inside the servo, a small circuit compares that command against the actual shaft position measured by a built-in potentiometer and drives the motor until they match. This closed loop is why servos hold position against light loads. The Arduino’s Servo library generates these precisely timed pulses in the background — you simply call myServo.write(90) and forget the details.

Parts Needed

  • Arduino Uno with USB cable
  • 1× SG90 (or similar 5 V micro) servo
  • 1× 10 kΩ potentiometer
  • Breadboard and jumper wires
  • Optional: 4×AA battery pack for powering larger servos

Wiring the Servo and Potentiometer

Micro servos like the SG90 can run from the Uno’s 5 V pin for light loads, but anything beefier should get its own supply. If you use external power, connect the supply ground to Arduino GND — without a common ground the signal wire means nothing.

Servo Wire Arduino Uno Notes
Brown/black (GND) GND Must be common with any external supply
Red (VCC) 5V 4.8–6 V; external pack for big servos
Orange/yellow (signal) Digital pin 9 Any pin works with the Servo library
Pot outer pins 5V and GND Order just reverses the direction
Pot wiper (center) Analog pin A0 Reads 0–1023 on analogRead

Knob-Controlled Servo Sketch

#include <Servo.h>

const int SERVO_PIN = 9;
const int KNOB_PIN  = A0;

Servo myServo;

void setup() {
  myServo.attach(SERVO_PIN); // Start generating 50 Hz pulses.
  Serial.begin(9600);
}

void loop() {
  int raw   = analogRead(KNOB_PIN);     // 0..1023 from the pot.
  int angle = map(raw, 0, 1023, 0, 180); // Scale to degrees.

  myServo.write(angle);
  Serial.println(angle);

  delay(15); // Let the servo keep up; 50 Hz update rate.
}

Walkthrough

attach() claims the pin and starts the pulse train; analogRead() converts the potentiometer’s wiper voltage into a number between 0 and 1023; map() rescales that to 0–180 degrees; write() commands the new position. Turn the knob and the horn follows smoothly. Want an automatic demo instead? Replace the loop body with a for loop that writes 0→180 and back with a 15 ms delay between steps — the classic sweep.

Going Further

Two servos plus a joystick make a pan-tilt camera mount. The Servo library can drive up to 12 servos on an Uno. If you need finer control than whole degrees, use writeMicroseconds(700–2300) to set the pulse width directly — but test the limits gently first, because forcing the horn past its mechanical stops stalls the motor, spikes the current and cooks the gears over time.

Try It in the Circuit Simulator

The analog side of this build — the potentiometer acting as a voltage divider — can be explored in our Circuit Simulator. Drag in a potentiometer and watch how the wiper voltage sweeps between the rails as you turn it; that voltage is exactly what A0 sees.

Powering Multiple Servos Properly

One SG90 idles near 10 mA but pulls 100–250 mA while moving and 650 mA or more when stalled. Four servos moving together can demand an amp — far beyond the Uno’s 5 V regulator or a USB port’s comfortable margin. The standard solution is a separate 5–6 V supply (a 4×AA holder or a 5 V 3 A adapter) wired to the servos’ red wires only, with all grounds joined: battery negative, servo browns, and Arduino GND together in one node. Add a 470–1000 µF capacitor across that rail to swallow startup surges, and your “random resets” disappear. Signal wires still come from Arduino pins — only the power changes address.

Troubleshooting

  • Servo jitters or twitches: usually a power problem — the Uno’s 5 V rail is sagging. Add external power with common ground, or a 470 µF capacitor across the servo supply pins.
  • Board resets when the servo moves: classic brownout; the stall current (650 mA+ on an SG90) is dragging USB power down. External supply required.
  • Servo does nothing: confirm the signal wire is on pin 9, you called attach(), and the brown wire is really on GND.
  • Buzzing at the extremes: your servo cannot reach exactly 0° or 180°; limit writes to 10–170° or tune with writeMicroseconds.

Safety Notes

  • Never force the horn by hand while powered; the gearbox is plastic and strips easily.
  • Keep fingers, hair and cables clear of the horn’s arc — servos snap to position faster than you can react.
  • Always share ground between external supplies and the Arduino, but never connect two positive supply rails together.

Conclusion

You have added precise motion to your toolbox: one signal pin, one library call, exact angles. Servos plus the ultrasonic sensor from the previous tutorial give you a scanning rangefinder; servos plus a button give you a uselessly delightful machine that turns itself off. Motion is where projects start feeling alive.

#arduino #motor #servo

DIY Daily

Electronics tinkerer and tutorial writer at DIY Daily.

Leave a Comment

Your email address will not be published. Required fields are marked *