Here we will control the position of the servo with a potentiometer. This can be very useful for us in future projects (for example, controlling a robot manipulator). Unlike a conventional electric motor, a servo drive is a complex composite device consisting of a DC motor, gear reducer, potentiometer and electronic circuit. All this allows the servo to rotate the shaft strictly at a given angle, and hold it.

## 1. CONCEPTS

Potentiometer: is a simple electromechanical transducer which converts the rotary or linear movement into a change of resistance. As a result, you can use it to control anything from the volume of a hi-fi system to the direction of a huge container ship.

Servo Motor: is an electric motor which is fundamentally characterized by its extreme controllability: it allows precise adjustment of angular position, acceleration and speed. To achieve these results, in addition to the servomotor itself, a rotor position sensor is also part of the basic architecture of the device. This sensor also called rotary encoder or motor feedback system is able to accurately detect the position of the motor shaft at a given moment. Most of the servo motors available can rotate between 0 degrees to 180 degrees precisely.

### Mapping the Input and Output Values using slope line formula:

In Arduino, the input analog signal resolution is 10 bit which means 2^10=1024 values. Whereas the Servo motor can rotate between 0 to 180 degrees. Therefore to make it work properly we have to map the input values with output values. This can be done using the slope line formula. In Figure 3 below, you can see that on the y-axis are the output angle values which need to be fed to the servo in order to make it rotate between 0 to 180 degrees. Whereas on the x-axis we have potentiometer values between 0 to 1023.

Calculating slope of the line:

slope, m = (y2-y1)/(x2-x1) = (180-0)/(1023-0)

m = 0.176

Mapping potentiometer values with angle:

Now, Point slope form of a line

y-y1 = m (x – x1)

or, angle – 0 = 0.176 (potValue – 0)

therefore, angle = 0.176*potValue

Note: if there is an error in the extreme values of servo motor then it can be rectified by replacing the values of y2 and y1 (y2 = max value, y2 = min value) in the formulas above.

### Mapping the Input and Output Values using map function:

Using Arduino, you don’t even have to do all this calculation rather you can use map() function to automatically map input values with the output.

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

value: the variable 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.

This will Re-map 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.

Use of both the methods depends on the situation. Map function works on integer math and it doesn’t generate fractional values. Therefore, whenever you need precise readings in decimals you should use manual calculations.

## 3. CONNECTIONS

Note that servos draw considerable power, so if you need to drive more than one or two, you’ll probably need to power them from a separate supply (i.e. not the +5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.  Figure 3: Connections of Servo motor and potentiometer with Arduino

## 4. PROGRAMS

### Program 1: Control the position of a hobby servo with a potentiometer using slope line formula.

```/*UNCIA ROBOTICS | www.unciarobotics.com
PROGRAM:ROTATE SERVO MOTOR USING POTENTIOMETER
Control the position of a hobby servo with a potentiometer
using slope line formula.

Connections:
Arduino     Servo            |    Arduino   Potentiometer
9           Signal (Orange)  |    A0        signal pin
5V          VCC (Red)        |    VCC       one end
GND         GND (Brown)      |    GND       other end
*/

#include<Servo.h>       //include Servo library
Servo myServo;          //give a name to your Servo
int pot;                //variable to store input values
int angle;              //variable to store angle
void setup() {
myServo.attach(9);    //attach Servo to pin 9
Serial.begin(9600);   //Start Serial communication
}

void loop() {
angle = 0.176 * pot;  //map IP/OP using slope line formula
Serial.println(angle);//print maped value on Serial Monitor
myServo.write(angle); //write mapped value on Servo
}
```

### Program 2: Control the position of a hobby servo with a potentiometer using map() function.

```/*UNCIA ROBOTICS | www.unciarobotics.com
PROGRAM:ROTATE SERVO MOTOR USING POTENTIOMETER
Control the position of a hobby servo with a potentiometer
using map() function.

Connections:
Arduino     Servo            |    Arduino   Potentiometer
9           Signal (Orange)  |    A0        signal pin
5V          VCC (Red)        |    VCC       one end
GND         GND (Brown)      |    GND       other end
*/

#include<Servo.h>       //include Servo library
Servo myServo;          //give a name to your Servo
int pot;                //variable to store input values
int angle;              //variable to store angle
void setup() {
myServo.attach(9);    //attach Servo to pin 9
Serial.begin(9600);   //Start Serial communication
}

void loop() {
angle = map(pot, 0, 1023, 0, 180); //map the IP/OP values
Serial.println(angle);//print maped value on Serial Monitor
myServo.write(angle); //write mapped value on Servo
}
```

## 6. FAQs

Which method is best for mapping the values; map() or manual calculations?

It depends on the type of project you are working on. If you quicky want to convert one range of values to other you can use map() function. But if you are working on a project which needs precise calculations or conversions including decimal values, then you can go for manual calculations.

What is the slope line formula?

For this, let us discuss how to find slope of line when its coordinates are known.

Let P(x1, y1) and Q(x2, y2) be two
points on non-vertical line l whose inclination is θ. Obviously, x1≠ x2, otherwise the line will become perpendicular to the x-axis and its slope will not be defined. The inclination of the line l may be acute or obtuse. Let us take these two cases. Draw perpendicular QR to x-axis and PM perpendicular to RQ as shown in Figure a and Figure b.

CASE 1: When angle θ is acute:

In Figure a, ∠MPQ = θ. … (1)
Therefore, slope of line l = m = tan θ

But in ∆MPQ, we have

tanθ = MQ/MP = (y2-y1)/(x2-x1)… (2)

From equations (1) and (2), we have

m = (y2-y1)/(x2-x1)

CASE 2: When angle θ is obtuse:

In Figure b, ∠MPQ = 180-θ. … (1)
Therefore, θ = 180 – ∠MPQ

Now, slope of the line l

m=tanθ

m= tan ( 180° – ∠MPQ) = – tan ∠MPQ

m= -(MP/MQ) = – ((y2-y1)/(x2-x1))

m = (y2-y1)/(x2-x1)