#include <Servo.h> // Include the Servo library
// Create a Servo object
Servo myServo;
// Define pin numbers
const int potPin = A0; // Analog pin for the potentiometer
const int servoPin = 10; // PWM pin for the servo motor
void setup() {
// Initialize the servo
myServo.attach(servoPin);
// Initialize the serial communication (for debugging)
Serial.begin(9600);
}
void loop() {
// Read the value from the potentiometer (0 to 1023)
int potValue = analogRead(potPin);
// Map the potentiometer value to a range for the servo (0 to 180 degrees)
int servoAngle = map(potValue, 0, 1023, 0, 180);
// Set the servo position
myServo.write(servoAngle);
// Print the values for debugging
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print(" | Servo Angle: ");
Serial.println(servoAngle);
// Small delay to make the movement smoother
delay(15);
}