Arduino Lesson 5: The Light Sensor – Make Your LED Respond to Light!

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:

  1. Review: Discuss previous lessons and components, including the concept of sensors.
  2. Meet the LDR: Introduce the LDR and explain how it changes resistance based on light intensity.
  3. 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.
  4. Programming the Sensor: Copy and paste the code below:

Step 1: Connecting the LDR

  1. Find the two legs of the LDR: They are usually the same length.
  2. Connect one leg of the LDR to an analog input pin on the Arduino: We’ll use pin A0 in this lesson.
  3. 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)

  1. 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)

  1. 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

  1. Open the Arduino IDE.
  2. 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
  }
}
  1. Upload the code to the Arduino board.

Step 5: Testing the Circuit

  1. Shine a light on the LDR: You should see the LED turn off.
  2. 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!