Raspberry Pi Pico - LDR Module

This guide will show you how to use a Raspberry Pi Pico and an LDR light sensor to monitor and measure light levels. We will explore the following topics in detail:

  1. Connect the LDR light sensor module to a Raspberry Pi Pico UNF R4.
  2. Program the Raspberry Pi Pico to recognize light with the LDR light sensor's digital signal.
  3. Program the Raspberry Pi Pico to measure light intensity using the LDR light sensor's analog signal.
Raspberry Pi Pico LDR Light Sensor Module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×LDR Light Sensor Module
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LDR Light Sensor Module

The LDR light sensor module helps detect light or measure the amount of light around it. It provides two options: a digital output and an analog output.

Pinout

The LDR light sensor module comes with four pins:

  • VCC pin: Attach this pin to VCC (3.3V to 5V).
  • GND pin: Attach this pin to GND (0V).
  • DO pin: This is a digital output pin. It shows HIGH in the dark and LOW in the light. Adjust the potentiometer to change the sensitivity to light and dark.
  • AO pin: This is an analog output pin. The output value decreases in bright conditions and increases in dark conditions.
LDR Light Sensor Module Pinout
image source: diyables.io

It also features two LED lights:

  • A PWR-LED indicator lights up to show the power is on.
  • A DO-LED indicator displays the light status on the DO pin: it lights up when there is light and turns off in the dark.

How It Works

About the DO pin:

  • This module includes a potentiimeter to adjust the light threshold. When the surrounding light is brighter than the threshold, the output pin (DO) of the sensor is LOW and the DO-LED (a tiny light on the module) is off. If the light around is dimmer than the threshold, the sensor's DO pin is HIGH and the DO-LED lights up.

For the AO pin:

  • The AO pin reading varies depending on the amount of light.
  • The reading is lower when there is more light.
  • The reading is higher in darker conditions.
  • The potentiometer does not change the AO pin's value. It only adjusts the threshold for the DO pin.

This method allows you to change how sensitive the DO pin is with the potentiometer, while you can still get accurate light measurements from the AO pin.

Wiring Diagram

The light sensor module offers two outputs. You can use either one or both, depending on what you need.

The wiring diagram between Raspberry Pi and Pico LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Read value from DO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-ldr-module """ from machine import Pin import utime # For timing functions DO_PIN = Pin(0, Pin.IN) # The Raspberry Pi Pico pin GPIO0 connected to the DO pin of the LDR module while True: light_state = DO_PIN.value() # Read the digital value from the pin if light_state == 1: print("The light is NOT present") else: print("The light is present") utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico the LDR module according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Use your hand or an object to cover and uncover the light on the LDR sensor module.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

If the LED is always on or stays off even in daylight, you can adjust the light sensitivity of the sensor by turning the potentiiconductor.

Now, you can change the code to make an LED or a light switch on when light is detected, or to make a servo motor rotate. For further instructions and detailed steps, please refer to the tutorials at the end of this document.

Raspberry Pi Pico Code - Read value from AO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-ldr-module """ from machine import ADC, Pin import utime # For timing functions # Define the Raspberry Pi Pico pin pin connected to the AO pin of the LDR module AO_PIN = ADC(Pin(28)) # GPIO28 (ADC2) as an analog input pin while True: light_value = AO_PIN.read_u16() # Read the analog value (0-65535) print(light_value) # Print the analog value utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Please follow these instructions one by one:

  • Copy the above code and open it in the Thonny IDE.
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Cover the LDR light sensor with your hand or an object and then remove it.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 580 584 584 2289 2713 3782 3826 4006 4010 4050 4054 2581 2185 1385 688
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!