0Pricing
Arduino & IoT Academy · Lección

Luz nocturna automática

Encienda un LED cuando oscurezca y aparezca movimiento.

Luz nocturna automática es una lección gratuita de Arduino & IoT Academy en CoddyKit. Esta es la lección 4 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.

The Project Goal

Now combine your skills into one device: an automatic night light that switches an LED on when it is dark and someone moves.

Two Inputs, One Output

You will read two inputs, the LDR for light and the PIR for motion, then drive one output: an LED.

Plan the Pins

Assign each part a pin: the LDR on an analog input, the PIR on a digital input, and the LED on a digital output.

Set Up the LED

In setup, mark the LED pin as an OUTPUT so the board can drive current to it on command.

pinMode(ledPin, OUTPUT);

Read Both Sensors

Each loop, grab a fresh light value and the current motion state so your decision uses up-to-date data.

int light = analogRead(A0);
int motion = digitalRead(pirPin);

Combine With AND

The light should turn on only when it is dark and motion is present. Use && to require both at once.

if (light < threshold && motion == HIGH)

Turn the LED On

When both conditions hold, drive the LED pin HIGH to light it up for whoever just walked in.

digitalWrite(ledPin, HIGH);

Turn the LED Off

Otherwise, set the pin LOW so the light goes dark again when motion stops or the room is bright.

digitalWrite(ledPin, LOW);

Keep It Lit Briefly

People dislike a light that snaps off instantly. Add a short hold time so it stays on a while after the last motion.

Skip Daytime Triggers

The light check matters: requiring dark means daytime motion is ignored, saving power and avoiding pointless blinks.

Make It Real

Swap the LED for a relay and you can switch a real lamp. Same logic, bigger impact in your hallway.

Quick Check

What condition turns the night light on?

Recap: Your Night Light

You merged an LDR, a PIR, and an LED, using a threshold and && so the light glows only in the dark with motion.

Preguntas frecuentes

¿La lección «Luz nocturna automática» es gratis?

Sí — el texto completo de «Luz nocturna automática» 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 «Luz nocturna automática»?

Encienda un LED cuando oscurezca y aparezca movimiento. 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 4 de 4.

¿Cuánto tiempo toma la lección «Luz nocturna automática»?

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

  1. Leer un sensor de movimiento PIR
  2. Medir la luz con una LDR
  3. Establecer un umbral de luz
  4. Luz nocturna automática
← Volver a Arduino & IoT Academy