Automatic Night Light
Turn on an LED when it gets dark and motion appears.
Automatic Night Light is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Automatic Night Light” lesson free?
Yes — the full text of “Automatic Night Light” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Automatic Night Light”?
Turn on an LED when it gets dark and motion appears. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Automatic Night Light” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Read a PIR Motion Sensor
- Measure Light with an LDR
- Set a Light Threshold
- Automatic Night Light