• EUR€
  • £GBP
  • $USD

Arduino Project 11: Controllable Servo

DFRobot Jun 05 2017 2336

Related Product: Beginner Kit for Arduino

We've learned to turn the servo to a specific an gleusing an external signal. In the new arduino project, we will use a potentiometer in this arduino starter kit to control a servo. You can also modify this circuit by swapping the potentiometer for a sensor such as a tilt switch, or changing the actuator to an LED. Get tinkering and use your imagination!


DFRduino UNO R3 (similar as Arduino UNO R3) *1

Prototype Shield *1
Jumber Cables M/M *3
Jumber Cables F/M *3
10K Potentiometer *1

Servo *1


This is a little different from our previous project as we are using a potentiometer. Potentiometers are sometimes called variable resistors, and they are just that. By turning the knob, you are altering the electrical resistance of the component. It has 3 pins: two side by side and one on top. Connect the side by side pins to 5V and GND on the Arduino respectively, and the single pin on the opposite side of the potentiometer to the analog 0 pin on the Arduino.


Sample code 11-1
//Project eleven controllable servo
#include <Servo.h>          // insert the Servo.h library
Servo myservo;                // create servo object to control servo
int potpin = 0;                  // connect potentiometer to digital pin0
int val;                               // variable value to read value from analog pin

void setup() {
myservo.attach(9);               //Attach the servo on pin 9 to the servo object.

void loop() {
val = analogRead(potpin);                    //eads the value of the potentiommeter
(value between 0 and 1023)
val = map(val, 0, 1023, 0, 179);           // scale it to use it with the servo (value
between 0 and 180)
myservo.write(val);                              // sets the servo position according to the
scaled value
delay(15);                                               // wait for 15 ms to turn to certain position

After uploading the sketch, you can see the servo turn according to the position of the potentiometer.


We declare to insert the <Servo.h> library first and then define the potentiometer on Analog pin 0 to read its value.
Now let’s dig into the “map” function. The format of map function is as below:

map(value, fromLow, fromHigh,
toLow, toHigh)

The “map” function re-maps a number from one range to another. That is, a value of “fromLow” would get mapped to “toLow”, a value of “fromHigh” to “toHigh”, values in-between to values in-between, etc.


value: the number to map
fromLow: the lower bound of the value's current range fromHigh: the upper bound of the value's current range

toLow: the lower bound of the value's target range
toHigh: the upper bound of the value's target range
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the “map()” function may be used to reverse a range of numbers,
y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that this example is also valid and works well:
y = map(x, 1, 50, 50, -100);
val = map(val, 0, 1023, 0, 179);

Back to our sketch: we map the analog value from 0 to 1023 to a servo value from 0 to 180.



A potentiometer is a simple knob that provides a variable resistance ranging from 0-10, which Arduino can read as an analog value. In this projeect, we connected three wires to the Arduino. The first went to ground from one of the outer pins of the potentiometer. The second went from 5 volts to the other outer pin of the potentiometer. The third went from analog input 2 to the middle pin of the potentiometer.

A potentiometer works in a similar way to the voltage divider in project 9. The potentiometer is divided in to 2 resistors by the shaft. By turning the shaft of the potentiometer, we change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction to the upper limit, there are 5 volts going to the pin, and we read 1023. In between, “analogRead()” returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

Related category: arduino kits > education kits

Last Arduino Tutorial 10: How to Drive A Servo
Next Arduino Tutorial 12: Interactive Adjustable RGB LED