How ToIoT HardwaresRaspberry PiTutorials/DIY

Interfacing a Light Sensor (LDR) with Raspberry Pi

In this tutorial, we will learn how to interface a Light Dependent Resistor (LDR) with a Raspberry Pi and control an LED based on the detected light intensity.

Components Required

  • Raspberry Pi (any model with GPIO support)
  • LDR (Light Dependent Resistor)
  • 1μF Capacitor
  • LED
  • Resistor (220Ω for LED protection)
  • Breadboard and Jumper Wires

Circuit Diagram and Connections

  • Connect one side of the LDR to the 3.3V pin on the Raspberry Pi.
  • Connect the other side of the LDR to the GPIO pin 18.
  • Connect the same side of the LDR (going to GPIO 18) to one side of the 1μF capacitor.
  • Connect the other side of the capacitor to GND.
  • Connect the positive leg of the LED to GPIO pin 25 (with a 220Ω resistor for protection).
  • Connect the negative leg of the LED to GND.

How It Works

The readLDR() function reads light intensity by:

  1. Setting the LDR pin as OUTPUT and setting it to LOW.
  2. Switching the LDR pin to INPUT and counting the time it takes for the voltage across the capacitor to exceed 1.4V (logical HIGH).
  3. The reading count is inversely proportional to the light intensity; higher light levels cause faster charging, resulting in a lower count.

Example Code

Python
# Example Code: Interfacing LDR with Raspberry Pi
# Iotbyhvm.ooo - Explore TechBytes

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# Pin Definitions
ldr_threshold = 1000
LDR_PIN = 18
LIGHT_PIN = 25

def readLDR(PIN):
    reading = 0
    GPIO.setup(LDR_PIN, GPIO.OUT)
    GPIO.output(LDR_PIN, False)
    time.sleep(0.1)
    GPIO.setup(LDR_PIN, GPIO.IN)

    # Count until pin reads HIGH
    while not GPIO.input(LDR_PIN):
        reading += 1

    return reading

def switchOnLight(PIN):
    GPIO.setup(PIN, GPIO.OUT)
    GPIO.output(PIN, True)

def switchOffLight(PIN):
    GPIO.setup(PIN, GPIO.OUT)
    GPIO.output(PIN, False)

# Main Loop
try:
    while True:
        ldr_reading = readLDR(LDR_PIN)
        print(f"LDR Reading: {ldr_reading}")

        if ldr_reading < ldr_threshold:
            switchOnLight(LIGHT_PIN)
        else:
            switchOffLight(LIGHT_PIN)

        time.sleep(1)
except KeyboardInterrupt:
    GPIO.cleanup()  # Clean up GPIO pins on exit

Code Explanation

  • readLDR() Function: Measures the light intensity based on charging time.
  • switchOnLight() and switchOffLight() Functions: Control the LED state.
  • Main Loop: Continuously monitors the LDR reading and switches the LED on/off accordingly.

Running the Code

  1. Save the code as ldr_sensor.py.
  2. Run the script with the following command:
sudo python3 ldr_sensor.py
  1. Observe the LED turning ON when the light intensity is low and turning OFF when it’s bright.

Sample Output

LDR Reading: 450  --> LED ON
LDR Reading: 1200 --> LED OFF

Important Notes

  • Ensure you have the RPi.GPIO library installed. If not, use this command:
sudo apt-get install python3-rpi.gpio
  • Properly connect the LDR, capacitor, and LED to prevent hardware damage.

Conclusion

This tutorial covers how to connect and program an LDR sensor with Raspberry Pi to control an LED based on ambient light levels. This project is ideal for smart lighting systems and energy-saving automation projects.

For further learning, consider combining this setup with temperature sensors, motion detectors, or IoT platforms to expand your home automation solutions.

You may like also:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

8 thoughts on “Interfacing a Light Sensor (LDR) with Raspberry Pi

Leave a Reply

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