#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h> // Include the ESP32 Servo library
const uint8_t I2C_ADDRESS = 0x27;
const uint8_t LCD_CHARS = 16;
const uint8_t LCD_LINES = 2;
LiquidCrystal_I2C lcd(I2C_ADDRESS, LCD_CHARS, LCD_LINES);
const int potPin = 35;
const int potVccPin = 36; // 5V supply for potentiometer
const int servoPin = 21; // PWM-capable pin for servo control
// Servo motor properties
const int servoMinAngle = 0;
const int servoMaxAngle = 180;
unsigned int servoVal, angle;
ESP32Servo myservo; // Use ESP32Servo instead of Servo
void setup()
{
Serial.begin(115200);
// Initializing the potentiometer power supply
pinMode(potVccPin, OUTPUT);
digitalWrite(potVccPin, HIGH);
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Output");
lcd.setCursor(0, 1);
lcd.print("Angle: "); // Two spaces for better alignment
// Attach servo motor
myservo.setPeriodHertz(50); // Set PWM frequency to 50Hz (standard for servos)
myservo.attach(servoPin);
}
void loop()
{
servoVal = analogRead(potPin); // Read potentiometer value (0-1023)
// Map potentiometer value to servo angle range (0-180)
angle = map(servoVal, 0, 1023, servoMinAngle, servoMaxAngle);
// Update servo position only if there's a significant change (avoids jitter)
if (abs(myservo.read()-angle)>=5)
{
myservo.write(angle);
lcdDisplay(angle);
}
delay(20);
}
void lcdDisplay(int currentAngle)
{
lcd.setCursor(7, 1); // Set cursor to the second space after "Angle:"
lcd.print(" "); // Clear any previous angle value
lcd.print(currentAngle);
lcd.print((char)223); // Degree symbol
}