Back to Step by Step Course by Robojax

Lesson 91: Controlling Servo motor using potentiometer and display angle on LCD

  • LCD Library from Robojax (zip)
  • LCD Library from GitHub
  • Part 05: Servo Motor

    This code (90-1) In this lesson we learn how control a servo motor using a potentiometer to move it to any angle between 0 to 180°. The angle is displayed on LCD screen. Full wiring diagram and wiring is explained with code.

    • 00:00 Introduction
    • 01:07 Code explained
    • 05:05 Wiring explained
    • 05:38 Demonstration
     Lesson 91
      * S05 Servo with Potentiometer and LCD1602-I2C
      Controlling a servo position using a potentiometer (variable resistor)
      by Michal Rinott <>
     Watch full instruction on YouTube
      modified on 8 Nov 2013
      by Scott Fitzgerald
      Update by Ahmad Shamshiri on Jan 01, 2019  in Ajax, Ontario, Canada
     #include <Servo.h>
     Servo myservo;  // create servo object to control a servo
     int potpin = A0;  // analog pin used to connect the potentiometer
     int servoPin = 3; // define a digital pin (with PWM) for servo. Can use only pin 3, 5, 6,9, 10, 11
     int VCC2 = 8;// define extra VCC for potentiometer
     int val;    // variable to read the value from the analog pin
     #include <Wire.h> 
     #include <LiquidCrystal_I2C.h>
     // Set the LCD address to 0x27 for a 16 chars and 2 line display
     LiquidCrystal_I2C lcd(0x3F, 16, 2);
     int VCC3 = 4;// define extra VCC for LCD
     int degPos;// degree symbol position
     void setup() {
       Serial.begin(9600);          //  setup serial
       myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
       pinMode(VCC2, OUTPUT);// set pin 8 as output
       digitalWrite(VCC2, HIGH);// set pin 8 HIGH so it acts as 5V pin
       // lines bellow are for LCD
       pinMode(VCC3, OUTPUT);// set pin 4 as output
       digitalWrite(VCC3, HIGH);// set pin 4 HIGH so it acts as 5V pin  
       lcd.begin();  // initialize the LCD, 
       // Turn on the blacklight and print a message.
     void loop() {
       val = analogRead(potpin)/5;            // reads the value of the potentiometer (value between 0 and 1023)
       val = map(val, 0, 1023/5, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
       myservo.write(val);                  // sets the servo position according to the scaled value
       lcd.print("Servo with Pot.");
       lcd.setCursor (0,1); // go to start of 2nd line
       lcd.setCursor (7,1); // go to 7 char of of 2nd line
       lcd.print(val);// print angle 
       if(val >99)
         degPos =10;
       }else if(val >9)
         degPos =9;
         degPos =8;
       lcd.setCursor (degPos,1); // go to 11 char of of 2nd line  
       lcd.print((char)223);// degree symbol
       delay(500);                           // waits for the servo to get there

    The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

    If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

    **** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

    Right Side