Example Code for Raspberry Pi – Analog Light Sensor
Last revision 2026/01/20
This article guides users on connecting and using an analog light sensor with Raspberry Pi, detailing sensor features, setup instructions, and Python code execution for ambient light detection.
Introduction
Based on PT550 environmentally friendly photodiode, this light sensor can be used to detect the intensity of ambient light. It is usually used to produce interactive works that produce special effects with changes in light intensity. The entire module is connected to the IO expansion board with a 3P analog data cable. As long as the color is corresponding, it will not be inserted wrong, really convenient.
- Wider Operating Voltage: 3.3 V~5 V
- Standard fixing hole design, two 3mm fixing holes separated by 5cm.

This LCD screen can display the detected light intensity of the sensor, which is reflected by the size of the circle

Precautions
The layout of the new version of the analog sensor port has the following two improvements. Please refer to our blog "How to change the data line connector layout" instructions. When using the sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Wiring Diagram
- Power the Raspberry Pi on and install the Raspberry Pi expansion board correctly
- Connect the sensor to analog port 0 on the expansion board

Sample Code
-
Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’
sudo apt-get install build-essential python-dev python-smbus git -
Install the drive library and program. In the terminal, type the following instructions and press ‘Enter’
cd \~
git clone https://github.com/DFRobotdl/37\_Pcs\_Sensor.git
- Find Analog_Ambient_Light_Sensor.py in \37_Pcs_Sensor\9_Analog_Ambient_Light_Sensor, open and run it with Thonny Python IDE, you can see the printed corresponding value of the light


Was this article helpful?
