#include <Servo.h>
#include <Wire.h> // Include the Wire library for I2C communication
#include <LiquidCrystal_I2C.h> // Include the LCD library
Servo servo; // Create a Servo object
int potPin = A0; // Analog input pin for the potentiometer
int servoPin = 9; // PWM pin for the servo
int pos = 0; // Variable to store the servo position
// Initialize the LCD with its I2C address
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) as per your LCD's address
void setup() {
servo.attach(servoPin); // Attaches the servo on pin 9 to the servo object
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Potentiometer: ");
}
void loop() {
// Read the value from the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value (0-1025) to the servo angle range (0-180)
int servoAngle = map(potValue, 0, 1025, 0, 180);
// Move the servo to the mapped angle
servo.write(servoAngle);
// Display the potentiometer value on the LCD
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print("Value: ");
lcd.print(potValue);
// Optional delay to slow down the servo movement
delay(15); // Adjust this delay to control the servo speed
}