Example Code for micro:bit-Color Piano
Last revision 2026/01/14
This article guides readers through creating a color piano using micro:bit and HuskyLens, detailing hardware and software requirements, wiring setup, and the process of color detection, learning, and recognition, with sample code and project results.
Hardware Preparation
- HuskyLens: 1
- micro:bit: 1
- IO Extender for micro:bit v2.0: 1
- paperboard: 7
Software Preparation
Search https://github.com/DFRobot/pxt-DFRobot_HuskyLens or "HusktLens", then click on the search button, you will see the HuskyLens plugin, and then click to add it.

Wiring Diagram
Note: HuskyLens needs to be connected to the specific interface. The HuskyLens interface uses a 5V voltage supply to ensure stable operation, as shown in the figure:Other Preparation Work
-
Dial the function button to the left until the word "Color recognition" is displayed at the top of the screen.
-
Press and hold the "function button" to enter the secondary menu parameter settings.
-
Dial the function button until "Learn Multiple" is displayed, then short press the function button, and dial to the right to turn on the "Learn Multiple" switch, that is, progress bar turns blue and the square icon on the progress bar moves to the right. Then short press the function button to confirm this parameter.

-
Dial the function button to the left until "Save & Return" shows. And the screen prompts "Do you want to save the parameters?" Select "Yes" in default, now short-press the function button to save the parameters and return automatically.
-
Color Detection
Point the icon "+" in the center of the HuskyLens screen to the target color block, and a white box will appear on the screen, which selects the target color block automatically. Adjust the angle and distance of the HuskyLens to the color block so that the white box frames the entire target color block as far as possible.

-
Color Learning
After detecting the color, press the "learning button" to learn the first color, then release the "learning button" to end the learning, meanwhile, a message "Click again to continue! Click other button to finish" will be displayed. Please short press the "learning button" before the countdown ends if you want to learn other colors. If not, short press the "function button" before the countdown ends, or do not press any button to let the countdown ends. The color ID is consistent with the order of learning colors, that is: ID will be marked as "ID1", "ID2", "ID3" in order, and so on, and different colors correspond to different border colors.

-
Color Recognition
When encountering the same or similar color blocks, a colored frame with an ID will be automatically displayed on the screen and track the blocks, and the size of the frame is same as the size of the color blocks. A variety of different colors can be recognized and tracked at the same time, and different colors correspond to different border colors.

When there are multiple same or similar color blocks appearing at the same time, the other color blocks cannot be recognized, that is, only one color block can be recognized at each time.

Color recognition is greatly affected by ambient light. Sometimes HuskyLens may misidentify similar colors. Please try to keep the ambient light unchanged and use in a well-light environment.
Sample Code
As shown below:

Program Link:
https://makecode.microbit.org/_EXWfPW1AyHdm
Result
HuskyLens recognizes the different colors it has learned, and the buzzer plays the tone corresponding to the color. The tone and ID corresponding to the color are shown in the table below.
| Color | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|---|---|
| Tone | C | D | E | F | G | A | B |
| ID | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Was this article helpful?







