/*
Práctica: ESP32 con sensor PIR, potenciómetro y dos LEDs
Componentes:
- ESP32 DevKit
- Sensor PIR
- Potenciómetro de 10 kΩ
- LED 1 con resistencia de 220 Ω
- LED 2 con resistencia de 220 Ω
Funcionamiento:
- Si el sensor PIR detecta presencia, se enciende el LED 1.
- Si el sensor PIR no detecta presencia, se apaga el LED 1.
- Si el potenciómetro llega a la mitad o está por encima, se enciende el LED 2.
- Si el potenciómetro está por debajo de la mitad, se apaga el LED 2.
Conexiones:
PIR:
VCC -> 3V3
OUT -> GPIO19
GND -> GND
Potenciómetro:
Extremo izquierdo -> 3V3
Pin central -> GPIO34
Extremo derecho -> GND
LED 1:
GPIO18 -> resistencia 220 Ω -> ánodo LED 1
cátodo LED 1 -> GND
LED 2:
GPIO5 -> resistencia 220 Ω -> ánodo LED 2
cátodo LED 2 -> GND
*/
// Entradas
const int PIR_PIN = 19;
const int POT_PIN = 34;
// Salidas
const int LED_PIR = 18;
const int LED_POT = 5;
// Rango ADC aproximado del ESP32: 0 a 4095
// La mitad aproximada del rango es 2048
const int UMBRAL_POT = 2048;
void setup() {
Serial.begin(115200);
// Configuración de entradas
pinMode(PIR_PIN, INPUT);
// Configuración de salidas
pinMode(LED_PIR, OUTPUT);
pinMode(LED_POT, OUTPUT);
// Estado inicial de los LEDs
digitalWrite(LED_PIR, LOW);
digitalWrite(LED_POT, LOW);
Serial.println("Practica ESP32: PIR + Potenciometro + 2 LEDs");
Serial.println("------------------------------------------------");
Serial.println("LED 1: Controlado por sensor PIR");
Serial.println("LED 2: Controlado por potenciometro");
Serial.println("Condicion LED 2: se enciende si el potenciometro llega a la mitad o mas");
Serial.println("Umbral del potenciometro: 2048");
Serial.println("------------------------------------------------");
}
void loop() {
// Leer sensor PIR
int estadoPIR = digitalRead(PIR_PIN);
// Leer valor analógico del potenciómetro
int valorPot = analogRead(POT_PIN);
// Calcular porcentaje aproximado del potenciómetro
int porcentajePot = map(valorPot, 0, 4095, 0, 100);
// Control del LED 1 por sensor PIR
if (estadoPIR == HIGH) {
digitalWrite(LED_PIR, HIGH);
} else {
digitalWrite(LED_PIR, LOW);
}
// Control del LED 2 por potenciómetro
// Se enciende cuando el valor llega a la mitad o está por encima
if (valorPot >= UMBRAL_POT) {
digitalWrite(LED_POT, HIGH);
} else {
digitalWrite(LED_POT, LOW);
}
// Mostrar información en el monitor serial
Serial.print("PIR: ");
if (estadoPIR == HIGH) {
Serial.print("Presencia detectada");
} else {
Serial.print("Sin presencia");
}
Serial.print(" | LED1: ");
if (estadoPIR == HIGH) {
Serial.print("ENCENDIDO");
} else {
Serial.print("APAGADO");
}
Serial.print(" | Potenciometro GPIO34: ");
Serial.print(valorPot);
Serial.print(" / 4095");
Serial.print(" | Posicion aprox.: ");
Serial.print(porcentajePot);
Serial.print("%");
Serial.print(" | Umbral: ");
Serial.print(UMBRAL_POT);
Serial.print(" | Estado del potenciometro: ");
if (valorPot >= UMBRAL_POT) {
Serial.print("MITAD O MAYOR");
} else {
Serial.print("MENOR A LA MITAD");
}
Serial.print(" | LED2: ");
if (valorPot >= UMBRAL_POT) {
Serial.println("ENCENDIDO");
} else {
Serial.println("APAGADO");
}
delay(500);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4