Arduino projects: Servo potentiometer control

Servos are useful devices, not only in robotics but almost everywhere. They are powerful, cheap and easy to use. You can also easily transform them to work in a continuous manner and use them as normal motors in you robot.

It can moves a ultrasonic sensor around to make a radar or be used to turn the front wheels in a RC car or close the lock of your door. As you can see servos are really useful and you should know how they works. Today, you will make your first project with servo and a potentiometer.

The potentiometer will work as an input on the analog pin A0 of Arduino and after mapping the values of the analog read function, we will set it to the servo.

Arduino Servo-Potentiometer project

Let’s start with the components needed for the project:

First, Let’s connect all the components together.

You can find the diagram that will let you do so below.

Because the legs of my potentiometer are too short to connect the cables to it directly, I plugged it to a small breadboard and connected the cables to it.

The servo I used is a standard size servo motor, but it does not matter, any type will just do.

You can find a cheaper micro servo motors which you can as well make use of for this project. To connect it to the Arduino, I used the cables with male-male connectors.

As I said I used a standard servo motor

But you can also use micro servo like this one

Those small blue micro servos are very popular and you can find them virtually in every electronic or RC shop.

Now we can upload a program to the Arduino.

The Code

//including the library that we need to control the servo motor
#include <Servo.h>
//declaring our servo
Servo servo;
void setup() {

//declaring A0 analog pin as input
pinMode(A0, INPUT);
//setting pin 13 as a servo pin, because here we connected our servo

void loop() {
//here we are mapping the value of analogRead() from pin A0 (potentiometer)
//this value is between 0 and 1023 and we want to have 0 180 because this
//is the value that servo uses when it is mapped we can write it to servo
//with servo.write()
servo.write(map(analogRead(A0), 0, 1023, 0, 180));

//map function works similiar to smoething like this:
//analogRead(A0)/1023 * 180
//but it has some more advanced features

Here you go!

With hope this gave you a glimpse of how a servo works and how to use it, I will suggest you go ahead an tweak the design to make it a little more complicated and gradually build something more advanced using this as a foundation. Try adding a delay at the end of the loop to see what happens.


Related Posts