#include <ESP32Servo.h> // ESP32-compatible Servo library (uses hardware PWM via LEDC)
// Pin definitions (constants)
int SERVO1_PIN = 18; // GPIO used to output the PWM signal to the servo
int POT1_PIN = 34; // ADC-capable GPIO used to read the potentiometer (input only on ESP32)
int SERVO2_PIN = 19;
int POT2_PIN = 35;
int SERVO3_PIN = 23;
int POT3_PIN = 32;
int SERVO_MIN_US = 500;
int SERVO_MAX_US = 2500;
// Create a Servo object instance
Servo servo1;
Servo servo2;
Servo servo3;
void setup() {
// Initialize Serial for debugging (Serial Monitor)
Serial.begin(115200);
// Servos typically expect a 50 Hz control signal (20 ms period)
servo1.setPeriodHertz(50);
servo2.setPeriodHertz(50);
servo2.setPeriodHertz(50);
// Attach the servo to a pin and define pulse width limits in microseconds:
// min pulse = 500 us
// max pulse = 2500 us
servo1.attach(SERVO1_PIN, SERVO_MIN_US, SERVO_MAX_US);
servo2.attach(SERVO2_PIN, SERVO_MIN_US, SERVO_MAX_US);
servo3.attach(SERVO3_PIN, SERVO_MIN_US, SERVO_MAX_US);
}
void loop() {
// Read the potentiometer using the ESP32 ADC (typically returns 0..4095)
int adc1 = analogRead(POT1_PIN);
int adc2 = analogRead(POT2_PIN);
int adc3 = analogRead(POT3_PIN);
// Map ADC range (0..4095) to servo pulse width range (500..2500 microseconds)
// This converts knob position to a pulse width that represents the servo position.
int us1 = map(adc1, 0, 4095, SERVO_MIN_US, SERVO_MAX_US);
int us2 = map(adc2, 0, 4095, SERVO_MIN_US, SERVO_MAX_US);
int us3 = map(adc3, 0, 4095, SERVO_MIN_US, SERVO_MAX_US);
// Send the pulse width to the servo (more precise than servo.write(angle))
servo1.writeMicroseconds(us1);
servo2.writeMicroseconds(us2);
servo3.writeMicroseconds(us3);
// Debug output: print raw ADC value and computed pulse width
Serial.print("Pot_1:");Serial.print(adc1);Serial.print("-->");Serial.print(us1);
Serial.print("Pot_2:");Serial.print(adc2);Serial.print("-->");Serial.print(us2);
Serial.print("Pot_3:");Serial.print(adc3);Serial.print("-->");Serial.print(us3);
// Small delay to limit update rate and reduce Serial spam
delay(10);
}