In this lesson, we’ll introduce a new sensor: the Light-Dependent Resistor (LDR). It’s a special component that changes its resistance depending on how much light it receives. We’ll use it to make our LED turn on and off based on the amount of light in the room!
Objective: Learn how to use a light sensor to detect light levels and control an LED.
Materials:
- Arduino Uno board
- Breadboard
- LED (any color)
- 220-ohm resistor
- Light-dependent resistor (LDR)
- Jumper wires
Steps:
- Review: Discuss previous lessons and components, including the concept of sensors.
- Meet the LDR: Introduce the LDR and explain how it changes resistance based on light intensity.
- Building the Circuit: Connect the LDR, LED, resistor, and Arduino board to the breadboard. The LDR should be connected to an analog input pin on the Arduino.
- Programming the Sensor: Copy and paste the code below:
Step 1: Connecting the LDR
- Find the two legs of the LDR: They are usually the same length.
- Connect one leg of the LDR to an analog input pin on the Arduino: We’ll use pin A0 in this lesson.
- Connect the other leg of the LDR to a ground rail on the breadboard: This completes the LDR’s circuit.
Step 2: Connecting the LED and Resistor (Review)
- Connect the LED and resistor as you did in Lesson 1: Make sure the LED’s longer leg is connected to a power rail and the resistor’s other leg is connected to the same row as the LED’s shorter leg.
Step 3: Connecting the Arduino (Review)
- Connect the LED’s longer leg to digital pin 13 on the Arduino: This is the output pin for the LED.
Step 4: Programming the Arduino
- Open the Arduino IDE.
- Copy and paste the following code:
const int ledPin = 13; // LED connected to pin 13
const int sensorPin = A0; // LDR connected to analog pin A0
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read LDR value
if (sensorValue < 500) { // If light is low
digitalWrite(ledPin, HIGH); // Turn LED on
} else {
digitalWrite(ledPin, LOW); // Turn LED off
}
}
- Upload the code to the Arduino board.
Step 5: Testing the Circuit
- Shine a light on the LDR: You should see the LED turn off.
- Cover the LDR with your hand (or block the light): The LED should turn on.
Congratulations! You’ve created a light-sensitive circuit!
Let’s Experiment:
- Adjust the threshold: Change the number in the if (sensorValue < 500) line to adjust how sensitive the circuit is to light.
- Use the LDR in different environments: Try testing the circuit in a dark room, a bright room, or outdoors to see how the LED reacts.
Keep experimenting with sensors and have fun with Arduino!