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:

Raspberry Pi Pico 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

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
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 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.

Light Sensor Pinout

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.

How Light Sensor Works

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

The wiring diagram between Raspberry Pi and Pico Light Sensor

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.

""" 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-light-sensor """ from machine import ADC, Pin import time # Initialize ADC (Analog to Digital Converter) adc = ADC(Pin(26)) # GP26 is ADC0 on the Raspberry Pi Pico while True: # Read the input on analog pin ADC0 (value between 0 and 65535) value = adc.read_u16() # Read the 16-bit ADC value directly description = "" # We'll have a few thresholds, qualitatively determined if value < 655: description = "Dark" elif value < 13107: description = "Dim" elif value < 32768: description = "Light" elif value < 52429: description = "Bright" else: description = "Very bright" print(f"Analog reading: {value} - {description}") time.sleep(0.5) # delay for 500 milliseconds

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog reading: 10432 - Dim Analog reading: 9728 - Dim Analog reading: 11968 - Dim Analog reading: 12032 - Dim Analog reading: 62528 - Very bright Analog reading: 63232 - Very bright Analog reading: 63936 - Very bright
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.

※ 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!