Example Code for micro:bit-Number Guessing
Last revision 2025/12/31
This article guides readers through setting up and coding a number guessing game using micro:bit, providing hardware/software preparation, wiring diagrams, and sample code to create an interactive project.
Hardware Preparation
- Math & Automatic expansion board for micro:bit, Quantity: 1
- micro:bit, Quantity: 1
- Buzzer, Quantity: 1
Software Preparation
Makecode graphical programming, Mind+ platform.
Makecode Library Address.
About how to install a software library?
Wiring Diagram
Buzzer is integrated on the board.
Other Preparation Work
Install the Makecode library from https://github.com/DFRobot/pxt-Math-Automatic.
About how to install a software library?
Sample Code

Number Guessing Example Code Download
Result
Touch the DFlogo key, and RGB LEDs flash twice, then the game begins. RGB 0~15 shines randomly for 15 times, meanwhile, micro:bit LED panel displays a countdown and buzzer makes sounds. Players guess which number the RGB LED will stop at, and press down that key before the end of countdown. If you guess right, the buzzer plays "correct beep sound" and LED panel displays the right number. The game goes on. Otherwise, the buzzer plays "game over sound" and LED panel shows the right answer. Game over. You have to touch the DFlogo key to restart. The micro:bit LED matrix will display times of guessing right first when you touch the DFlogo key again, then the game starts again.
Was this article helpful?
