Welcome to Building:bit Superkit repository
3.4 Photosensitive robot
Photosensitive robot
Photosensitive robot1. Learning objectives2. Building blocks3. Sensor wiring4. Code analysis5. Write and download the program6. Experimental phenomenon
1. Learning objectives
In this course, we mainly learn how to realize light-sensitive robots through Python programming.
2. Building blocks
For the building blocks steps, please refer to the installation drawings or building blocks installation brochure of [Assembly Course]--[Light-controlled bipedal robot] in the materials.
3. Sensor wiring
The motor wiring on the left side of the car is inserted into the M1 interface of the Super:bit expansion board, and the black wire is close to the battery side;
The photosensitive module is connected to the P1P2 interface.

4. Code analysis
For the program of this course, please see the Photosensitive-robot.py file.
from microbit import *import WOM_Sensor_Kitimport superbitFirst, import the libraries needed for this lesson from microbit: the WOM_Sensor_Kit library is used for sensors; the superbit library is dedicated to the superbit expansion board.
xxxxxxxxxxwhile True:if WOM_Sensor_Kit.WOM_light_V2(pin1) >= 800:superbit.motor_control(superbit.M1, -255, 0)elif WOM_Sensor_Kit.WOM_light_V2(pin1) < 800:superbit.motor_control(superbit.M1, 0, 0)Read the value of the photosensor connected to pin0 in an infinite loop. When the light becomes stronger (value ≥ 800), the M1 motor reverses at the maximum speed (-255); when the light becomes weaker (< 800), the motor stops (the speed is set to 0)
5. Write and download the program
- Open the Mu software and enter the code in the editing window. Note! All English and symbols should be entered in English mode, use the Tab key for indentation, and the last line ends with a blank program.
- Click the thumb 'Check' button to check if there are any errors in our code. If a cursor or underline appears in a line, it means a syntax error. Please check and modify it. If there is no error, the lower left corner will prompt that there is no problem with the detection.

- Click the 'REPL' button to check if the Superbit library has been downloaded. If not, please refer to [Preparation before class] --> [2.4 Python Programming Guide].

- After the program is written, connect the computer and the microbit mainboard with a microUSB data cable. Please click the 'Flash' button to download the program to the micro:bit mainboard. (You need to click the 'REPL' button again to turn off the import library file function before you can download the program normally).

- If the download fails, please confirm whether the microbit is properly connected to the computer via the microUSB data cable and the Superbit Python library has been imported.
6. Experimental phenomenon
After the program runs successfully, if the photosensitive module detects light, the motor will drive the bipedal man to walk. If the photosensitive module is blocked by hand, the bipedal man will stop walking.