Servo guides · On this page

Turn a Servo with a Knob on an Arduino

Turn a servo with a knob on an Arduino, running in your browser before you build it. A hobby servo turns its horn to the angle you ask for, about 0 to 180°, and holds it there. It takes three wires, for power, ground and a signal of pulses whose length sets the angle. One small servo runs from the Uno's 5V, but bigger ones, or several, need a supply of their own.

Left to right: a small translucent blue micro servo with a white single-arm horn, a much larger black standard servo with a round white horn, and a small black continuous-rotation servo with a round hub; each has a brown, red and orange lead ending in a black three-socket connector.
Micro, like the SG909 g: this guideStandard, like the MG996R55 g: give it its own supplyContinuous rotationSpins; check the listing

Try it first, no parts needed

This is the whole project, wired and running in your browser: a knob on A0 sets the angle of a servo on pin 9. Press Run simulation, then turn the knob and watch the horn follow it.

Ready
Open in Cirkit Designer

The knob on A0

Turning the knob from end to end swings the servo from 0° to 180°.

50 %

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch reads the knob, turns its position into an angle from 0 to 180°, and turns the servo to it.

servo_knob.ino
// Turn the knob on A0: the servo on pin 9 follows it,
// from 0 to 180 degrees.
#include <Servo.h>

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

Servo servo;
int shownAngle = -1;

void setup() {
  Serial.begin(115200);
  // Pulses from 544 us (0 degrees) to 2400 us (180).
  servo.attach(SERVO_PIN);
}

void loop() {
  int reading = analogRead(KNOB_PIN);  // 0 to 1023
  int angle = map(reading, 0, 1023, 0, 180);
  servo.write(angle);

  // Print only when the angle changes.
  if (angle != shownAngle) {
    shownAngle = angle;
    Serial.print(F("A0="));
    Serial.print(reading);
    Serial.print(F(" angle="));
    Serial.println(angle);
  }
  delay(15);  // give the servo time to get there
}
  1. Servo servo;

    A Servo object, one for each servo; it remembers the pin and the angle.

  2. servo.attach(SERVO_PIN);

    Starts the pulses on pin 9: one every 20 ms, from 544 µs for 0° to 2400 µs for 180°, until the sketch asks for another angle.

  3. int angle = map(reading, 0, 1023, 0, 180);

    Turns the knob's reading, 0 to 1023, into an angle from 0 to 180°.

  4. servo.write(angle);

    Sets the pulse length for that angle. The servo turns there and holds it; the sketch carries on at once.

  5. delay(15);  // give the servo time to get there

    Waits 15 ms before the next reading, as Arduino's own Knob example does. The servo keeps turning while the sketch reads on; a full 180° turn takes about 0.3 s.

Build it on an Arduino

Now with real parts. Check which wire is which, wire it, get the code, then upload it and turn the knob. Using an ESP32 board? It has its own guide →

Which wire is which: the servo pinout

A servo has one lead with three wires, and on almost every servo power is the middle one. The colours vary between makers: brown, red and orange, as here, or black, red and white. The lead ends in a three-socket connector with 0.1 in (2.54 mm) spacing, so jumper wires plug straight into it.

Servo pinout: one three-wire lead, power in the middle. 1 Ground, brown, shared with the board; 2 Power, red, 4.8 to 6 V, to 5V on an Uno for one small servo; 3 Signal, orange, pulses from the board, to pin 9 on an Uno. The wire colours vary between makers; brown, red and orange is common.
TurnsTo an angle and holds it: about 0 to 180°, depending on the servo
SignalA pulse every 20 ms; its length sets the angle, 1.5 ms is the usual neutral position
Supply4.8 to 6 V; one small servo can run from the Uno’s 5V
SG90 speed0.1 s per 60° at 4.8 V
SG90 stall torque1.8 kg·cm at 4.8 V
LibraryServo, which comes with the Arduino IDE

You'll need

  • An Arduino Uno (a Nano works the same way) and its USB cable
  • A micro servo, such as the SG90
  • A 10 kΩ potentiometer, the knob
  • A breadboard and six jumper wires; three of them plug into the servo's connector
  • Software to upload the sketch to the Uno: either Cirkit Designer, free in your browser, or the Arduino IDE, a free download for Windows, macOS and Linux
  1. Wire it

    Three wires for the servo, three for the knob.

    Tap a connection to see it on the circuit.

    PinConnect to the UnoShow
    Servo ground (brown)GNDShow
    Servo power (red, middle)5VShow
    Servo signal (orange)Pin 9Show
    Knob, one outer leg5VShow
    Knob, middle legA0Show
    Knob, other outer legGNDShow

    One small servo can run from the Uno's 5V. For a bigger servo, or more than one, power them from a separate 5 to 6 V supply and join its ground to the Uno's GND.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch already in it, so there's nothing to install.

    No Arduino IDE yet? Download it free for Windows, macOS or Linux.

    The Servo library comes with the Arduino IDE, so there's nothing to install. Copy the sketch into a new one:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    Servo on an Arduino UnoFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 3420 bytes (10%) of program storage space. Maximum is 32256 bytes.
    Global variables use 231 bytes (11%) of dynamic memory, leaving 1817 bytes for local variables. Maximum is 2048 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    A0=512 angle=90
    A0=257 angle=45
    A0=1023 angle=180
    A0=0 angle=0
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    servo_knob.ino
    // Turn the knob on A0: the servo on pin 9 follows it,
    // from 0 to 180 degrees.
    #include <Servo.h>
    
    const int KNOB_PIN = A0;
    const int SERVO_PIN = 9;
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 3420 bytes (10%) of program storage space. Maximum is 32256 bytes.

    Global variables use 231 bytes (11%) of dynamic memory, leaving 1817 bytes for local variables. Maximum is 2048 bytes.

    Uploading...

    A0=512 angle=90

    A0=257 angle=45

    A0=1023 angle=180

    A0=0 angle=0

    Done uploading.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • The horn follows the knob, from one end of its travel to the other.
    • The Serial Monitor prints a line each time the angle changes, such as A0=257 angle=45 with the knob a quarter of the way round, as pictured. On real hardware the readings vary a little.
    The servo standing on end as it sits in the circuit, its horn turned 45 degrees: halfway from pointing right (0 degrees) to pointing down (90 degrees).
    With the knob a quarter of the way round, the sketch writes 45° and the horn turns there.

    Not working? See troubleshooting.

    Next guideThe same servo on an ESP32ESP32Servo, the ESP32's pins, and powering a servo from a 3.3 V board.

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

The servo doesn't move, but the Serial Monitor shows readings

The sketch runs, but its pulses don't reach the servo, or the servo has no power. Check, in order:

  • The signal wire is on pin 9. It must be the pin servo.attach() names.
  • The servo is wired by its colours. Power (red, the middle wire) to 5V, ground (brown or black) to GND.
  • With a separate supply, the grounds are joined. The pulses mean nothing to the servo unless its ground is the Uno's ground.

The servo jitters, or the Uno resets when the servo moves

The servo is drawing more current than its supply can give, so the supply's voltage dips.

  • Only one small servo runs from the Uno's 5V. A micro servo can draw a few hundred milliamps while it works hard; a standard servo such as the MG996R up to 2.5 A at 6 V. Give anything bigger, or a second servo, its own 5 to 6 V supply, with the grounds joined.
  • The USB port or cable is up to it. A hub or a long, thin cable sags under load; try a port on the computer itself.

The servo buzzes or strains at one end

  • The pulse range suits your servo. The Servo library's 544 to 2400 µs can push a servo against its own end stops. Narrow it with servo.attach(9, 1000, 2000), then widen it until the horn reaches each end without straining.
  • Nothing blocks the horn. A servo that can't reach its angle keeps pushing, and draws its most current doing so.

The horn turns the opposite way to the knob

  • The knob's outer legs are the other way round. Swap them, or reverse the angle in the code: servo.write(180 - angle).

The servo spins round and round instead of turning to an angle

  • It is a continuous-rotation servo. It looks like a positional one, but the pulse length sets its speed and direction, not an angle; this sketch needs a positional servo. See servo or something else.

analogWrite() stopped working on pin 9 or 10

  • The Servo library uses them. On an Uno, attaching any servo turns off analogWrite() on pins 9 and 10, whether or not a servo is on them. Use another PWM pin, such as 3, 5, 6 or 11, for the LED or motor.

Nothing in the Serial Monitor, or garbled characters

  • The Serial Monitor is set to 115200 baud. It must match Serial.begin(115200) in the sketch.
  • The sketch prints only when the angle changes. It prints once at the start; turn the knob to see more.

How a servo works

Inside the case are a small motor, a gearbox that slows it down and makes it stronger, and a potentiometer on the output shaft that tells a little control board where the shaft is. The board compares that with the angle the pulses ask for and drives the motor until they match.

Inside a servo: the board's pulses go to a small control board. It drives a motor, which turns the output shaft and horn through a gearbox. A potentiometer on the output shaft tells the control board where the shaft is, and the board drives the motor until that matches the angle the pulses ask for, then stops.

The pulses come about 50 times a second, every 20 ms with the Servo library, and their length is the whole message: 1.5 ms is the usual neutral position, and shorter pulses turn it one way, longer ones the other. Below is the signal this guide's sketch sends.

The servo's signal, pulse by pulseExample: write(90)Run the circuit above, and this shows the pulses your sketch sends and where the horn goes.

Every 20 ms, one pulse

0 ms20 ms40 ms

One pulse, zoomed in: its length sets the angle

1472 µs
544 µs: 0°1472 µs: 90°2400 µs: 180°

Horn at 90°, pulse 1472 µs

The Servo library sends 544 µs for 0° and 2400 µs for 180° unless you give attach() other limits; servos differ, so your servo may need a narrower or wider range for its full travel.

servo.write(angle) turns an angle into a pulse length within the range attach() set; servo.writeMicroseconds(1500) sends a pulse length directly. And servo.read() returns the last angle you wrote, not where the horn is: the servo has no wire to say where it got to.

Speed, strength and other limits

A micro servo such as the SG90, on an Uno
TravelAbout 0 to 180°; each servo's end points differ, so the exact pulse lengths for its ends do too.
Speed0.1 s per 60° at 4.8 V, so about 0.3 s for a 180° turn.
StrengthStall torque 1.8 kg·cm at 4.8 V: the most it can push before it stops, the same as 1.8 kg at 1 cm from the shaft or 0.18 kg at 10 cm. Loads that heavy stall it.
CurrentStalled, a micro servo draws a few hundred milliamps. Idle, with nothing to push against, a servo draws from a few dozen to one or two hundred milliamps; holding a load takes more, up to its stall current. The SG90's datasheet gives no current.
Smallest stepThe SG90 ignores a change of less than 10 µs in the pulse, its dead band: about 1° with the library's default range.
PositionThe sketch can't read where the horn is; read() gives back the last angle written.
How manyUp to 12 servos on an Uno. Attaching a servo turns off analogWrite() on pins 9 and 10.

For loads near its limit, use a bigger servo with its own supply. To move more slowly than the servo's top speed, step the angle a degree at a time:

A slow sweep
// Move one degree at a time, 15 ms apart: about 2.7 s
// from 0 to 180 degrees.
for (int angle = 0; angle <= 180; angle++) {
  servo.write(angle);
  delay(15);
}

Servo or something else?

MovesStrength and speedPowerChoose it for
Micro servo, such as the SG90To an angle, about 0 to 180°, and holds itStall torque 1.8 kg·cm, 0.1 s per 60° at 4.8 V; 9 gOne runs from the Uno's 5VPointers, flaps and small arms: this guide
Standard servo, such as the MG996RTo an angle, about 120° by its datasheetStall torque 9.4 kg·cm, 0.17 s per 60° at 4.8 V; 55 g4.8 to 7.2 V, up to 2.5 A when stalled at 6 V: its own supplyHeavier loads, such as a robot arm's joints
Continuous-rotation servoRound and round; the pulse length sets speed and directionAs a positional servo of its sizeAs a positional servo of its sizeSmall wheels, with no extra driver board
Stepper motorIn fixed steps, as many turns as you likeDepends on the motor and driverIts own supply, through a driver boardExact positions beyond one turn, such as a dial or a slide

A continuous-rotation servo looks just like a positional one; the listing says which. It can't turn to an angle. A pulse of 1.5 ms stops it when it is calibrated to; many have a small trimmer for that. Pulses further from 1.5 ms make it spin faster, shorter one way and longer the other. Stop it with writeMicroseconds(1500): with the library's default range, write(90) sends 1472 µs, which may leave it creeping.

Servo FAQ

Can I run a servo from the Uno's 5V pin?

One small servo, yes. For a second servo, or a big one, use a separate supply, with its ground joined to the Uno's GND; the Servo library's own notes say the same for more than one or two. See jitter and resets.

How do I control two servos?

Make a Servo object for each and attach each to its own pin: left.attach(9), right.attach(10). An Uno can run up to 12; give them their own supply.

Why does my servo buzz at 0° or 180°?

The library's default pulses can ask for more travel than your servo has, so it pushes against its end stop. Give attach() a narrower range; see buzzing at one end.

Which servo wire is which?

Power is the middle wire, usually red. Ground is brown or black, and signal is orange, white or blue. See the pinout.

Can a servo turn 360°?

An ordinary hobby servo turns about 180° at most; its potentiometer and end stops limit it. Multi-turn servos, a rarer kind, go to positions over one or more full turns. A continuous-rotation servo turns round and round, but can't go to an angle. See servo or something else.

What's the difference between write() and writeMicroseconds()?

write() takes an angle and turns it into a pulse length; writeMicroseconds() takes the pulse length itself, for finer control or a servo whose range you've measured.

Simulating the servo in Cirkit Designer

Cirkit's servo is a generic positional servo. It measures each pulse on its signal pin, as a real servo's control board does, and turns its horn to the matching angle, so the same code runs on hardware. It moves only with 4 to 6 V between its power and ground pins, the simulator's window for a small servo. It is tested on the Arduino Uno, the ESP32-S3 and the classic ESP32.

Where it differs from a real servo: the horn reaches its angle at once, with no speed, torque, load or current, and no end stops. On real hardware, give the servo time to move, and its own supply for anything bigger than a micro servo.

References