# Arduino project: control a servo motor with a potentiometer

> Learn how to control a servo motor with an Arduino and a potentiometer, mapping the analog 0 to 1023 reading to a 0 to 180 angle with the Servo library.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2021-04-01 | Topics: [Arduino](https://flaviocopes.com/tags/electronics/) | Canonical: https://flaviocopes.com/arduino-project-servo-motor/

In this project we'll see how to drive a servo motor using a potentiometer.

Using an analog input pin we can read the rotation of the potentiometer, with values ranging from 0 to 1023.

We'll use those values to rotate the servo motor from 0° to 180°.

First we create the circuit, then we'll write the program.

Connect the power pins `5V` and `GND` to the `+` and `-` breadboard lines:

![Arduino Uno board connected to breadboard with red and black power wires](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3570.jpg)

Then pick those signals and connect them to the input pins of the potentiometer:

![Potentiometer inserted on breadboard with red and black wires connected to power rails](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3572.jpg)

Connect the output pin to the analog I/O pin `A0`:

![Yellow wire connected from Arduino analog pin A0 to breadboard](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3578.jpg)

![Complete breadboard setup showing potentiometer wired with power and output connections](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3577.jpg)

Next, connect the servo motor. Connect the brown wire to 0V, the red one to 5V, the orange one to the pin `9`:

![Arduino circuit with servo motor connected showing brown, red and orange wires to servo](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3580.jpg)

![Close-up view of servo motor connections with potentiometer on breadboard](https://flaviocopes.com/images/arduino-project-servo-motor/IMG_3579.jpg)

Now let's switch to the Arduino IDE to write the program.

We need to acquire the input of the potentiometer first:

```c
void setup() {
    Serial.begin(9600);
}

void loop() {
    int value = analogRead(A0);
    Serial.println(value);
}
```

Upload this program to the Arduino and you should see an output from 0 to 1023 printed in the serial monitor.

Now we need to remap this 0-1023 set of values to 0-180, the ones we're going to feed to the servo motor.

We do so using the `map()` function:

```c
value = map(value, 0, 1023, 0, 180);
```

Now we're going to use a library.

In the Arduino IDE `Sketch` menu, select `Include Library` and pick `Servo`:

![Arduino IDE Sketch menu showing Include Library submenu with Servo library highlighted](https://flaviocopes.com/images/arduino-project-servo-motor/Screen_Shot_2020-12-15_at_18.08.17.png)

This will add a `#include <Servo.h>` line on top of the file.

The Servo library is a built-in library, and to have more information on this library you can open the menu `Tools` and `Manage libraries...`.

This will open the **Library manager**:

![Arduino IDE Library Manager window showing available Arduino libraries](https://flaviocopes.com/images/arduino-project-servo-motor/Screen_Shot_2020-12-15_at_18.10.35.png)

Search "servo" in the box, and you should see it show up:

![Library Manager search results showing Servo library by Arduino with install options](https://flaviocopes.com/images/arduino-project-servo-motor/Screen_Shot_2020-12-15_at_18.10.59.png)

Click the "More info" link, this will open the page <https://www.arduino.cc/reference/en/libraries/servo/> in your browser.

![Arduino.cc Servo library reference page showing library documentation and compatibility](https://flaviocopes.com/images/arduino-project-servo-motor/Screen_Shot_2020-12-15_at_18.12.08.png)

This is the way to get information about any library, not just this one, and it's useful to know where you can get more info.

The page lists some usage information, the methods exposed by the library, and some examples too:

![Arduino Servo library usage documentation showing methods and examples](https://flaviocopes.com/images/arduino-project-servo-motor/Screen_Shot_2020-12-15_at_18.12.49.png)

The nice thing about Arduino is that it's all open source. In this case you can find the library source code at <https://github.com/arduino-libraries/Servo>, so you can write your own modified version or just understand how it works under the hood.

We're going to use 2 methods: [`attach()`](https://www.arduino.cc/reference/en/libraries/servo/attach/) and [`write()`](https://www.arduino.cc/reference/en/libraries/servo/write/).

With `attach()` we tell Servo which port to use.

With `write()` we move the servo motor to the desired angle, specified as parameter, from 0 to 180.

But first we need to declare a Servo object. We do so before the `setup()` function, with `Servo myservo;`:

```c
#include <Servo.h>
Servo myservo;
```

Then in `setup()` we attach the servo to the I/O pin 9:

```c
#include <Servo.h>
Servo myservo;

void setup() {
    myservo.attach(9);
}
```

Finally in `loop()` we call `myservo.write()` passing a value from 0 to 180:

```c
#include <Servo.h>
Servo myservo;

void setup() {
    myservo.attach(9);
}

void loop() {
    int value = analogRead(A0);
    value = map(value, 0, 1023, 0, 180);
    myservo.write(value);
}
```
