/*
* Servo with Potentiometer Control for ESP32
* This sketch controls a servo motor using a potentiometer.
*/
#include <ESP32Servo.h> // Include the ESP32Servo library
Servo myservo; // Create a Servo object to control the servo
const int potPin = 12; // GPIO pin for the potentiometer (must be an ADC pin)
const int servoPin = 5; // GPIO pin for the servo motor
void setup() {
myservo.attach(servoPin); // Attach the servo to the specified pin
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Servo and Potentiometer control initialized");
}
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value (0-4095)
int angle = map(potValue, 0, 4095, 0, 180); // Scale it to servo angle (0-180°)
myservo.write(angle); // Set the servo position
delay(15); // Wait for the servo to reach the position
// Print the potentiometer and angle values for debugging
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" - Servo Angle: ");
Serial.println(angle);
}