// www.robotronix.co.il
// Embedded Systems and Robotics Programming Course
// ESP32 - Potentiometer, LED and Servo Motor
#include <ESP32Servo.h> // Include ESP32 servo library
const int ledPin = 2; // LED connected to GPIO 2
const int potentiometerPin = 32; // Potentiometer connected to GPIO 32
const int servoPin = 4; // Servo connected to GPIO 4
Servo servo; // Create servo object
void setup() { // Run once at startup
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(potentiometerPin, INPUT); // Set potentiometer pin as input
servo.setPeriodHertz(50); // Set servo PWM frequency to 50 Hz
servo.attach(servoPin); // Attach servo to GPIO 4
Serial.begin(115200); // Start serial communication
}
void loop() { // Run continuously
int potValue = analogRead(potentiometerPin); // Read analog value (0-4095)
Serial.print("Potentiometer Value: "); // Print label
Serial.println(potValue); // Print measured value
if (potValue > 1000) { // Check if value is above threshold
digitalWrite(ledPin, HIGH); // Turn LED ON
servo.write(0); // Move servo to 0 degrees
} else { // Value is 1000 or less
digitalWrite(ledPin, LOW); // Turn LED OFF
servo.write(180); // Move servo to 180 degrees
}
delay(100); // Wait 100 milliseconds
}