Skip to content

Interfacing Light(LDR) Sensor Sensor with Raspberry Pi

Introduction

In this guide, we will explore how to interface an LDR (Light Dependent Resistor) sensor with the Raspberry Pi. The LDR sensor serves the purpose of detecting light intensity. We will utilize the GPIO (General Purpose Input/Output) pins on the Raspberry Pi to establish a connection with the LDR sensor and control an LED based on the light intensity readings acquired by the sensor. When the light intensity is low, we will program the Raspberry Pi to turn on the LED, indicating the presence of low light conditions. By following this guide, you will gain the necessary knowledge to integrate an LDR sensor with the Raspberry Pi and create light-based applications or automation systems.

Problem Statement

The objective of this project is to interface the LDR sensor using Raspberry Pi 3B+ and control an LED based on the light intensity detected by the sensor.

Requirements

  1. Raspberry Pi
  2. LDR sensor : HW-072 module
  3. LED
  4. Resistor (220/330 ohms)
  5. Jumper wires
  6. Breadboard

Working

The LDR(Light) sensor can be utilized with a Raspberry Pi 3B+ to detect light and provide visual feedback through an LED connected to a breadboard.

Circuit Diagram

  • Connect the +5V of LDR sensor to +5V(Pin 2) of RPi.
  • Connect the GND of LDR sensor to GND(Pin 14) of RPi.
  • Connect one side of the resistor to GPIO 24(PIN 18) of RPi.
  • Connect other side of the resistor to LED.
  • Connect other side of the LED to GND(PIN 14).
  • Connect the DO of LDR sensor to GPIO 23(Pin 16) of RPi.
Raspberry Pi

Code

Now let's write the Python code to detect light in a vicinity. Follow the steps below:

  1. Connect to your Raspberry Pi using VNC or Desktop environment.
  2. Launch the Geany Python IDE or open a text editor to write the code.
  3. Create a new file named ldr.py and save the following code into it.
  4. You can copy and paste the following code from here into the file.
py

import RPi.GPIO as GPIO  
import time  

GPIO.setwarnings(False)     # disable warnings
GPIO.setmode(GPIO.BCM)      # Set up BCM GPIO numbering      
LDR_PIN = 23                # GPIO23 (pin 16) connected to LDR
LED_PIN = 24                # GPIO24 (pin 18) connected to LED
GPIO.setup(LDR_PIN, GPIO.IN)   # LDR pin set as input
GPIO.setup(LED_PIN, GPIO.OUT)  # LED pin set as output
GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED

try:
    while True:  
        # If the LDR sensor detects light, LED is turned on
        if GPIO.input(LDR_PIN):
            GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED
            print("Light Not Detected")

        # Otherwise, LED is turned off
        else:
            GPIO.output(LED_PIN, GPIO.LOW)
            print("Light Detected")

except KeyboardInterrupt:      # If CTRL+C is pressed, exit cleanly:
    pass

GPIO.cleanup()
  1. After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate to the folder you saved the file, execute it by typing python ldr.py

Output

LED will turn on when light is detected by the IR sensor. To stop the program, press Ctrl + C in the terminal where you executed the program.

Conclusion

By following this guide, you have acquired the knowledge to utilize an LDR (Light Dependent Resistor) sensor with a Raspberry Pi. You have successfully connected the LDR sensor, written the necessary Python code, and obtained the ability to detect light variations using the sensor. This opens up a wide array of possibilities for incorporating light detection capabilities into your Raspberry Pi projects. You can now explore and experiment with various applications, such as creating a smart lighting system that adjusts based on ambient light levels, designing a sunrise alarm clock that gradually increases light intensity, or even building a plant monitoring system that adjusts the amount of light provided to optimize growth. The integration of an LDR sensor with a Raspberry Pi empowers you to enhance your projects with intelligent light sensing capabilities. Enjoy exploring the potential of LDR sensors in your Raspberry Pi endeavors!