// Define el pin del LED
const int ledPin = 2;
// Define el pin Touch que vamos a usar (ejemplo: GPIO 4 - T0)
const int touchPin = 4;
// Define un umbral para detectar el toque
const int touchThreshold = 40; // Ajusta este valor según tu entorno
// Variable para almacenar el estado del LED
bool ledState = false;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
Serial.println("Control de LED con Sensor Touch ESP32");
Serial.print("Umbral Touch: ");
Serial.println(touchThreshold);
}
void loop() {
// Lee el valor del sensor Touch
int touchValue = touchRead(touchPin);
Serial.print("Valor Touch (GPIO ");
Serial.print(touchPin);
Serial.print("): ");
Serial.println(touchValue);
// Verifica si el valor del Touch está por debajo del umbral (se detecta un toque)
if (touchValue < touchThreshold) {
// Invierte el estado del LED
ledState = !ledState;
digitalWrite(ledPin, ledState);
Serial.print("LED Estado: ");
Serial.println(ledState ? "ENCENDIDO" : "APAGADO");
// Pequeño retardo para evitar múltiples toggles con un solo toque prolongado
delay(500);
}
delay(100); // Pequeño retardo para la lectura del sensor
}