Servo Motor with Potentiometer and LCD with Arduino - SP-LCD-1

Project Video
Project Details

In this video we learn how to control the angle of servo motor using a potentiometer (variable resistor)
and display the angle on LCD1602 or LCD2004 with I2C module that has 4 wires.

This is code 1 where we need breadboard to distribute power from 5V to Potentiometer, Servo motor and LCD.
Details have been explained in the video.
Here is the code for use without breadboard.

Topics shown in video

  • 00:40 Introduction
  • 04:25 Wiring Explained
  • 08:14 Finding I2C Address
  • 09:14 Arduino Code explained
  • 19:35 Demonstration LCD1602 with Breadboard
  • 20:30 Demonstration LCD2004
  • 21:00 Demonstration Power from Digital pins
  • 22:15 Demonstration limiting angle

Project Code

 * Original code is taken form Arduino IDE 
 * from File->Examples->Servo->Knob
 * This is Arduino code to use display angle of servo motor on LCD1602 or LCD2004 with I2C module. The angle is set changed by 
 * potentiometer(variable resistor)
 * This is code 1 of this tuitorial where breadboard is needed.
  * See code SP-LCD-2:
 * Please watch video instruction :
 * Updated from original code by 
 * Ahmad Shamshiri on Nov 22, 2020 at 15:53 in Ajax
 * Ontario, Canada 

 * Get this code and other Arduino codes from
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon

or make donation using PayPal

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
// Set the LCD address to 0x27 for a 16 chars and 2 line display

//LCD settings
const uint8_t I2C_ADDRESS =0x3f;//watch video. use I2C Scanenr to get the address
const uint8_t LCD_CHAR= 16;
const uint8_t LCD_LINE = 2;
char *TITLE_ANGLE1 ="Angle: ";

LiquidCrystal_I2C lcd(I2C_ADDRESS, LCD_CHAR, LCD_LINE);
unsigned int LCD_VCC=13;//5V pin for LCD


// start of servo1 settings *******
Servo myservo1;  // create servo 1 object to control a servo
int pot1pin = A0;  // analog pin used to connect the potentiometer
int pot1VccPin = 12;//pin 12 used as 5V for potentiometer
unsigned int servo1Pin=3;//any pin with ~ which means pin is PWM enabled
unsigned int servo1AngleMin=0;//servo1 minimum angle 
unsigned int servo1AngleMax=180;//servo1 maximum angle details 
//// do not change the lines below
unsigned int servo1Val=0;// variable to read the value from the analog pin
unsigned int angle1=0;
// END of servo1 settings *******

void setup()

  Serial.println("Servo with Potentiometer");
  myservo1.attach(servo1Pin);  // attaches the servo 1  to the servo object

  pinMode(pot1VccPin, OUTPUT);
  digitalWrite(pot1VccPin,HIGH);//5V for Potentiometer (variable resistor)
  // initialize the LCD
  pinMode(LCD_VCC, OUTPUT);
  digitalWrite(LCD_VCC,HIGH);//5V for LCD

  lcd.print("Robojax Servo");
  lcd.print("Angle: "); 

void loop()
 servo1Val = analogRead(pot1pin);            // reads the value of the potentiometer (value between 0 and 1023)
 sendServo(servo1Val);//send sevo zero
}// loop end

 * sendServo(int value)
 * @brief sends the servo the "avlue" angle position
 * @param "value" integer between 0 to 180
 * @return none
 * Written by Ahmad Shamshiri for
 * on Nov 22, 2020  in Ajax, Ontario, Canada
void sendServo(int value)
  // Please watch video instruction :
  unsigned int newAngle1;
  newAngle1 = map(value, 0, 1023, servo1AngleMin, servo1AngleMax);     // scale it to use it with the servo (value between servoAngleMin and servoAngleMax)
  if(angle1 !=newAngle1)
  angle1 =newAngle1;

 * lcdDisplay(int angle)
 * @brief dispalys the "angle" angle position
 * @param "angle" integer between 0 to 180
 * @return none
 * Written by Ahmad Shamshiri for
 * on Nov 22, 2020  in Ajax, Ontario, Canada
void lcdDisplay(int angle)
 //Please watch video instruction :
   // LCD1602 for Vervo motor
   lcd.setCursor((unsigned)strlen(TITLE_ANGLE1), 1);
   lcd.print(angle);//print value of angle
} //

 * @brief clears a line of display (erases all characters)
 * @param none
 * @return does not return anything
 * Written by Ahmad Shamshiri
 * code May 28, 2020 at 16:21 in Ajax, Ontario, Canada
void clearCharacters()
    for (int i=(unsigned)strlen(TITLE_ANGLE1)-1; i<=LCD_CHAR-1; i++)
    lcd.setCursor (i,1); //  

Required File(s)