// ESP32 Potentiometer Interface with Slide Switch and LED
// Read potentiometer value, control LED, and print data via serial
const int potPin = 34; // Potentiometer connected to GPIO 34 (Analog ADC1_CH6)
const int ledPin = 12; // LED connected to GPIO 12
const int LED_BUILTIN= 13; // Slide switch connected to GPIO 14
bool isRunning = false; // Flag to track start/stop state
void setup() {
Serial.begin(115200);
pinMode(potPin, INPUT); // Set potentiometer pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(LED_BUILTIN, OUTPUT);
pinMode(13, INPUT_PULLUP);
}
void loop() {
// Read potentiometer value (0 to 4095)
int analogValue = analogRead(potPin);
// Map the analog value to LED brightness (0 to 255)
int brightness = map(analogValue, 0, 4095, 0, 255);
// Turn on/off LED based on threshold (2000)
if (analogValue > 2000) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// Turn on/off LED based on button status
{
if ('LED_BUILTIN ')
digitalWrite(LED_BUILTIN, digitalRead((13)));
}
// Print data via serial
Serial.print("Raw data: ");
Serial.print(analogValue);
Serial.print(" | Mapped data: ");
Serial.print(brightness);
Serial.print(" | LED status: ");
Serial.println(analogValue > 2000 ? "ON" : "OFF");
delay(100); // Adjust delay as needed
}