Illustration, from above: a hand turns the potentiometer, with the servo and the board beside it on a cutting mat.Robotics
Illustration (made with AI): roughly what the finished project looks like.

Control a Servo with a Knob

Turning a potentiometer swings a servo from 0 to 180 degrees. This is how a robot arm, or any other moving part, works.

Difficulty
Beginner
Time
30 min
Age
10+
Parts
5, 5 on the site

Translated from “Servo Motor Basics with Arduino” by Arduino. Licence: CC BY-SA 4.0. This translation is shared under the same licence. The parts list and the tips are RoboHub's.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Connect the servo

    The servo's brown wire goes to ground, red to 5V and yellow (or orange) to pin 9. If it sits far away, use an extension cable.

    D9 is the board's digital pin 9, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • SG90 servo SIG → Arduino D9
    • SG90 servo + → Arduino 5V
    • SG90 servo − → Arduino GND
  2. The potentiometer

    The outer legs go to 5V and ground, the middle leg to A0.

    D9 is the board's digital pin 9, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Potentiometer 1 → Arduino 5V
    • Potentiometer 2 → Arduino A0
    • Potentiometer 3 → Arduino GND
  3. Upload the code

    This is Arduino's well-known Knob example.

    Arduino C++
    #include <Servo.h>
    
    Servo servo;
    const int KNOB = A0;
    
    void setup() {
      servo.attach(9);
    }
    
    void loop() {
      int value = analogRead(KNOB);             // 0-1023
      int angle = map(value, 0, 1023, 0, 180);  // 0-180 degrees
      servo.write(angle);
      delay(15);
    }
  4. Try it

    Turn the knob and the servo follows. If the servo jitters, give it its own 5 V supply.

    Illustration: a potentiometer on a breadboard and a servo, both wired to the board.

    Illustration, made with AI

  • Sensors
    Beginner1 h10+

    Parking Sensor with Sound and Lights

    An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

    6/6 parts on the site
  • Radio control
    Intermediate1 h12+

    Control an Arduino with a FlySky Transmitter

    The FS-iA6B receiver passes every channel of the transmitter to the Arduino over a single wire (i-BUS). That way the transmitter can drive servos, lights or your own robot.

    6/6 parts on the site
  • Lights and displays
    Beginner30 min8+

    A Colour-Changing Lamp with an RGB LED

    An RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.

    5/5 parts on the site