Public post

6.1 controlling a servo motor with a potentiometer


In this tutorial, you are going to control the rotation of a servo with a potentiometer. In contrast to the previous tutorial about controlling a servo motor by code. we will use an analog input to control the servo motor. This will be helpful if you decide to make something with a pan/tilt platform.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 2x
Breadboard 400 points 1x
100 uF Capacitor 2x
10Kohm potentiometer 1x
Servo Motor 1x

Circuit Diagram

Circuit Diagram – Atach 5V and ground to one side of your breadboard from the Arduino. The servo has three wires coming out of it. One is power (red), one is ground (black), and the third (white) is the control line that will receive information from the Arduino. Plug three male headers into the female ends of the servo wires. Connect the headers to your breadboard so that each pin is in a diferent row. Connect 5V to the red wire, ground to the black wire, and the white wire to pin 9.

When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100uf capacitor across power and ground right next to the male headers, you can smooth out any voltage changes that may occur. You can also place a capacitor across the power and ground going into your potentiometer. These are called decoupling capacitors because they reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that’s the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode.

The Code

#include <Servo.h>
Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer
int value; // variable to read the value from the analog pin
int angle; //variable to store the mapped angle

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

  value = analogRead(potpin); // reads the value of the potentiometer

  Serial.print("value: ");

  angle = map(value, 0, 1023, 0, 179); // scale it to use it with the servo
  myservo.write(angle); // sets position to the scaled value

  Serial.print(", angle: ");

  delay(15); // waits for the servo to get there

Code Explanation

Similar to the way you used pulses to PWM an LED in the Color Mixing Lamp Project, servo motors expect a number of pulses that tell them what angle to move to. The pulses always come at the same time intervals, but the width varies between 1000 and 2000 microseconds.

While it’s possible to write code to generate these pulses, the Arduino sofware comes with a library that allows you to easily control the motor. Because the servo only rotates 180 degrees, and your analog input goes from 0-1023, you’ll need to use a function called map() to change the scale of the values coming from the potentiometer

Import the Servo Libary

To use the servo library, you’ll first need to import it. This makes the additions from the library available to your sketch.

#include <Servo.h>
Create a Servo Object

To refer to the servo, you’re going to need to create a named instance of the servo library in a variable. This is called an object. When you do this, you’re making a unique name that will have all the functions and capabilities that the servo library ofers. From this point on in the program, every time you refer to myServo, you’ll be talking to the servo object.

Servo myservo; // create servo object to control a servo
Declare the variables

Set up a variable for storing the angle of the servo motor is very important. You also need to declare which analog pin is used. In this example we are using analog pin A0.

// analog pin used to connect the potentiometer 
int potpin = A0; 
// variable to read the value from the analog pin
int value;  
//variable to store the mapped angle
int angle; 
Pinmode of servo object

Since we are using the library servo.h we can set the “pinmode” for the servo by the command object.attach(pinnumber);. Remember to set the object name right and the pinnumber.

myservo.attach(9); // attaches the servo on pin 9 to the servo object
Read the analog pin

You arrived at the void loop() part of the sketch. Firstly, you need to read the analog value of the potentiometer. Secondly, we print the value of the analog value to the Serial Monitor.

// reads the value of the potentiometer 
value = analogRead(potpin); 
Serial.print("value: "); 
Map the analog signal

To create a usable value for the servo motor from your analog input, it’s easiest to use the map() function. This handy function scales numbers for you. In this case, it will change values between 0-1023 to values between 0-179. It takes five arguments: the number to be scaled (here it’s potVal), the minimum value of the input (0), the maximum value of the input (1023), the minimum value of the output (0), and the maximum value of the output (179). Store this new value in the angle variable. Then, print out the mapped value to the serial monitor.

Finally, it’s time to move the servo. The command servo. write() moves the motor to the angle you specify. At the end of the loop() put a delay so the servo has time to move to its new position.

// scale it to use it with the servo 
angle = map(value, 0, 1023, 0, 179);
// sets position to the scaled value
Serial.print(", angle: "); 

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member


Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.