Day 4: Movement in the Shadows

Astrid and Gear had made significant progress by controlling multiple LEDs. However, the city of Cogsworth remained shrouded in darkness, and mysterious movements in the shadows hinted at lurking dangers. Gear knew that to protect themselves, they needed to detect changes in light to sense movement and potential threats. Today, Astrid would learn how to use photoresistors to detect light changes, a crucial skill for creating a responsive alert system.

Gear: “Astrid, we’ve come a long way, but the shadows are hiding potential dangers. We need to set up a system to detect changes in light and alert us to any movement. This will help us stay safe and continue our journey. Are you ready to master movement in the shadows?”

Astrid: “I’m ready, Gear. This sounds important for our safety. Let’s get started.”

  1. Understand the basics of using photoresistors to detect light changes.
  2. Learn how to connect photoresistors to the HERO board and breadboard.
  3. Write and upload code to alert when light changes are detected using the Arduino IDE.
  4. Develop further skills in coding and electronics.

– HERO board
– USB cable
– Breadboard
– LED
– Resistors (220 ohm and 10k ohm)
– Photoresistor
– Jumper wires (Male to Male)

Gear: “Astrid, today we’re going to use photoresistors to detect changes in light. This will help us sense movement and stay safe.”
Astrid: “How do they work, Gear?”
Gear: “A photoresistor’s resistance changes based on the amount of light it receives. We can use this to detect changes in our environment.”

Gear: “The photoresistor allows us to measure light levels. When it’s bright, the resistance is low, and when it’s dark, the resistance is high. We can use this change in resistance to detect movement by monitoring light changes.”

Astrid: “So, how do we connect the photoresistor to the HERO board?”

Gear: “We’ll create a voltage divider using the photoresistor and a fixed resistor. This will allow us to read the light levels as an analog value.”

Gear: “Place the photoresistor on the breadboard.”
– Connect one end of the photoresistor to the 5V rail on the breadboard.
– Connect the other end of the photoresistor to an analog pin on the HERO board (e.g., A0) and to one end of a 10k ohm resistor.
– Connect the other end of the 10k ohm resistor to the ground (GND) rail on the breadboard.

Gear: “Now we need to add an alert system to indicate when a change in light is detected. We can use an LED for this.”

Astrid: “Alright, Gear. Let’s set it up.”

Gear: “Keep the LED in the breadboard with the long leg to a digital pin (e.g., pin 13) and the short leg to GND through a 220 ohm resistor.”

Gear: “Now, it’s time to write some code to detect changes in light and trigger an alert.”

Astrid: “I’m excited, Gear. Let’s see if this works.”

Gear: “We’ll write a code that reads the light level from the photoresistor. If a significant change is detected, it will trigger an alert by turning on the LED or buzzer.”

Gear: “Let’s break down the code step-by-step:”
– Open the Arduino IDE on the computer.
– Write the following code:




     - `const int photoPin = A0;`: "This line sets up a constant variable named `photoPin` to analog pin A0, where the photoresistor is connected."

     - `const int ledPin = 13;`: "This line sets up a constant variable named `ledPin` to digital pin 13, where the LED is connected."

     - `int lightLevel = 0;`: "This line creates a variable named `lightLevel` to store the light level reading."

     - `int threshold = 100;`: "This line sets a threshold value for detecting a significant change in light."

     - `void setup() { }`: "This function runs once when you start your HERO R3. It sets up the LED and buzzer pins as outputs and begins serial communication."

     - `pinMode(ledPin, OUTPUT);`: "This line tells the HERO R3 that the LED pin will be an output."

     - `Serial.begin(9600);`: "This line starts serial communication for debugging purposes."

     - `void loop() { }`: "This function runs over and over again in a loop."

     - `lightLevel = analogRead(photoPin);`: "This line reads the light level from the photoresistor and stores it in `lightLevel`."

     - `Serial.println(lightLevel);`: "This line prints the light level to the serial monitor for debugging purposes."

     - `if (lightLevel < threshold) { }`: "This line checks if the light level is below the threshold."

     - `digitalWrite(ledPin, HIGH);`: "If the light level is below the threshold, this line turns on the LED."

     - `digitalWrite(ledPin, LOW);`: "If the light level is above the threshold, this line turns off the LED."

     - `digitalWrite(buzzerPin, LOW);`: "If the light level is above the threshold, this line turns off the buzzer (optional)."

     - `delay(100);`: "This line adds a small delay to stabilize the readings."

Astrid: “The code is uploaded, Gear. What now?”

Gear: “Wave your hand over the photoresistor to see if the LED turns on. If the light changes significantly, the alert system should activate.”

Astrid: “It works! The LED turns on when I block the light. This will help us detect movement in the shadows!”

Astrid: “We did it, Gear! Now we can detect movement in the shadows. This is so useful!”
Gear: “You did a fantastic job, Astrid. Today, you learned how to use a photoresistor to detect changes in light. This skill will help us stay safe and aware of our surroundings.”

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *