Landing : Athabascau University
  • Blogs
  • Circuit 3A: Servo Motors

Circuit 3A: Servo Motors

I had no issues building the circuit. I modified the example code so that instead of having the position of the servo set by the position of the potentiometer, the servo constantly moves back and forth, and the potentiometer sets the speed at which the servo moves. Here is the code:

#include <Servo.h> //include the servo library 

int minAngle = 20; //The min and max angles specify how far the servo motor
int maxAngle = 160; //will rotate in each direction.

int minDelay = 1; //The min and max delays specify how fast the servo motor
int maxDelay = 20; //will move back and forth between the min and max angles.
Servo myservo; //create a servo object
void setup() {
myservo.attach(9); //tell the servo object that its servo is plugged into pin 9
void loop() {
int servoPosition =; //Initial position of the servo

//This loop continues moving the servo motor until it reaches the maxAngle
while(servoPosition < maxAngle){
//This loop moves the servo motor in the other direction until it reaches the minAngle
while(servoPosition > minAngle){

//This method determines how fast the servo motor will move
int getDelayTime(){
int potPosition = analogRead(A0); //use analog read to measure the position of the potentiometer (0-1023)

int delayTime = map(potPosition, 0, 1023, minDelay, maxDelay); //convert the potentiometer number to an int between minDelay and maxDelay

return delayTime;

The code is based on the example program. My changes include the addition of variables to set the speed and the range of motion of the servo. I have also included two while loops inside the loop() function. These two while loops move the servo motor by incrementing or decrementing the servoPosition variable by one, calling myservo.write() with the new servoPosition value, and then calling delay() with the value returned by the getDelayTime() method. The getDelayTime() method uses the map() function to set the int value of delayTime based on the position of the potentiometer, which is then returned.

Here is a video of the program in action:  ServoMotor

I did not have any major issues while writing the program. I initially used analogRead(A0) as the argument for the delay() function, so the getDelayTime() method was not needed. It did work, but the servo always moved slowly unless the potentiometer was turned practically all the way to one side. This is because analogRead(A0) could return a value as high as 1023. To reduce the range of values that can be used for the delay time I created the getDelayTime() method which uses the map() function to set the delay time. According to the Arduino documentation, the map() function maps a number from one range to a different range, so I used it to map the potentiometer value from its original range of 0 – 1023 to the range from minDelay to maxDelay.


These comments are moderated. Your comment will not be visible unless accepted by the content owner.

Only simple HTML formatting is allowed and any hyperlinks will be stripped away. If you need to include a URL then please simply type it so that users can copy and paste it if needed.