// Define pin numbers
int led = 17; // LED is connected to GPIO 17 on the ESP32
int pot_pin = 35; // Potentiometer is connected to GPIO 35 (analog pin on ESP32)
int pot_value = 0; // Variable to store the analog value read from the potentiometer
void setup() {
// Initialize the LED pin as an output pin
pinMode(led, OUTPUT);
// Start the serial communication to send data to the Serial Monitor
Serial.begin(9600); // Set baud rate to 9600 for serial communication
}
void loop() {
// Read the analog value from the potentiometer (range: 0 to 4095 for ESP32)
pot_value = analogRead(pot_pin);
// Map the potentiometer value (0 to 4095) to a brightness value (0 to 255)
// 4055 is used instead of 4095 to avoid any noise at the upper range
int brightness = map(pot_value, 0, 4055, 0, 255);
// Set the brightness of the LED based on the potentiometer value
analogWrite(led, brightness); // Control the LED brightness
// Print the potentiometer value to the Serial Monitor for debugging
Serial.print("POT Value: ");
Serial.println(pot_value); // Display the raw potentiometer value
// Print the mapped brightness value to the Serial Monitor for debugging
Serial.print("Brightness: ");
Serial.println(brightness); // Display the brightness value
// Wait for 1 second before repeating the loop
delay(1000); // 1-second delay between readings
}