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:
- Setting the LDR pin as OUTPUT and setting it to LOW.
- Switching the LDR pin to INPUT and counting the time it takes for the voltage across the capacitor to exceed 1.4V (logical HIGH).
- The reading count is inversely proportional to the light intensity; higher light levels cause faster charging, resulting in a lower count.
Example Code
# 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()
andswitchOffLight()
Functions: Control the LED state.- Main Loop: Continuously monitors the LDR reading and switches the LED on/off accordingly.
Running the Code
- Save the code as
ldr_sensor.py
. - Run the script with the following command:
sudo python3 ldr_sensor.py
- 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:
Pingback: Interfacing a light Sensor (LDR) with Raspberry Pi — IoTbyHVM – Explore tech bytes – hashstacks
Pingback: Raspberry Pi - Introduction | Overview | Setup and Management | Tutorials
Pingback: piCore (Tiny Core) Linux on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Setup Docker on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Alternatives in 2019 - IoTbyHVM - Bits & Bytes of IoT
Pingback: Flask SocketIO - Socket.IO integration for Flask applications.
Pingback: Control LED with Raspberry Pi using Nodejs - IoTbyHVM
Pingback: Raspberry Pi Introduction and Tutorials — OnionLinux