Servo Motor with Potentiometer and LCD with Arduino - SP-LCD-1
Servo Motor with Potentiometer and LCD with Arduino - SPLCD-1
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
-How to use breadboard
-Severvo with Arduino
-Potentiometer with Servo
Resources for this sketch
- PWM pins for Arduino boards
- Download SG90 Datasheet (pdf)
- Learn Arduino step by step from beginner to Advance (Coruse)
- Get Early Access to my videos via Patreon
Code #1 controling Servo motor
/*
* 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: http://robojax.com/L/?id=290
* Please watch video instruction : https://youtu.be/xrIiKqvm8hQ
* Updated from original code by
* Ahmad Shamshiri on Nov 22, 2020 at 15:53 in Ajax
* Ontario, Canada
* https://www.youtube.com/robojaxTV
* www.robojax.com
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62
If you found this tutorial helpful, please support me so I can continue creating
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64
* * 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 Robojax.com
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 <https://www.gnu.org/licenses/>.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// 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
#include <Servo.h>
// 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.begin(9600);
Serial.println("Servo with Potentiometer");
Serial.println("Robojax.com");
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.begin();
lcd.backlight();
lcd.print("Robojax Servo");
lcd.setCursor(0,1);
lcd.print("Angle: ");
delay(2000);
}
void loop()
{
servo1Val = analogRead(pot1pin); // reads the value of the potentiometer (value between 0 and 1023)
sendServo(servo1Val);//send sevo zero
delay(20);
}// 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 robojax.com
* on Nov 22, 2020 in Ajax, Ontario, Canada
*/
void sendServo(int value)
{
// Please watch video instruction : https://youtu.be/xrIiKqvm8hQ
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)
{
myservo1.write(newAngle1);
lcdDisplay(newAngle1);
angle1 =newAngle1;
delay(100);
}
}//sendServo()
/*
* lcdDisplay(int angle)
* @brief dispalys the "angle" angle position
* @param "angle" integer between 0 to 180
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on Nov 22, 2020 in Ajax, Ontario, Canada
*/
void lcdDisplay(int angle)
{
//Please watch video instruction : https://youtu.be/xrIiKqvm8hQ
// Robojax.com LCD1602 for Vervo motor
clearCharacters();
Serial.print("angle");
Serial.println(angle);
lcd.setCursor((unsigned)strlen(TITLE_ANGLE1), 1);
lcd.print(angle);//print value of angle
lcd.print((char)223);
} //
/*
clearCharacters()
* @brief clears a line of display (erases all characters)
* @param none
* @return does not return anything
* Written by Ahmad Shamshiri
* www.Robojax.com 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); //
lcd.write(254);
}
}//clearCharacters