#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Initialize the LCD with I2C address 0x27, 20 columns, and 4 rows
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Initialize the Servo
Servo myServo;
// Define the pins
const int potPin = A0; // Potentiometer connected to A0
const int servoPin = 9; // Servo connected to pin 9
void setup() {
// Initialize the Serial Monitor
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Attach the servo to the pin
myServo.attach(servoPin);
// Display initial message on the LCD
lcd.setCursor(0, 0);
lcd.print("Pot Voltage: ");
lcd.setCursor(0, 1);
lcd.print("Angle: ");
}
void loop() {
// Read the potentiometer value (0-1023)
int potValue = analogRead(potPin);
// Convert the potentiometer value to voltage
float voltage = potValue * (5.0 / 1023.0); // Assuming a 5V reference
// Map the potentiometer value to servo angle (0-180)
int servoAngle = map(potValue, 0, 1023, 0, 180);
servoAngle = constrain(servoAngle, 0, 180);
// Set the servo position
myServo.write(servoAngle);
// Display the potentiometer voltage and servo angle on the LCD
lcd.setCursor(12, 0);
lcd.print(" "); // Clear previous value
lcd.setCursor(12, 0);
lcd.print(voltage, 2); // Display voltage with 2 decimal places
lcd.setCursor(12, 1);
lcd.print(" "); // Clear previous value
lcd.setCursor(12, 1);
lcd.print(servoAngle); // Display servo angle
// Print the values to the Serial Monitor for debugging
Serial.print("Potentiometer Voltage: ");
Serial.print(voltage, 2);
Serial.print(" V, Servo Angle: ");
Serial.println(servoAngle);
// Delay for a moment
delay(500); // Update every half a second
}