sheen.bot Logo

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.

sheenbot∞ Light Sensor

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 forever block:
    • Drag a screen display text block to show the light sensor reading on the screen. It reads the value from LIGHT on P0 and prints it.
    • Add an if/else block to make a decision based on the light level.
    • Within the if block, use another READ LIGHT on P0 block and check if the value is greater than 4080.
    • If true, use the lampNumber 0 displayColor block to turn on Lamp 0 with a YELLOW color.
    • If false, use the close number 0 LED block to turn off Lamp 0.
  • 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