Raspberry Pi - LDR Module

The LDR light sensor module can figure out how much light is around it. It has two outputs: a digital output that can be either low or high, and an analog output.

This tutorial instructs you how to use an Raspberry Pi and an LDR light sensor module to find out the amount of light in an area. We will cover the following steps:

LDR Light Sensor Module
image source: diyables.io

Later on, you can change the code to turn on an LED or a light bulb (using a relay) when it detects light.

If you prefer a light sensor in its raw form, I suggest exploring the tutorial on the Raspberry Pi - Light Sensor.

Hardware Preparation

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

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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 LDR Light Sensor Module

The LDR light sensor module can be used to find out if there is light or measure how much light is present in the surroundings. It has two options: a digital output pin and an analog output pin.

Pinout

The LDR light sensor module has four pins with different functions:

  • VCC pin: Connect it to a power source (between 3.3V to 5V).
  • GND pin: Connect it to the ground (0V).
  • DO pin: This is a digital output pin. It goes HIGH when it's dark and LOW when it's light. You can adjust the threshold between darkness and lightness using a built-in potentiometer.
  • AO pin: This is an analog output pin. The output value decreases as the light gets brighter and increases as the light gets darker.
LDR Light Sensor Module Pinout
image source: diyables.io

Additionally, the LDR light sensor module has two LED indicators:

  • The PWR-LED indicator shows the power status.
  • The DO-LED indicator shows the state of light on the DO pin. It is illuminated when there is light and turned off when it is dark.

How It Works

Regarding the DO pin:

  • The LDR light sensor module has a built-in potentiometer that allows you to adjust the sensitivity or threshold for detecting light.
  • When the surrounding light intensity is higher than the set threshold (considered as light), the output pin of the sensor is set to LOW, and the DO-LED indicator turns on.
  • When the surrounding light intensity is lower than the set threshold (considered as dark), the output pin of the sensor is set to HIGH, and the DO-LED indicator turns off.

Regarding the AO pin:

  • The value read from the AO pin decreases as the surrounding light intensity increases (considered as light).
  • The value read from the AO pin increases as the surrounding light intensity decreases (considered as dark).

It's important to note that adjusting the potentiometer does not affect the value read from the AO pin.

Wiring Diagram

Since the light sensor module has two outputs, you can choose to use one or both of them, depending on what you need. However, Raspberry Pi does not have an ADC pin. If you want to read the analog value from AO pin, you have to use an external ADC module. For the sake of simplity, this tutorial onlys show how to read the digital value from DO pin.

  • The wiring diagram between Raspberry Pi and the LDR light sensor module.
The wiring diagram between Raspberry Pi and LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - Read value from DO pin

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_module.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-ldr-module import RPi.GPIO as GPIO from time import sleep # Set the Raspberry Pi GPIO pin number connected to the DO pin of the ldr light sensor module DO_PIN = 7 # Set the GPIO mode and configure the ldr light sensor module pin as INPUT GPIO.setmode(GPIO.BCM) GPIO.setup(DO_PIN, GPIO.IN) try: while True: # Read the current state of the ldr light sensor module light_state = GPIO.input(DO_PIN) # Print whether the light is present or not if light_state == GPIO.LOW: print("The light is present!") else: print("The light is NOT present!") # Add a small delay to prevent continuous readings sleep(0.1) except KeyboardInterrupt: # Clean up GPIO settings when Ctrl+C is pressed GPIO.cleanup() print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 ldr_module.py
  • Cover and uncover the LDR light sensor module by your hand or something
  • See the result on the Terminal.
PuTTY - Raspberry Pi
The light is present The light is present The light is NOT present The light is NOT present The light is NOT present The light is present The light is present The light is present

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

If you observe that the LED status remains constantly on or off, even when there is light, you have the option to adjust the potentiometer. This adjustment allows you to finely tune the light sensitivity of the sensor.

Additionally, it is possible to modify the code to activate an LED or a light when light is detected, or even to enable the rotation of a servo motor. Detailed information and step-by-step instructions can be found in the tutorials provided at the end of this guide.

Raspberry Pi Code - Detecting the Light

Let's modify the above code to detect the light state change.

Detailed Instructions

  • Create a Python script file ldr_module.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-ldr-module import RPi.GPIO as GPIO from time import sleep # Set the Raspberry Pi GPIO pin number connected to the DO pin of the ldr light sensor module DO_PIN = 7 # Set the GPIO mode and configure the ldr light sensor module pin as INPUT GPIO.setmode(GPIO.BCM) GPIO.setup(DO_PIN, GPIO.IN) # Initialize the previous state variable with the current state prev_light_state = GPIO.input(DO_PIN) try: while True: # Read the current state of the ldr light sensor module light_state = GPIO.input(DO_PIN) # Check for a state change (LOW to HIGH or HIGH to LOW) if light_state != prev_light_state: if light_state == GPIO.LOW: print("Light detected!") else: print("Light is gone!") # Update the previous state variable prev_light_state = light_state # Add a small delay to prevent continuous readings sleep(0.1) except KeyboardInterrupt: # Clean up GPIO settings when Ctrl+C is pressed GPIO.cleanup() print("\nExiting the program.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 ldr_module.py
  • Cover and uncover the LDR light sensor module by your hand or something
  • See the result on the Terminal.
PuTTY - Raspberry Pi
Light detected! Light is gone!

Raspberry Pi Code - Read value from AO pin

To read the value from the AO pin, you need to use ADS1115 ADC Module since Raspberry Pi does not have any ADC pin. See how to use ADS1115 ADC Module with Raspberry Pi

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!