Back to Step by Step Course by Robojax

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

If you don't like to see ads while video is being played you can purchase YouTube premium Here

  • 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 <http://people.interaction-ivrea.it/m.rinott>
     
     Watch full instruction on YouTube https://youtu.be/E-H-4igygzA
      modified on 8 Nov 2013
      by Scott Fitzgerald
      http://www.arduino.cc/en/Tutorial/Knob
     
      Update by Ahmad Shamshiri on Jan 01, 2019  in Ajax, Ontario, Canada
     
     http://robojax.com/learn/arduino/?vid=robojax-servo-knob
     */
     
     #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.
       lcd.backlight();    
     }
     
     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
       Serial.print(val);
       Serial.println();
     
       lcd.clear();
       lcd.print("Servo with Pot.");
       lcd.setCursor (0,1); // go to start of 2nd line
       lcd.print("Angle:");  
       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;
       }else
       {
         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 thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps 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 Amazon.com and affiliated sites.

    Right Side
    footer