Posted on Leave a comment

LDR light sensor on Raspberry Pi Pico

Raspberry Pi Pico LDR light on off

In this tutorial we’ll connect a light sensor, also known as LDR (Light-Dependant Resistor) or photo-resistor, to a Raspberry Pi Pico. On the basis of a MicroPython script we’ll be able to to measure the intensity of the ambient light.

When you have completed this tutorial, you will be able to connect a photo-resistor to your Raspberry Pi Pico. You’ll also have the basic code to convert the output signal of the sensor to usable information in your MicroPython script.

  1. Prepare the hardware

    – First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.

    – Next you need an USB cable with micro-USB plug.
    – You also need a Raspberry Pi Pico of course. For this tutorial you need pin headers soldered to the GPIO-pins of your board.

    And finally you’ll need some extra components :
    – a breadboard (we are using a 400 points breadboard)
    – an analog light sensor (LDR or photo-resistor)

    Visit our shop if you miss any components.Raspberry Pi Pico breadboard LDR

  2. Get to know the photo-resistor

    The LDR (Light Dependent Resistor) has a variable resistance that changes with the light intensity that falls upon it. This allows the component to measure the intensity of the ambient light. The resistance of the LDR decreases when the intensity of the light increases.
    LDR photo-resistor

  3. Understanding the analogue-to-digital converter (ADC)

    The output of the LDR results in an analogue signal. As microcontrollers are digital devices, they don’t understand analogue signals. To transform our analogue signal to digital information we need an analogue-to-digital converter (ADC).analogue-to-digital converter
    Luckily, our Raspberry Pi Pico has it’s own ADC on board. And this ADC is connected to some digital inputs (labeled with ADC on the pinout card). So, we’ll need to be selective in the choise of the input pin for our LDR.

    The resolution of the Pico’s ADC is 12 bits. This means that the analogue signal will be transformed into a digital signal with a resoltion of 4096 units.

    Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.

  4. Setup the hardware part

    Raspberry Pi Pico LDR pinout– connect one end of the LDR to GP27 (=GPIO 27 or ADC1)
    – connect the other end of the LDR to a GND (ground) pin

    As we have to do with a kind of resistor, the direction doesn’t matter here.
    Raspberry Pi Pico LDR

  5. Write the code

    The aim is here to write a very basic script to get a numeric value that reflects the measured light intensity on a given moment.

    Now, open Thonny and write or paste following code in the IDE:

    from machine import Pin
    import time
    ldr = machine.ADC(27)
    while True:

    Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tabs”.

    MicroPython Pico LDR
    Some explanations about the code :

    from machine import Pin : to partially import the machine module to have access to the GPIO pins.
    import time : to import the time module. This will allow us to use time-related tasks.
    ldr = machine.ADC(27) : to configure the LDR-pin as an analogue input
    while True: is an infinitive loop (until we stop the program).
    print(ldr.read_u16()) : to read and print the measured value from the LDR. The ‘u16’ part translates the binary value in an unsigned 16-bit integer. So we can expect a result between 0 and 65535.
    time.sleep(2): wait for 2 seconds

  6. Run your script

    Now, it’s time to save your script. You can either save it on your computer or on your Pico board.

    Then, click on the Run button of the Thonny IDE. In the Thonny shell, a number should appear every 2 seconds. If you cover the LDR or switch off the light, the number should increase. That’s because the resistance of the LDR increases with less light. And with the Pico we’re actually measuring a Voltage. As the resistance increases, logically the voltage increases too. Raspberry Pi Pico LDR light on off

Congratulations! With this setup you can define the light intensity status of ambient light. Now you can implement a threshold in your script to process the result in an application where you want to control a light bulb for example. Have fun with it !

How useful was this post?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 28

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

How useful was this post?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 28

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

find out more products

Be the first to be informed about our latest tutorials and products by subscribing to our Newsletter respects your privacy. Read our privacy policy on how we handle your personal information.