Lección 7 de 15 E/S digital
Lee un pulsador y enciende un LED según su estado.
Código
pulsador_led.ino
const int LED_PIN = 8;
const int BUTTON_PIN = 2;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
💡 ¿Por qué LOW al pulsar?
Con
INPUT_PULLUP el pin está en HIGH por defecto. Al pulsar conectas a GND → lee LOW.ℹ️ Rebote (bounce)
Los pulsadores mecánicos vibran unos ms al cerrar. Más adelante añadirás antirrebote; de momento es aceptable.