Mission Briefing: The Great Blackout
The emergency klaxons echoed through Cogsworth City's underground tunnels as Chief Engineer Martinez pulled up the power grid diagnostics on her holographic display. Sector 7's main lighting array had failed again, leaving hundreds of maintenance workers stumbling through pitch-black corridors with nothing but handheld torches.
"We can't keep sending repair crews into those tunnels blind," she muttered, watching the casualty reports tick upward on her screen. Three twisted ankles, two head injuries, and one very angry sanitation bot that had crashed into a wall in the darkness. The city's backup lighting system was decades old, inefficient, and frankly, unreliable.
Martinez turned to the rookie engineers gathered around the briefing table. "What we need is something smarter. Something that knows when darkness falls and responds instantly. Not a manual switch that someone has to flip, not a timer that might be wrong, but a system that actually sees the light levels and reacts accordingly."
She tapped her stylus against the display, highlighting the failed sector. "Your mission is to design an automated night light system using photoresistors. These sensors can detect ambient light levels and trigger our LED arrays accordingly. We need two versions: one that gradually adjusts brightness as darkness falls, and another that clicks on full power when a threshold is crossed. The tunnels of Cogsworth City depend on you getting this right."
What You'll Learn
When you complete this mission, you'll be able to:
- Read analog light levels using a photoresistor sensor
- Automatically calibrate your sensor to different room conditions
- Create a continuously dimming night light that responds to ambient brightness
- Build a standard on/off night light with a brightness threshold
- Use the map() and constrain() functions to scale sensor values
- Implement adaptive calibration that learns your environment
Understanding Photoresistors
A photoresistor is essentially a piece of semiconductor material that changes its electrical resistance based on how much light hits it. Think of it like a dimmer switch that adjusts itself automatically. When bright light shines on it, electrons get excited and flow more easily, creating low resistance. In darkness, fewer electrons are available, creating high resistance.
Picture your bedroom window blinds. When they're wide open, lots of light streams in and you can see everything clearly. When they're mostly closed, only a little light gets through and the room feels dim. A photoresistor works similarly, except instead of controlling how much you can see, it controls how much electrical current can flow through it.
The brilliant part is that we can measure this changing resistance with our microcontroller and use it to make decisions. Is it getting dark? Turn on the lights. Is the sun coming up? Dim them down or turn them off entirely. This creates responsive systems that adapt to their environment without any human intervention.
Real night lights in your home work exactly this way. Street lights that automatically turn on at dusk use photoresistors. Even your smartphone's automatic screen brightness adjustment relies on a tiny light sensor that operates on these same principles.
Wiring Your Light Sensor
The photoresistor needs to be part of a voltage divider circuit to work properly with our microcontroller. Here's why each connection matters:
- Connect one leg of the photoresistor to the 5V pin (this provides our reference voltage)
- Connect the other leg to both analog pin A0 AND one leg of a 10kΩ resistor
- Connect the remaining leg of the 10kΩ resistor to ground (GND)
- Connect your LED's positive leg to pin 9 through a 220Ω resistor
- Connect the LED's negative leg directly to ground
The 10kΩ resistor creates a voltage divider with the photoresistor. As light changes the photoresistor's resistance, the voltage at pin A0 changes proportionally. This gives us a varying voltage signal that represents light intensity.
Complete Code: Two Night Light Versions
Here's the full code for both night light versions. Copy this into your editor first, then we'll break down how it works: