Example Code for Light Sensor – Light-Avoiding and Light-Chasing Robots

Last revision 2026/01/20

This project shows how to use light sensors on the Maqueen Plus V3 robot to create light-following or light-avoiding behaviors, with example code and basic setup instructions for educational robotics projects.

Introduction

The Maqueen Plus V3 features two built-in front-facing light sensors—one on the left and one on the right. These sensors measure ambient light intensity in real time. By comparing the readings from both sides, the robot can be programmed to either:

  • Avoid bright light sources (e.g., move away from a flashlight), or
  • Chase light sources (e.g., follow a moving lamp)

Both behaviors rely on a threshold-based comparison (default: 150 units). The vehicle lights are set to blue at startup for visual feedback.

Example 1: Light-Avoiding Robot

Introduction

This robot actively steers away from strong light sources. When a significant difference (>150) in light intensity is detected between the left and right sensors, the robot turns toward the darker side, effectively avoiding illumination.

Hardware Preparation

  • Maqueen Plus V3
  • micro:bit
  • USB cable
  • A controllable light source (e.g., flashlight or desk lamp)

Software Preparation

Wiring Diagram

All components are integrated onboard—no external wiring required.

Sample Code

View on MakeCode

Program Logic:

  • Set vehicle lights to blue on startup
  • Continuously read left (L) and right (R) light sensor values
  • If L > R + 150 → turn right (avoid left-side light)
  • If R > L + 150 → turn left (avoid right-side light)
  • If |L - R| ≤ 150 → move forward

Result

The robot navigates away from directed light, demonstrating photophobic behavior ideal for simulating "shy" or evasive robots.

Example 2: Light-Chasing Robot

Introduction

This robot exhibits phototropic behavior—it actively seeks and follows light sources. When one side detects significantly more light (>150 units higher), the robot turns toward that side to align with the brightest direction.

Hardware Preparation

  • Maqueen Plus V3
  • micro:bit
  • USB cable
  • A movable light source (e.g., phone flashlight)

Software Preparation

  • MakeCode Editor with Maqueen Plus V3 extension

Wiring Diagram

Fully integrated system—no additional connections needed.

Sample Code

View on MakeCode

Program Logic:

  • Set vehicle lights to blue on startup
  • Read left (L) and right (R) light sensor values
  • If L > R + 150 → turn left (chase left-side light)
  • If R > L + 150 → turn right (chase right-side light)
  • If |L - R| ≤ 150 → move forward

Result

The robot consistently steers toward the brightest area, mimicking sun-tracking or search-and-rescue behaviors in dynamic lighting environments.

Was this article helpful?

TOP