Example Code for MicroPython-Light Intensity Reading
Last revision 2026/01/09
This project demonstrates how to use the DFR0026 Analog Ambient Light Sensor with MicroPython to read ambient light intensity and print the value. Users can learn how to use the ADC (Analog-to-Digital Converter) module in MicroPython to read analog sensor data.
Hardware Preparation
| Name | Model/SKU | Quantity | Purchase Link |
|---|---|---|---|
| MicroPython-supported Board (e.g., ESP32/ESP8266) | - | 1 | - |
| Analog Ambient Light Sensor | DFR0026 | 1 | DFR0026 |
| Jumper Wires | - | Several | - |
Software Preparation
- Development Tool: Thonny IDE (version 4.x or later). Download link: Thonny IDE
- MicroPython Firmware: Download the appropriate firmware for your board from MicroPython Downloads
- No additional libraries are required for this sample code.
Wiring Diagram
| MicroPython Board | Sensor |
|---|---|
| ADC0 | GREEN |
| 3.3V/5V | RED |
| GND | BLACK |
(Note: Refer to your MicroPython board's pinout for the exact ADC0 pin location.)
Other Preparation Work
- Flash the MicroPython firmware to your board following the instructions from MicroPython Documentation.
- Connect the DFR0026 sensor to the MicroPython board according to the Wiring Diagram.
- Open Thonny IDE and select the correct port and MicroPython interpreter.
Sample Code
from machine import ADC
import time
adc0=ADC(0)
while True:
print("adc0=",adc0.read())
time.sleep(0.1)
Result
Open the Thonny IDE shell. The light intensity around the sensor is different, the received data is also different.The more light, the bigger the data.
Was this article helpful?
