2.11 Light Sensor
A light sensor is an analog input device that measures the intensity of ambient light. It is commonly used to trigger actions based on how bright or dark the surroundings are.
![]()
Example Applications:
- Automatic brightness adjustment
- Smart lighting systems
- Day/night detection for robots
2.11.1 Example Code for the Light Sensor
The light sensor outputs an analog value (typically between 0 and 4095) based on the surrounding light. In this example, we check if the brightness exceeds a threshold (4080) to turn on a yellow lamp.
- Start by connecting the light sensor module to one of the available analog input pins using the provided Type-C cable.
- Select the correct pin on your controller (e.g.,
P0) where the light sensor is connected. - Inside the
foreverblock:- Drag a
screen display textblock to show the light sensor reading on the screen. It reads the value fromLIGHTonP0and prints it. - Add an
if/elseblock to make a decision based on the light level. - Within the
ifblock, use anotherREAD LIGHT on P0block and check if the value is greater than4080. - If true, use the
lampNumber 0 displayColorblock to turn on Lamp 0 with a YELLOW color. - If false, use the
close number 0 LEDblock to turn off Lamp 0.
- Drag a
- Explanation:
- The light sensor gives a value between 0 (bright) and 4095 (dark).
- This program reads the light level from pin P0 and displays it continuously.
- When brightness exceeds 4080, the lamp lights up yellow to indicate strong light.
- When it’s dimmer, the lamp turns off.
Figure 2.6.1a: Light sensor threshold logic to control Lamp 0 using block-based programming
