Day 4: Movement in the Shadows

Movement in the Shadows

The maintenance tunnels beneath Cogsworth City hummed with the sound of cooling systems and electrical circuits. Tech Specialist Rivera adjusted her headlamp and crept forward through the narrow passage, her HERO Board clutched in one gloved hand. Somewhere in these depths, the city's automated defense systems had gone offline.

"Control, this is Rivera," she whispered into her comm device. "I'm approaching sector seven. The power readings are all wrong down here." The tunnels were supposed to be lit by motion-activated panels, but half of them had failed. Every shadow could hide a maintenance droid gone rogue, or worse, one of the saboteurs who'd been targeting the city's infrastructure.

Rivera knew she needed an early warning system. Something that could detect movement and changes in light, alerting her to threats before they got too close. The photoresistor sensor in her kit would be perfect for this. It could sense when shadows moved, when someone blocked a light source, or when a maintenance panel flickered on unexpectedly.

She pulled out her components and began connecting the sensor to her HERO Board. In these dangerous tunnels, light wasn't just illumination—it was survival. The sensor would watch for any change in the ambient light, triggering an LED warning and optional buzzer when shadows shifted unexpectedly. Whether it was a helpful maintenance bot or something more sinister, she'd know something was coming before it knew she was there.

What You'll Learn

When you finish this lesson, you'll be able to build your own light-detection security system. You'll understand how photoresistors sense changes in light levels and how to program your HERO Board to react when shadows move or light conditions change.

Specifically, you'll master reading analog sensor values, setting up threshold-based detection, and controlling multiple outputs based on sensor input. Your finished project will light up an LED and sound a buzzer whenever someone or something blocks the light hitting your photoresistor.

Understanding Light Detection

Think about the automatic doors at a grocery store. They know when you're approaching, even though no one pressed a button. Many of these systems use light sensors that detect when your body blocks the light between a transmitter and receiver. Your shadow triggers the door to open.

A photoresistor works on a similar principle, but simpler. It's like a light-sensitive dimmer switch. When lots of light hits it, it allows electricity to flow easily (low resistance). When it's in shadow, it restricts the electrical flow (high resistance). Your microcontroller can measure this resistance change and determine whether the light level increased or decreased.

This makes photoresistors perfect for security systems, automatic lighting, or even simple games. They're watching for change, not just absolute brightness. A photoresistor in a bright room will still detect when someone walks by and casts a shadow. It's measuring the difference, not the total amount of light.

In Cogsworth City's maintenance tunnels, this sensor becomes Rivera's digital watchdog. It continuously monitors the ambient light, ready to sound an alarm the moment something moves through the shadows. The beauty is in its simplicity: light changes, resistance changes, microcontroller detects the change, action happens.

Wiring Your Light Detection System

Wiring diagram for photoresistor circuit
  1. Photoresistor to A0: This analog pin can measure the changing resistance values. Digital pins only know ON or OFF, but analog pins can detect the full range of light levels from very bright to very dark.
  2. LED to pin 13: Pin 13 has a built-in resistor, making it perfect for LEDs. This will be your visual alert when shadows are detected.
  3. Buzzer to pin 12: This digital output pin will control your audible alert. The buzzer needs a clear ON/OFF signal, which digital pins provide perfectly.
  4. Ground connections: Both the LED and buzzer need a path back to ground to complete their electrical circuits. Without ground, no current flows.
  5. Power for photoresistor: The photoresistor needs a connection to 5V through a resistor to create a voltage divider circuit that the microcontroller can read.

Code Walkthrough: Setting Up the Variables

Pin Declarations

const int photoPin = A0;    // the pin where the photoresistor is connected
const int ledPin = 13;      // the pin where the LED is connected
const int buzzerPin = 12;   // the pin where the buzzer is connected (optional)