Python Program 2: Interfacing LDR Sensor with Raspberry Pi

OBJECTIVE:

To interface an LDR sensor with the Raspberry Pi and write a Python program to turn ON an LED based on the ambient light level sensed by the LDR.


RESOURCES REQUIRED:


THEORY:

LDR (Light Dependent Resistor) Basics:

  • An LDR is a light-sensitive device whose resistance decreases as the light intensity increases.

  • In bright light, the LDR conducts more current; in darkness, its resistance increases, limiting current.

  • By connecting the LDR in a voltage divider with a resistor, we can read the light level as a digital HIGH or LOW using the Raspberry Pi’s GPIO pin.

Current Limiting Resistor for LED:

  • Just like with any LED circuit, a 330Ω resistor is used in series with the LED to prevent excess current and protect the LED.

GPIO Pins on Raspberry Pi:

  • The Raspberry Pi features GPIO pins that can be set as input or output in software.

  • We’ll use one GPIO pin as an input (to read from LDR) and another as an output (to control the LED).


Circuit Design:

  • LDR and 10kΩ form a voltage divider.

  • GPIO 26 reads the divided voltage and determines light intensity (1 = dark, 0 = light).

  • GPIO 2 controls the LED based on the LDR input.


Python Code: Switching LED Based on LDR Detection

import RPi.GPIO as GPIO import time # Define pins LDR_PIN = 26 # Input from LDR LED_PIN = 2 # Output to LED # Setup GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(LDR_PIN, GPIO.IN) GPIO.setup(LED_PIN, GPIO.OUT) try: while True: if GPIO.input(LDR_PIN): # If it's dark GPIO.output(LED_PIN, True) # Turn ON LED else: GPIO.output(LED_PIN, False) # Turn OFF LED time.sleep(0.5) except KeyboardInterrupt: GPIO.cleanup()

Explanation of the Program:

Import Libraries:

  • RPi.GPIO → To control GPIO pins
  • time → To allow delay (debounce and loop control)

Pin Setup:

  • LDR_PIN = 26 → Connects to the voltage divider from LDR
  • LED_PIN = 2 → Connects to the LED circuit
  • GPIO is set to BCM mode
  • Pins are set as input (LDR) and output (LED)

Main Loop:

  • If the LDR detects darkness (logic HIGH), the LED is turned ON.
  • If light is detected, the LED is turned OFF.
  • A small delay helps smooth the readings and prevent rapid flickering.

Exit Handling:

  • On pressing Ctrl + C, the loop exits, and GPIO.cleanup() resets the pins to a safe state.


🔁 Assignment Time!

📘 Assignment 1: Switching Multiple LEDs Based on LDR Reading

Objective: Extend the above program to control three LEDs (GPIO 2, 3, 4).
Behavior: When LDR detects darkness, turn all LEDs ON. Otherwise, turn them all OFF.

📘 Assignment 2: Switching Multiple LEDs With Different Delays Based on LDR Reading

Objective: Use 3 LEDs on GPIO pins (2, 3, 4).
Behavior:

  • When darkness is detected:

    • Turn ON LED 1 (GPIO 2) immediately.

    • Turn ON LED 2 (GPIO 3) after 1 second.

    • Turn ON LED 3 (GPIO 4) after 2 seconds.

  • When light is detected, turn OFF all LEDs at once.



🚀 Ready to Build?

💬 Drop your solution in the comments — show off your GPIO magic!
🔔 Check back tomorrow to see if your answer matches the solution!

Comments

Popular posts from this blog

Fundamental of python : 1.Python Numbers