Other Arduino Codes and Videos by Robojax
In this video you will learn how to control a servo using potentiometer (variable resistor), LCD1602 and Arduino.
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
// May 20, 2018 20:08
// Written by Ahmad S. for Robojax.com at Ajax, Ontario, Canada
//Watch video for this code at https://youtu.be/DN1LPMSZtNA
//This code is taken from http://robojax.com/learn/arduino
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// start of servo settings *******
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int angle; // the angle servo is sent to
// END of servo settings *******
void setup() {
// Robojax Servo knob with LCD1602 Test
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Robojax Servo knob with LCD1602 Test
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Robojax Srevo Knob");
delay(2000);
}
void loop() {
// Robojax Servo knob with LCD1602 Test
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
angle = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(angle); // sets the servo position according to the scaled value
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pot. Value:");
// set the cursor to column 0, line 1
lcd.setCursor(11, 0);
lcd.print(val);
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print("Angle :");
lcd.setCursor(11, 1);
lcd.print(angle);
delay(500);
// Robojax Servo knob with LCD1602 Test
}
If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal