How to control Servo Motor Control using Arduino Nano

 Servo motors allows precise positioning of shaft arms. They are used in robotics, industrial automation, conveyor belts, home automation, cameras, telescope etc where precise control of arm position is important. Servo motor can be controlled using PWM signals. We have posted a tutorial which illustrated couple of PWM application example with Arduino Nano. In this tutorial we will show how you can control a Servo Motor using Arduino Nano and a Potentiometer.

Interfacing Arduino Nano with Servo Motor and Potentiometer

The connection diagram is shown below. The Arduino Nano analog pin A0 is connected to the middle pin of a 10KOhm potentiometer. The Servo Motor signal pin is connected to PWM digital pin 10. 

Wiring diagram Servo Motor Potentiometer and Arduino
When the potentiometer knob is rotated, depending upon the voltage read by the ADC module of the Arduino Nano, corresponding duty cycle PWM signal is generated at the digital pin 10. This PWM signal will drive the Servo motor. When for example the voltage read from the POT is 0V then 0 duty cycle PWM signal is generated and when for example a voltage of 2.5V is read then a duty cycle of 50% or PWM level of 127 is generated. These values internally are converted to corresponding angle values that is actually sent.

On a breadboard the connection looks like the following.

Servo motor with Arduino Nano and Potentiometer

Program code 

The following is program code for Servo Motor control using PWM.

#include <Servo.h>

Servo myservo; 
const int servoPin = 10;

void setup() {

void loop() {
   int p = analogRead(A0);
   int angle = map(p, 0, 1023, 0 ,179);

In the above code we have made use of Servo library that is included in the Arduino IDE. To use this library we have included the header file Servo.h. To use the servo library and functions within it we first have to instantiate a servo object of class Servo. Here the name of the instantiated servo object is myservo. In the next line we just created an alias name servoPin for the PWM digital pin 10. This naming is optional. In the setup() function we interfaced the servoPin to the myservo object using the attach() method. In the loop() function we first read in the voltage sensed at analog pin A0 using the analogRead() function and store that value in variable p of data type int. Next we use the map() function to map the value read p which can have value from 0 to 1023 to angle value range 0 to 179. Finally we use the write() method to send the angle value to the digital pin 10. This produces PWM signal to drive the servo motor to specific angle.

Video demonstration

Below video demonstrates control of Servo motor using POT and Arduino Nano.


In this Arduino Nano tutorial we showed how to control a Servo Motor using Potentiometer and Arduino Nano. We showed you how to interface the servo motor with Arduino nano and potentiometer. The program code was provided and explained. 

See related tutorial Servo Motor Control with Motor Shield and Arduino and Servo Motor control using Simulink and Arduino.


Post a Comment

Previous Post Next Post