Example Code for micro:bit-Light Sensor Applications

Last revision 2025/12/19

This article presents example code for utilizing micro:bit light sensors in robotics, offering two projects where a robot car can either avoid or follow light, complete with preparation instructions and testing methods.

Hardware Preparation

  • Maqueen V5 Robot Car: 1 (Purchase Link: https://www.dfrobot.com/product-2937.html)
  • micro:bit Mainboard: 1
  • 3 AA Batteries: 1 set
  • Mobile Phone (with Flashlight): 1

The positions of the light sensors are shown as follows:

Software Preparation

  • MakeCode Platform: https://makecode.microbit.org/
  • Maqueen Library: Load via MakeCode Extensions (search for "dfrobot" and select "Maqueen")

Other Preparation Work

  • Ensure the micro:bit mainboard is properly plugged into Maqueen V5.
  • Install the batteries correctly.
  • Turn on the power switch.
  • Prepare a mobile phone with a flashlight for testing.

Sample Code

Example 1: The Robot That Avoids Light

Program Description:

  • This example demonstrates that if there is strong light on one side during the driving process of the car, it will turn to avoid the light and drive.
  • In actual tests, the flashlight function of the mobile phone can be used to shine on the light sensor to verify the program effect.
    Program Link: https://makecode.microbit.org/_4FtRXhemt0Af

Example 2: The Robot That Tracks Light

Program Description:

  • This example demonstrates that if there is strong light on one side during the driving process of the car, it will follow the direction of the stronger light to drive.
  • In actual tests, the flashlight function of the mobile phone can be used to shine on the light sensor to verify the program effect.

Result

  • Example 1: Car turns away from strong light.
  • Example 2: Car moves toward strong light.

Was this article helpful?

TOP