Welcome to World of Module Sensor Kit repository
8.10 Color sorter
Note: Please use the double-headed data cable provided by us to connect the Micro:bit board and expansion board to the computer, otherwise it will not be possible to drive the servo due to insufficient power.
1. Learning target
In this lesson, we will use micro:bit board, building blocks and sensor modules to build a color sorter. It can distinguish red, green, blue three colors.
2. Servo calibration
Before assembling the building blocks, we need to use code to calibrate the servo to a fixed angle.
If calibration is not calibrated before using, it is easy to jam the servo during use and cause the steering gear to stall and damage the servo.
Calibration method:
2.1 Connect the brown line of the servo to GND (black), the red line of the servo to VCC (red), and the yellow line of the servo to IO on expansion board.
The left servo is connected to P1, the right servo is connected to P2. As shown below.
2.2 Then connect the computer to the Micro:bit board and expansion board through the double-head micro USB cable we provided.
2.3 Download the servo calibration code (Servo-calibration-color-sorter.py) to the micro:bit board.
2.4 When a "0" pattern is displayed on the dot matrix of the Micro:bit board, it means the servo be calibrated successfully.
3. Building blocks assembly
Please follow the steps we provide to assemble the building block models.
Pay attention to the installation direction of the servo when assembling, otherwise the servo will be damaged due to the wrong angle of the servo after running the program.
After the assembly is completed, please check the wiring of the servo as shown below.
4. About wiring
5. About code
# -*- coding: utf-8-*-# Encoding cookie added by Mu Editor from microbit import * import WOM_Sensor_Kit display.off() WOM_Sensor_Kit.WOM_servo360(pin1, 190) WOM_Sensor_Kit.WOM_servo360(pin2, 190) while True: if WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_red) == 255: if (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_green) < 20) and (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_blue) < 20): WOM_Sensor_Kit.WOM_rgb(1023, 0, 0) sleep(1000) WOM_Sensor_Kit.WOM_rgb(0, 0, 0) WOM_Sensor_Kit.WOM_servo360(pin1, 220) WOM_Sensor_Kit.WOM_servo360(pin2, 220) sleep(1000) WOM_Sensor_Kit.WOM_servo360(pin1, 190) WOM_Sensor_Kit.WOM_servo360(pin2, 190) sleep(1000) elif WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_blue) == 255: if (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_red) < 20) and (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_green) < 20): WOM_Sensor_Kit.WOM_rgb(0, 0, 1023) sleep(1000) WOM_Sensor_Kit.WOM_rgb(0, 0, 0) WOM_Sensor_Kit.WOM_servo360(pin1, 220) WOM_Sensor_Kit.WOM_servo360(pin2, 150) sleep(1000) WOM_Sensor_Kit.WOM_servo360(pin1, 190) WOM_Sensor_Kit.WOM_servo360(pin2, 190) sleep(1000) elif WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_green) == 255: if (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_red) < 20) and (WOM_Sensor_Kit.WOM_color(WOM_Sensor_Kit.WOM_blue) < 20): WOM_Sensor_Kit.WOM_rgb(0, 1023, 0) sleep(1000) WOM_Sensor_Kit.WOM_rgb(0, 0, 0) WOM_Sensor_Kit.WOM_servo360(pin1, 160) WOM_Sensor_Kit.WOM_servo360(pin2, 150) sleep(1000) WOM_Sensor_Kit.WOM_servo360(pin1, 190) WOM_Sensor_Kit.WOM_servo360(pin2, 190) sleep(1000)
6.Writing and download code
6.1 You should open the Mu software, and enter the code in the edit window, , as shown below.
Note! All English and symbols should be entered in English, use the Tab key (tab key) to indent and the last line must be a space.
6.2 You can click the “Check” button to check if our code has an error.
If a cursor or underline appears on a line, it indicates a syntax error, please check and modify. If there is no error in the program, the bottom left of the interface will prompt that there is no problem in detection.
6.3 Click the ‘REPL’ button to check whether the WOM_Sensor_Kit Python library has been downloaded.
If not, please refer to [Preparation before class] --> [Python Programming Guide] .
6.4 After the program is written, use a micro USB cable to connect the computer and the micro:bit board. Please click the ‘Flash’ button to download the program to the micro:bit motherboard (You need to click the ‘REPL’ button again to close the function of importing library files before you download the program).
6.5 If the download failed, please confirm whether the micro:bit is connected to the computer through the micro USB data cable, and confirm whether the WOM_Sensor_Kit Python library has been imported.
7. Experimental phenomena
After the program is downloaded successfully. We control UFO catcher movement and Gripping-release by rocker module.
We can place red, green, and blue blocks on the color sorter to allow the machine to sort.
Place a red block under the color recognition module on the platform. The color sorter will tilt to the left and the blocks will fall.
Place a green block under the color recognition module on the platform. The color sorter will tilt to the right and the blocks will fall.
Place a blue block under the color recognition module on the platform. The color sorter will tilt to the front and the blocks will fall.