// Define the pins
int servoPin = 9; // PWM pin for the servo
int potPin = A0; // Analog input pin for the potentiometer
// Variables
int potValue; // Potentiometer value (0-1023)
int servoAngle = 0; // Servo angle (0-180)
void setup() {
// Attach the servo pin
pinMode(servoPin, OUTPUT);
}
void loop() {
// Read the potentiometer value
potValue = analogRead(potPin);
// Map the potentiometer value to the servo angle (0-180)
servoAngle = map(potValue, 0, 1023 , 0, 180);
// Generate the PWM signal to control the servo
int pulseWidth = map(servoAngle, 0, 180, 1000, 2000); // Convert angle to microseconds
digitalWrite(servoPin, HIGH); // Start the pulse
delayMicroseconds(pulseWidth); // Delay for pulse width
digitalWrite(servoPin, LOW); // End the pulse
delay(200); // Delay between updates (adjust as needed)
}