Raspberry Pi Pico - Light Sensor
This guide shows you how to use the LDR light sensor with the Raspberry Pi Pico. Here's what we'll cover:
- How a light sensor functions.
- Steps to link a light sensor to a Raspberry Pi Pico.
- Creating a program for Raspberry Pi Pico to obtain readings from the light sensor.
If you are looking for a module with a light sensor, check the tutorial Raspberry Pi Pico - LDR Light Sensor Module.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Light Sensor
This guide explains how to use a light sensor called a photoresistor, or Light-Dependent Resistor (LDR), which helps determine and measure the brightness of the light around it.
Pinout
A photoresistor has two pins. Since it is a resistor, you do not need to distinguish these pins. They are identical.
How It Works
A photoresistor is a special kind of resistor that adjusts its resistance depending on the light it senses. When the light is bright, the photoresistor's resistance is very low. When it's dark or there's no light, the resistance is very high. By checking the resistance of the photoresistor, we can find out how bright or dark the light around it is. This helps us measure light levels in various places.
WARNING
The light sensor value gives you an idea of the brightness of the light, but it's not exact. Use it when you don't need precise measurements.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
This code checks the light amount using a photocell and describes how bright it is.
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 LDR light sensor to the Raspberry Pi Pico 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.
- Direct light onto the sensor.
- Check out the message in the Shell at the bottom of Thonny.
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.