0Pricing
Arduino & IoT Academy · Aula

Luz noturna automática

Acenda um LED quando escurecer e houver movimento.

Luz noturna automática é uma aula grátis de Arduino & IoT Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Arduino & IoT Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Arduino & IoT Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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.

Perguntas Frequentes

A aula “Luz noturna automática” é grátis?

Sim — o texto completo de “Luz noturna automática” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Arduino & IoT Academy, atualize para CoddyKit PRO. O curso de Arduino & IoT Academy inclui 4 aulas no total.

O que vou aprender em “Luz noturna automática”?

Acenda um LED quando escurecer e houver movimento. Você pratica Arduino & IoT Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Arduino & IoT Academy?

Nenhuma experiência prévia é necessária. Arduino & IoT Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “Luz noturna automática”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Arduino & IoT Academy?

Sim. Cada aula de Arduino & IoT Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Leia um sensor de movimento PIR
  2. Meça a luz com um LDR
  3. Defina um limiar de luz
  4. Luz noturna automática
← Voltar para Arduino & IoT Academy