Control a Servo with a Pot

click to enlarge
Today we will learn how to control a RC Servo motor (9g) with a Linear Taper potentiometer 10 K and the Arduino Uno board.
The components that we will be using for this experiment can be found in any Arduino Uno starter kit. You can find Ads of several kits on my website at the right hand column.

I will list all the components for this experiment.

click to enlarge
  • Arduino Uno Board
  • One Breadboard (small)
  • RC Servo motor ( Tower Pro Micro Servo 9g SG 90)
  • Potentiometer (B10K)
  • Male-Female jumper wires 3
  • Male-Male jumper wires 3
  • small M-M Jumper wires 4
You can use wires of any color. The wires I normally use in my experiment are Red for Positive supply, Black for Ground, Yellow for Signal pins. 

Here is the circuit diagram for this experiment.
click to enlarge

We will give 5 volts supply to both the Servo motor and the Potentiometer. I will use the Red and Blue marked lanes on the breadboard for 5 volt and ground connections. If your breadboard does not have Red and Blue lines on it, that  does not matter. You can use one of the lane for supply and other lane for Ground. I will get the 5 volts supply from the pin on Arduino's board in the Power section. Also I will use one of the Gnd pins adjacent to the 5 volts pin. 

In the diagram above Yellow wires indicate the signal pins both for Servo motor and the Pot. However in reality the wires that come connected with this servo motor are Orange, Red and Brown. Therefore you have to keep that in mind. The wires are 
Orange - Signal
Red - 5 volts
Brown - Gnd.

In case of the Potentiometer you can decide which colors of wires you use. The middle pin of the Pot is the Signal, therefore Yellow, The pin at the side of the notch will be Black for Gnd and the pin on the other end will be Red - for 5 volts.

I will connect the M-M jumper wires to the wires of the Servo motor. The connectors of the Servo motor are Female and we will connect them on the breadboard therefore we will need Male pins at both the ends. 

I will connect the Potentiometer pins with M-F jumper wires. The female pins can fit into the Pot pins, and the male pins can go in the breadboard. You can attach the potentiometer directly on the breadboard if you want to.

Connect the Red wire of the Servo motor to the Red lane, and Brown wire to the Blue lane on the breadboard. 

Connect the Red wire from the Pot to the Red lane and the Black wire to the Blue lane on the breadboard. 

Connect the Orange wire from the Servo motor anywhere the breadboard, and connect another wire in the same row to the Digital (PWM) pin number 9 on Arduino board.

Connect the Yellow wire from the Potentiometer anywhere on the breadboard and connect another wire in the same row to the Analog pin number A0 on Arduino board.

We will be using the example program from the Arduino's IDE for this experiment. Connect the Arduino's board to your computer's USB port and open this program.

File - Examples - Servo - Knob

Upload this program on the Arduino, and after that you will be able to move the Servo shaft with the rotation of the potentiometer knob.

The program given in the IDE is self explanatory. However if you are new to programming. I have created a trimmed version of the same program which will work fine for this experiment. To type the code in this page is tricky therefore I have given a snapshot of this program. Please enlarge the image to for comfortable viewing. 

click to enlarge

Here we are using a header library called Servo.h
We create an instance of the Servo object. 
We will require an integer variable, I have named it x.

The setup() function
Here 9 indicated the Digital pin number 9 on Arduino's board. You can use any PWM enabled pins instead. 

The loop() function

analogRead(0) function reads the output of the potentiometer and stores the value in variable x. This value is in the range of (0 to 1023) 

map() function
This will map the value of x from the range of (0 to 1023 to corresponding value (0 to 180) and return the mapped value to x. In fact you can use another variable to store it's value if you want to understand the program clearly.

Now at this moment x contains a value in the range of 0 to 180, which is the range of movement of the Servo motor's shaft. We can now move the shaft to that degree. 


will turn the shaft to (x) Degrees.


gives a 15 millisecond delay between iteration of the loop() function. This give the time required for actual movement of the shaft.

Popular posts from this blog

Introduction to Scratch Programming for ICSE Class 5

Hour of Code - Frozen with Anna and Elsa

Programming for kids - Course 4 # Artist for loops