0Pricing
Arduino & IoT Academy · Lezione

Luce notturna automatica

Accenda un LED quando fa buio e viene rilevato un movimento.

Luce notturna automatica è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Luce notturna automatica» è gratuita?

Sì — il testo completo di «Luce notturna automatica» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Cosa imparerò in «Luce notturna automatica»?

Accenda un LED quando fa buio e viene rilevato un movimento. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Arduino & IoT Academy?

Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Luce notturna automatica»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?

Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Leggere un sensore di movimento PIR
  2. Misurare la luce con un LDR
  3. Impostare una soglia di luminosità
  4. Luce notturna automatica
← Torna a Arduino & IoT Academy