Welcome to Building:bit Superkit repository
2.8 Color recognition machine
Color recognition machine
Color recognition machine1. 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 implement the temperature and humidity reminder function through Python programming.
2. Building blocks
For the building blocks steps, please refer to the installation drawings of [Assembly Course]--[Color recognition machine] or the building blocks installation brochure in the materials.
3. Sensor wiring
The color recognition module is connected to the I2C (SDA, SCL) interface.

4. Code analysis
For the program of this course, please see the Color-recognition-machine.py file.
from microbit import *import neopixelimport WOM_Sensor_KitFirst, import the libraries needed for this lesson from microbit: WOM_Sensor_Kit library is used for sensors; neopixel is used to control RGB lights.
xxxxxxxxxxnp = neopixel.NeoPixel(pin12, 4)np.clear()np.show()Initialize the RGB light connected to the pin12 pin, clear and update.
xxxxxxxxxxwhile True:R = WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_red)G = WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_green)B = WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_blue)for pixel_id in range(0, len(np)):np[pixel_id] = (R, G, B)np.show()sleep(100)Continuously read the red, green, and blue channel color values in the environment in an infinite loop, apply the read RGB values to the 4 lamp beads, make the display of the lamp close to the recognized color, and refresh every 100 milliseconds.
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 whether 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 whether 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 microbit mainboard with a microUSB data cable, 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 connected to the computer normally via the microUSB data cable and the Superbit Python library has been imported.
6. Experimental phenomenon
After the program runs successfully, the color sensor recognizes different colors and the RGB light displays the corresponding color.