Welcome to Building:bit Superkit repository
3.8 Automatic safety door
Color recognition automatic door
Color recognition automatic door1. 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 make color recognition security automatic doors through Python programming.
2. Building blocks
For the building blocks steps, please refer to the installation drawings of [Assembly Course]--[Color-recognition automatic door] or the building blocks installation album in the materials.
3. Sensor wiring
The building blocks servo wiring is inserted into the Super:bit expansion board S1 interface, and the servo orange wiring is inserted into the yellow pin of S1.
The color recognition module is connected to the I2C (SDA, SCL) interface.

! Notes:
When taking the course related to the building block servo for the first time, we need to remove the blue building block installed on the servo and upload the program of this course to the micro:bit; then turn on the power switch of the Super:bit expansion board and wait for the building block servo to turn to the initial position; then, we can turn off the power, adjust the security automatic door to the open state, as shown in the figure below, and then install the blue building block. (If you have used the security automatic door and servo related program before, you can skip this step)

4. Code analysis
For the program of this course, please see the Automatic-safety-door.py file.
from microbit import *import WOM_Sensor_Kitimport superbitFirst, import the libraries needed for this lesson from microbit: WOM_Sensor_Kit library is used for sensors; superbit library is dedicated to superbit expansion board.
xxxxxxxxxxsuperbit.servo270_V2(superbit.S1, 0)display.show(Image.HEART)sleep(500)Initialize the servo to 0°, and the dot matrix displays a heart pattern.
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) if R >= 15 and R <= 65 and G >= 93 and G <= 109 and B >= 250 and B <= 255: superbit.servo270_V2(superbit.S1, 90) display.show(Image.HAPPY) sleep(2000) superbit.servo270_V2(superbit.S1, 0) sleep(500) else: display.show(Image.ASLEEP)Continuously read the red, green, and blue channel values in the environment in an infinite loop. If the detected color value meets a specific blue range (R/G/B falls within the specified interval), the target color is considered to be recognized, the servo turns to 90°, and the smiley icon is displayed. After staying for 2 seconds, the servo returns to 0° and pauses for 0.5 seconds. If the color does not match, the "sleeping" expression is displayed.
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 (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 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 the microbit mainboard with a microUSB data cable, and 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, the servo is initialized to 0°. When the color sensor recognizes blue, the door will open, and the microbit dot matrix will display "smiley face". It will close the door after two seconds. The door will not open for other colors, and the microbit dot matrix will display "asleep".