Raspberry Pi - Light Sensor LED

This tutorial instructs you how to use Raspberry Pi and light sensor module to trigger LED. In detail:

The light sensor is also known as photoresistor, light-dependent resistor, photocell, LDR. The Raspberry Pi employs a light sensor to measure the ambient light level. If the surroundings are dark, the Raspberry Pi activates the LED, and conversely, if they are bright, it deactivates it.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LDR Light Sensor Module
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LED and Light Sensor

If you are not familiar with LED and light sensor (pinout, how it works, how to program ...), the following tutorials can help you out:

Wiring Diagram

The wiring diagram between Raspberry Pi and Light Sensor LED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

The light sensor module outputs LOW when light is present and HIGH when light is not detected. The Raspberry Pi is programmed to read the value from the light sensor module and, based on the readings, activate the LED when light is not present and turn it off when light is detected.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file ldr_led.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-light-sensor-led import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM GPIO.setmode(GPIO.BCM) # Define the PIN numbers for the light sensor and the LED LIGHT_SENSOR_PIN = 15 LED_PIN = 12 # Set up the light sensor PIN as an input with a pull-up resistor GPIO.setup(LIGHT_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up the LED PIN as an output GPIO.setup(LED_PIN, GPIO.OUT) try: while True: # Read the state of the light sensor light_state = GPIO.input(LIGHT_SENSOR_PIN) # Control the LED based on the light sensor state if light_state == GPIO.LOW: # Light is present, turn off the LED GPIO.output(LED_PIN, GPIO.LOW) else: # Light is not present, turn on the LED GPIO.output(LED_PIN, GPIO.HIGH) # Small delay to avoid rapid state changes time.sleep(1) except KeyboardInterrupt: # Clean up GPIO settings on program exit GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 ldr_led.py
  • Cover the LRD light sensor module by your hand
  • Check out the LED's state

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial