if/else: el LED sigue al botón
Encienda un LED solo mientras mantenga pulsado el botón
if/else: el LED sigue al botón es una lección gratuita de Arduino & IoT Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Arduino & IoT Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
Make a Decision
Reading a button is great, but the magic is reacting to it. An if statement lets your board choose what to do based on the reading.
if Runs Conditionally
The code inside if only runs when its test is true. So you can light an LED only when the button is actually pressed.
if (state == LOW) {
// button is pressed
}Test for Pressed
Remember the inversion: with INPUT_PULLUP, pressed is LOW. So your condition checks whether the stored state equals LOW.
int state = digitalRead(2);
if (state == LOW) {
digitalWrite(13, HIGH);
}Add an else Branch
What about when it isn't pressed? The else block runs whenever the if test is false, giving you the other half of the behavior.
if (state == LOW) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}LED Follows the Button
Together if and else make the LED mirror the button: on while held, off when released. That is your first real input-to-output link. 💡
Set Up Both Pins
The button pin is an input and the LED pin is an output. Declare both modes in setup() so each pin knows its job.
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(13, OUTPUT);
}One Equals vs Two
A classic trap: use == to compare, not a single =. One equals assigns a value, while two equals asks if they match.
Loop Keeps It Live
Put the whole thing in loop() so it checks the button thousands of times a second. The LED reacts the moment you press.
Read Then Decide
The pattern is always the same: read the input, then decide with if/else. Get comfortable with it; you'll reuse it everywhere.
Compare Right in the if
You can skip the variable and test the read directly. It reads cleanly and does exactly the same comparison inside the parentheses.
if (digitalRead(2) == LOW) {
digitalWrite(13, HIGH);
}From Sensing to Acting
You just closed the loop from input to output with pure logic. Swap the LED for a buzzer or motor and the same idea still drives it.
Quick Check
Pick the condition that lights the LED while the button is held.
Recap
You used if/else to make an LED follow a button: LOW lights it, HIGH turns it off. Remember == for comparison. Next you'll wire it all up. 🔌
Preguntas frecuentes
¿La lección «if/else: el LED sigue al botón» es gratis?
Sí — el texto completo de «if/else: el LED sigue al botón» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Arduino & IoT Academy, actualiza a CoddyKit PRO. El curso de Arduino & IoT Academy incluye 4 lecciones en total.
¿Qué aprenderé en «if/else: el LED sigue al botón»?
Encienda un LED solo mientras mantenga pulsado el botón Practicas Arduino & IoT Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Arduino & IoT Academy?
No se requiere experiencia previa. Arduino & IoT Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.
¿Cuánto tiempo toma la lección «if/else: el LED sigue al botón»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Arduino & IoT Academy?
Sí. Cada lección de Arduino & IoT Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- INPUT frente a INPUT_PULLUP
- Lectura del estado de un botón
- if/else: el LED sigue al botón
- Conecte un botón en una breadboard