Labs / Bipes + MicroPython / 7 Sensors: Enable Programs to "Perceive the World"

7.10 Auto Turn on Lights When It Gets Dark? Detect Light Intensity with a Simple Module! —— GL5516-Based Light Intensity Sensor Module

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

Young makers, it's finally time to fit "eyes" to your development board! Today we're going to get to know the light intensity sensor module — it acts as the "visual cells" of the development board, capable of accurately detecting changes in the brightness of the surrounding light, letting the program know "whether it's bright or dark right now", and can even achieve smart functions like "automatically turning on the lights when it gets dark and turning them off when dawn breaks"~

1. Module Introduction

The core principle of the light intensity sensor (we use the GL5516 photoresistor type) is extremely simple — just remember one rule: light intensity and resistance value move in opposite directions, while the readout value moves in lockstep with light intensity.

Essence of the work: The resistance of the photoresistor in the sensor changes with the intensity of light ——

Key Technologies: The development board utilizes the ADC (Analog-to-Digital Conversion) function to convert the "voltage signal" output by the sensor into a numerical value ranging from 0 to 65535, so that the program can measure brightness using this "digital value"!

To make the most of a light intensity sensor, you mainly need to use these categories of blocks in combination:

  1. ADC input reading block: Core function! Reads the digital value corresponding to the light intensity;
  2. Conditional judgment block: For example, "If the value > 50000 (too dark), turn on the LED";
  3. Digital / PWM Output Blocks: Link with LEDs and buzzers to turn "sensing" into "action";
  4. Delay block: Controls the detection frequency (e. g., once per second) to prevent excessively frequent data fluctuations.

Let’s recap: What exactly is an ADC?

An ADC is a tool that stands for "analog-to-digital conversion" — the voltage output by a sensor is a "continuously varying analog quantity"(e. g., 0~3.3V), while a development board can only recognize "digital quantities"(e. g., 0~65535). The ADC acts as this "interpreter", translating voltage into digital values.

2. Wiring Instructions

7.10 Auto Turn on Lights When It Gets Dark? Detect Light Intensity with a Simple Module! —— GL5516-Based Light Intensity Sensor Module

For the sensor to "see" light, you must first connect the wires correctly! We will connect the light intensity sensor module to the AIN0 interface of the development board, and make sure to remember the specific pin correspondence:

Development Board Pins

Module Pins

Description

Pin 26

AIN

Analog Output Pin

Pin 27

NC

-

3. Example: Display the original light intensity value

Experiment Objective: Make the development board continuously detect the ambient light, and print the ADC raw value corresponding to the brightness on the terminal every 1 second, so as to intuitively observe the correspondence between "light intensity" and "numerical value".

Core Principle: Characteristics of the GL5516 photoresistor sensor:

7.10 Auto Turn on Lights When It Gets Dark? Detect Light Intensity with a Simple Module! —— GL5516-Based Light Intensity Sensor Module

Building Block Assembly Steps (Step-by-Step Guide for You):

  1. Infinite Loop: Select the repeat while true block → Continuously detect the brightness;
  2. Print prompt: Add print "Print the original ADC value" block → The prompt will soon display the brightness value;
  3. Read the sensor value: select the Read RPI Pico ADC Input block, set pin to Pin 31/ADC0/GPIO26 → read the voltage value of the photosensitive sensor (converted to a digital value);
  4. Print the value: Add the print block, associate it with the read ADC value → display the number corresponding to the brightness on the terminal;
  5. Delay: Add delay 1 seconds block → Check the brightness level once every 1 second.

After running the program:

7.10 Auto Turn on Lights When It Gets Dark? Detect Light Intensity with a Simple Module! —— GL5516-Based Light Intensity Sensor Module

XML files can be directly imported into the building blocks:

Light sensor.xml 2 KB

Tips

  1. Is the value jumping too drastically? Change the delay from 1 second to 0.5 seconds, or add the logic of "take multiple readings and calculate the average" in the code (for example, take 3 readings and use the median value), and the data will be more stable;
  2. Want a more intuitive way? Replace "print the value" with "display the value on the OLED", so you can view the brightness directly on the screen instead of staring at the terminal;
  3. Is the value always 0 or 65535? Check if the pins are connected incorrectly or if the sensor is broken~

✨ Interactive Quick Question

  1. Think about it: If you put the sensor in a pitch-black drawer, will the ADC value be close to 65535 or 0? (Answer: It will be close to 65535! The darker the environment, the higher the value~)
  2. Try this: Shine your phone's flashlight at the sensor, and the reading will suddenly drop; when you move the flashlight away, it will gradually rise again. It's super fun~

Summary

  1. Core Logic of the Sensor: The light intensity sensor relies on the photoresistor to vary its resistance value and output a voltage signal; the development board converts the voltage into a digital value via ADC, where a smaller value indicates brighter light and a larger value indicates dimmer light.
  2. Key points for reading: You must select the correct ADC channel (GPIO26 = ADC0), and using an infinite loop plus delay can achieve stable data reading;
  3. Broaden your mindset: After reading the value, you can implement the intelligent control logic of "turning on the LED when it's dark and turning off the LED when it's bright" by adding conditional judgment.

After mastering this basic example, you can try the "light-controlled LED" — add a conditional block: if the ADC value > 50000 (too dark), turn on the LED; otherwise, turn it off. Your development board will immediately be able to achieve the function of "automatically turning on the light when it gets dark", which is super rewarding!