How To Use A Photoresistor With Raspberry PI Pico

4.6
(12)

Last Updated on 2nd September 2023 by peppe8o

In this tutorial, I’m going to show you how to use a photoresistor with Raspberry Pi Pico. If you have instead a Foundation computer board, you can also use my tutorial to use Photoresistors with Raspberry PI computer board.

Projects running different actions in function of sun exposure usually need acquiring information on the light level as a trigger to execute specified tasks. Photoresistors with Raspberry PI Pico can provide light info by acquiring analog levels from its input pins.

With the new Pico microcontroller availability, as it includes also analog ports we have access to a number of projects using analog sensors. A useful case is with photoresistors.

The Photoresistor

photoresistor

A photoresistor (also known as photocell) is a Light Dependent Resistor (LDR). As the name suggests, these components act just like a resistor, changing their resistance in response to how much light is falling on it. Usually, photoresistors have a very high resistance in near darkness and very low resistance in bright light.

As this sensor works as a simple resistor, you will not get absolute light values (for example, in lumens). Compared to a reference voltage level (that is the 3.3 V provided by Raspberry PI Pico) you will read a value minor or equal to Vref, reduced proportionally to the light exposed to the photoresistor. With ADC module from MicroPython, this value will range between 0 and 65535 (16-bit max value) and we’ll transform it into a percentage (between 0% and 100%).

What We Need

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
amazon raspberry pi pico box
Amazon photoresistor box
Amazon Resistors box
Amazon Breadboard box
Amazon Dupont Wiring box

Wiring Diagram

Please find below the used wiring diagram:

raspberry pi pico photoresistor wiring diagram

Following picture show some cabling details:

raspberry pi pico photoresistor details 01
raspberry pi pico photoresistor details 02
raspberry pi pico photoresistor details 03

Step-by-Step Procedure

Prepare cabling according to the previous paragraph. Connect RPI Pico to Thonny (you can refer to my tutorial about First steps with Raspberry PI Pico). Download my picoPhotoresistor.py script on your computer and open it with Thonny.

Following paragraphs will describe my code line by line.

Required modules are imported first:

from machine import ADC, Pin
from time import sleep

Then, PIN number connected to photoresistor is associated to a variable. In this way our code improves both in readability and management:

photoPIN = 26

A function will handle all the (few) operations required to read light intensity and convert it into a percentage. This function requires only the PIN number connected to the photoresistor. This number is used at function start to initialize the micropython ADC object:

def readLight(photoGP):
    photoRes = ADC(Pin(26))

The current light value is read with classic micropython 16-bit reading function:

    light = photoRes.read_u16()

As 16-bit resulting reading will range between 0 and 65,535, the light value will be transformed in a percentage and finally returned as function result:

    light = round(light/65535*100,2)
    return light

Following rows are the main loop. In this infinite loop, we get light value with “readLight(photoPIN)” call. As it returns a numeric value and as print function requires a string, the str() method converts the number object in a string, so that it can be printed on shell. Final sleep keeps readings not too fast:

while True:
    print("light: " + str(readLight(photoPIN)) +"%")
    sleep(1) # set a delay between readings

Running the picoPhotoresistor.py Script

Run this script in your Thonny IDE (F5) and you will start seeing your readings printed on Thonny shell, as in the following:

>>> %Run -c $EDITOR_CONTENT
light: 10.67%
light: 21.49%
light: 21.37%
light: 29.06%
light: 33.55%
light: 33.6%

What’s Next

Interested to do more with your Raspberry PI Pico? Try to look at my Raspberry PI Pico tutorials for useful and funny projects!

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.6 / 5. Vote count: 12

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?