Figure 1 Obstacle Avoidance Robot Diagram
:::{Note}
When assembling the patrol mode, you can remove the main structure as shown above, and then install the ultrasonic sensor onto the robotic arm.
:::
* **Step 1: Assemble ultrasonic sensor**
* **Step 2:Connect ultrasonic sensor to port 2 of micro:bit expansion board**
### 5.1.2 Hand Chasing
* **Preparation**
(1) Connect ultrasonic sensor to port 2 of expansion board.
(2) The connection port of motors. Connect motor cable to the motor port on the same side as motor.
* **Learning Objective**
(1) Learn about the creation and assignment of variables, as well as the use of Boolean instruction blocks.
(2) Program Qtruck to achieve an intelligent following effect.
* **Project Logic**
When the distance is greater than the value set in program between Qtruck and hand, Qtruck moves closer to hand at certain speed. When it is less than 15cm, it will move away from hand at a certain speed.
The ultrasonic sensor is equipped with two transducers, one for transmitting and one for receiving ultrasonic waves. When measuring distance, the module automatically emits eight 40 kHz square wave pulses to detect whether an echo is returned. If an echo is detected, the sensor outputs a high-level signal. The duration of this high-level signal corresponds to the time it takes for the ultrasonic waves to travel to the object and bounce back.
Formula: measured distance= (the duration of high level\*sound velocity(340M/S))/2
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click
|
| Create a variable named `distance` and assign a value of 0. |
|
|
| Obtain the distance between ultrasonic sensor and obstacle. |
|
|
| If two inputs are True, the result is True. |
(3) Coding Block Combination
:::{Note}
The value of **"distance"** in program can be adjusted according to the actual situation.
:::
* **Project Outcome**
Connect micro:bit board only to your computer. After the program is transferred to micro:bit, insert it into expansion board. Turn on Qtruck and place your hand in front of ultrasonic sensor. When move your hand away from Qtruck, it goes forward. When move your hand close to it, it goes backward.
### 5.1.3 Cross Maze
* **Preparation**
(1) Connect ultrasonic sensor to port 2 of expansion board.
(2) The position of servos on Qtruck.
* **Learning Objective**
(1) Learn about the ultrasonic sensor and grasp the corresponding coding block.
(2) Program Qtruck to implement intelligent obstacle avoidance.
* **Project Logic**
Ultrasonic sensor can measure distances in different directions. When the measured distance is less than the set value, Qtruck will stop. Then detect the distance between the left and right sides. By comparing the distance between two sides, Qtruck will turn and continue to move forward.
The ultrasonic sensor is equipped with two transducers, one for transmitting and one for receiving ultrasonic waves. When measuring distance, the module automatically emits eight 40 kHz square wave pulses to detect whether an echo is returned. If an echo is detected, the sensor outputs a high-level signal. The duration of this high-level signal corresponds to the time it takes for the ultrasonic waves to travel to the object and bounce back.
Formula: measured distance= (the duration of high level\*sound velocity(340M/S))/2
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click
|
| Set the rotation angle and range of servo 1 to 8. |
|
|
| If meeting the judgement condition, statement is executed. |
(3) Coding Block Combination
:::{Note}
The value of **"distance"** in program can be adjusted according to the actual situation.
:::
* **Project Outcome**
Connect micro:bit board only to your computer. After the program is transferred to micro:bit, insert it into expansion board. Turn on Qtruck, and it will stop when encounter an obstacle, then detect and compare the distance between left and right sides. Finally, Qtruck will make a turn in the direction of the greater distance and then move forward.
### 5.1.4 Obstacle Detection
* **Preparation**
(1) Connect ultrasonic sensor to port No.2 of expansion board.
(2) Prepare a small carton box
* **Learning Objective**
(1) Learn about the principle of ultrasonic sensor and coding blocks.
(2) Learn the coding blocks of music.
(3) Program Qtruck to turn implement intelligent turning.
* **Project Logic**
Program Qtruck to find the nearest obstacle and then turn its head in the corresponding direction.
The ultrasonic sensor is equipped with two transducers, one for transmitting and one for receiving ultrasonic waves. When measuring distance, the module automatically emits eight 40 kHz square wave pulses to detect whether an echo is returned. If an echo is detected, the sensor outputs a high-level signal. The duration of this high-level signal corresponds to the time it takes for the ultrasonic waves to travel to the object and bounce back.
Formula: measured distance= (the duration of high level\*sound velocity(340M/S))/2
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click | Coding Block | Class | Function |
|---|---|---|
![]() |
![]() |
Obtain the distance between ultrasonic sensor and obstacle. |
![]() |
Set the rotation angle and range of servo 1 to 8. | |
![]() |
Set the speed of motor 1 and 2 ranging -100 and 100 to control Qtruck's movement. When it is 0, motor stops working. | |
![]() |
![]() |
Play a sound. |
The program is shown below:
:::{Note}
The **"distance"**, **"variable value"** and **"angle"** can be modified according the actual situation.
:::
* **Project Outcome**
Connect micro:bit board only to your computer. After the program is transferred to micro:bit, insert it into expansion board. Turn on Qtruck, and it will find the nearest obstacle and turn its head towards the obstacle.
## 5.2 Balancing Robot
### 5.2.1 Balancing Robot Assembly
:::{Note}
When you assemble Qtruck to a balancing pose, you can directly remove the gripper and ultrasonic sensor from transportation robot's primary pose, as the picture shown above.
:::
### 5.2.2 Robotic Arm Balance Control
* **Preparation**
The position of Accelerometer on micro:bit board, as the picture shown below:
* **Learning Objective**
(1) Learn about the working principle of accelerometer.
(2) Program Qtruck to achieve robotic arm self-balancing while tilting the body.
* **Project Logic**
The angle detection of the accelerometer is used to detect the current status of the Qtruck, and the angle value obtained from the detection is superimposed on the servo angle of the balance state to achieve the balance control of the robotic arm.
When the micro:bit is stationary or moving at a constant speed, the accelerometer only detects gravitational acceleration. If the micro:bit is shaken lightly, the acceleration from the shake is much smaller than gravitational acceleration and can be ignored. Therefore, when using the micro:bit, the primary focus is on detecting changes in gravitational acceleration along the x, y, and z axes as the device's orientation changes, and applying this data accordingly.
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click
|
| Obtain the rotation angle detected by accelerator |
|
|
| The values are rounded according to the numerical modification rules rounding. |
(3) Coding Block Combination
* **Project Outcome**
Connect micro:bit board only to your computer. After the program is transferred to micro:bit, insert it into expansion board. Turn on Qtruck, when it detects that the body is flipping forward and backward, it drives robotic arm to maintain a balancing pose.
## 5.3 Way-exploring Robot
### 5.3.1 Way-exploring Robot Assembly
Figure 1 Way-exploring Robot Diagram
:::{Note}
To set up the Way-exploring mode, first disassemble the main structure as shown above, then follow the steps below to install the components.
:::
* **Step 1: Install the claw bracket.**
Figure 2 Diagram of claw bracket installation
(1) Install ultrasonic sensor.
Figure 3 Diagram of ultrasonic sensor installation
### 5.3.2 Falling Detection
* **Preparation**
The position where ultrasonic sensor is assembled, connecting ultrasonic sensor to port 2 of expansion board.
* **Learning Objective**
(1) Learn about the ultrasonic sensor and its working principle, and understand how obstacle avoidance is achieved.
(2) Master the use of ultrasonic-related instruction blocks, and program Qtruck to achieve self-protection.
* **Project Logic**
Use ultrasonic sensor to detect distance and recognize the edge ahead based on the measured distance to prevent the robot from falling.
The ultrasonic sensor is equipped with two transducers, one for transmitting and one for receiving ultrasonic waves. When measuring distance, the module automatically emits eight 40 kHz square wave pulses to detect whether an echo is returned. If an echo is detected, the sensor outputs a high-level signal. The duration of this high-level signal corresponds to the time it takes for the ultrasonic waves to travel to the object and bounce back.
Formula: measured distance= (the duration of high level\*sound velocity(340M/S))/2
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click
|
| Obtain the rotation angle detected by accelerator |
|
|
| The values are rounded according to the numerical modification rules rounding. |
(3) Coding Block Combination
:::{Note}
**"Distance"** in program can be modified according to actual situation.
:::
* **Project Outcome**
:::{Note}
Please keep your eyes on robot when start the feature. You need to hold the robot immediately if it falls.
:::
Connect micro:bit board only to your computer. After the program is transferred to micro:bit, insert it into expansion board. Turn on Qtruck and place it on a round table, then it will detect the edge of table. Once the detected distance get larger abruptly, Qtruck will move back.
## 5.4 microbit Lesson
### 5.4.1 Introduction to micro:bit
* **Introduction**
Being led by a well-known English TV station BBC, Microbit is jointly developed by Microsoft, Samsung, ARM and Lancaster University, UK, which gears to adolescents' programming education. As you can see, the size of micro:bit merely accounts for half of the bank card. Although the size is small, many rich functional electronic modules have been well equipped on it. From a technical perspective, the micro:bit development board is essentially a microcontroller based on the ARM architecture. In terms of features, this board is very suitable for beginners, especially elementary and middle school students. It is easy to program, supports block-based coding, and also offers options for JavaScript, Python, and other programming languages.
* **Control System Diagram**
:::{Note}
As shown in the figure below, it mainly consists of onboard modules including an accelerometer, programmable buttons, pins, LED matrix, USB port, Bluetooth, radio, and an electronic compass.
:::
(1) The front side of micro:bit board:
(2) The back side of micro:bit board:
* **Introduction to Module Function**
(1) Bluetooth antennas: micro:bit has nRF51822 processor and comes with Bluetooth 5.0 and 2.4G Hz RF antennas. Bluetooth is used to send signal to communicate between app and micro:bit, which can download program or connect to micro:bit.
(2) Compass and accelerometer: The accelerator and compass are combine in a module. micro:bit adopts Freescale `MMA8653FC` three axes (X,Y, Z) accelerometer. Acceleration describes how quickly an object's velocity changes. By measuring the acceleration caused by gravity, this module is designed to measure and calculate related physical quantities. In the micro:bit, it is mainly used to detect changes in gravitational acceleration along the three axes.
The electronic compass, also known as a magnetometer, can detect the strength of the Earth's magnetic field and also function as a digital compass to determine direction. It is an important component of an Attitude and Heading Reference System (AHRS).
(3) 5\*5 programmable LED dot matrix: Equipped with a 5\*5 LED dot matrix, micro:bit is able to display preset or custom numbers, English characters or simple graphics.
(4) Programmable Buttons: With two programmable buttons, micro:bit can execute the corresponding operation by pressing button, which is customizable.
### 5.4.2 Breathing Light
* **Preparation**
The position of dot matrix on micro:bit board, as the picture shown below:
* **Learning Objective**
(1) Learn about micro:bit dot matrix and related coding blocks.
(2) Program Qtruck to implement gradient pattern on micro:bit.
* **Project Logic**
Equipped with a 5\*5 LED dot matrix, micro:bit is able to display preset or custom numbers, English characters or simple graphics.
* **Program Writing**
(1) Coding Block Learning
| **Coding block** | **Class** | **Function** |
|:--:|:--:|:--:|
|
|
| Display the set pattern. |
|
|
| Obtain the brightness of display from 0 to 225. |
| 
|
| If the condition is met, the same sequence of operations will be executed. |
(2) Coding Block Combination
* **Project Outcome**
Connect micro:bit board to your computer using a USB cable and transfer program to micro:bit. Turn on micro:bit, then a gradient heart shape will display on micro:bit.
### 5.4.3 Fingertip Temperature Detection
* **Preparation**
(1) The position of processor on micro:bit board.
(2) The position of dot matrix on micro:bit board.
* **Learning Objective**
(1) Learn about the processor and related coding black.
(2) Reinforce the understanding and use of the LED matrix.
(3) Program to display temperature value on micro:bit.
* **Project Logic**
The main chip of the micro:bit includes an `nRF51822` processor, which is used to detect temperature. Therefore, the measured temperature is closer to the chip temperature itself and may differ from the actual ambient temperature.
* **Program Writing**
(1) Coding Block Learning
| **Coding block** | **Class** | **Function** |
|----|----|----|
|
|
| Set the displayed value |
|
|
| Obtain temperature in Celsius |
(2) Coding Block Combination
* **Project Outcome**
Connect micro:bit board to your computer using a USB cable and transfer program to micro:bit. Turn on micro:bit and check the temperature value on LED, then slightly press processor and check it again.
### 5.4.4 Roll the Dice
* **Preparation**
(1) The position of acceleration sensor on micro:bit.
(2) The position of dot matrix on micro:bit board.
* **Learning Objective**
(1) Learn about acceleration sensor and related coding blocks.
(2) Program micro:bit to implement random number display.
* **Project Logic**
Use accelerometer to detect the status of micro:bit and display a number for each detection.
When the micro:bit is stationary or moving at a constant speed, the accelerometer only detects gravitational acceleration. When the micro:bit is shaken lightly, the acceleration caused by the shake is much smaller than the gravitational acceleration and can be ignored. Therefore, during use, the micro:bit mainly detects changes in gravitational acceleration along the x, y, and z axes as its orientation changes, and applies this data accordingly.
* **Program Writing**
(1) Coding Block Learning
| Coding block | Class | Function |
|---|---|---|
![]() |
![]() |
Set the displayed number |
![]() |
Display the set LED pattern | |
![]() |
![]() |
Execute the program after completing a specific action (for example, shaking micro:bit) |
![]() |
![]() |
Return a random number between minimum and maximum value. |
* **Project Outcome**
Connect micro:bit board to your computer using a USB cable and transfer program to micro:bit. Turn on micro:bit and observe the number displayed on LED dot matrix after shaking micro:bit. Then you can press B key to clear the pattern.
### 5.4.5 Motion Trajectory
* **Preparation**
(1) The position of acceleration sensor on micro:bit.
(2) The position of dot matrix on micro:bit board.
* **Learning Objective**
(1) Learn about acceleration sensor and related coding blocks.
(2) Learn the creation of variables.
(3) Program micro:bit to implement position feedback.
* **Project Logic**
Use accelerometer to detect the status of micro:bit and display the detected result on LED doc matrix.
When the micro:bit is stationary or moving at a constant speed, the accelerometer only detects gravitational acceleration. When the micro:bit is shaken lightly, the acceleration caused by the shake is much smaller than the gravitational acceleration and can be ignored. Therefore, during use, the micro:bit mainly detects changes in gravitational acceleration along the x, y, and z axes as its orientation changes, and applies this data accordingly.
* **Program Writing**
(1) Coding Block Learning
| Coding Block | Class | Function |
|---|---|---|
![]() |
![]() |
Display the set arrow pattern |
![]() |
Display the set arrow pattern | |
![]() |
![]() |
Obtain the acceleration in mg. |
![]() |
![]() |
If the condition is True, execute the set statement. |
* **Project Outcome**
Connect micro:bit board to your computer using a USB cable and transfer program to micro:bit. Move micro:bit up, down, left or right, then observe the displayed pattern on micro:bit.
## 5.5 Microbit Expansion Board Lesson
### 5.5.1 Magnet Detection and Alarm
* **Objective**
When the micro:bit is close to the magnet, the LED matrix displays a sad face, and the expansion board buzzer sounds. When the micro:bit moves away from the magnet, the LED matrix displays a smiley face.
* **Project Principle**
The micro:bit has a built-in electronic compass that measures magnetic field strength in all directions, allowing it to detect whether a magnet is nearby and determine direction.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official micro:bit website, which requires internet access to open.
:::
* **Project Outcome**
(1) When there is no magnet, a smiley face is displayed.
(2) When a magnet approaches, a sad face is displayed and a "beep beep beep" sound is played.
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--: | :--: | :--: |
|
|
| Get the magnetic field strength |
|
|
| Display a preset icon on the LED matrix |
|
|
| Make the buzzer play a sound with a specified duration and pitch |
(2) Program Analysis
[Source Code]()
① Use the micro:bit’s electronic compass to measure magnetic field strength. When approaching a magnet, the field strength exceeds 400, indicating that a magnet has been detected, and the corresponding code blocks are executed.
② When no magnet is detected, another set of code is executed to display a smiley face.
### 5.5.2 Electronic Candle
* **Objective**
Display a candle flame pattern on the LED matrix of the micro:bit. When blow into the microphone, the stronger you blow, the dimmer the brightness becomes. When it reaches a certain threshold, the flame goes out and then gradually returns to full brightness.
* **Project Principle**
There is a sound sensor on the micro:bit expansion board, which acts like a microphone. It receives sound waves and detects the intensity of the sound.
The sound intensity ranges from 0 to 255, where 0 means no sound.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official micro:bit website, which requires internet access to open.
:::
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--: | :--: | :--: |
|
|
| Obtain the volume detected by microphone, ranging from 0 to 225. |
|
|
| Convert an input from one range to another. For example, converting 50 from a value between 0 and 100 to a value between 0 and 1000. The result obtained is 500. |
|
|
| Change the index value from 0 to a set value and execute the program in module. |
(2) Program Analysis
[Source Code]()
① When powered on, it displays a candle icon on the LED.
② Map sound and brightness in a corresponding relationship: as the sound gets louder, the brightness gets lower. Sound levels from 0–255 map to brightness from 255–0.
③ When brightness is below 10, record the "brightness < 10" count, and when it is above 10, reset the count.
④ If the brightness is below 10 for two consecutive loops, the program inside will run, simulating the candle being blown out.
⑤ Gradually increase brightness from 0 to 255 and display it on the LED. This makes the candle slowly light up again.
### 5.5.3 Night Alarming
* **Objective**
When the brightness is below a certain threshold and button A is pressed, the buzzer sounds an alarm and the LED matrix flashes.
* **Project Principle**
The micro:bit provides a brightness level command, which allows us to get the brightness through the LED matrix. The light sensor is actually installed inside the LED matrix.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official micro:bit website, which requires internet access to open.
:::
* **Project Outcome**
When the brightness is below a certain value and button A is pressed, the buzzer alarms and all LEDs on the LED matrix start flashing.
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--: | :--: | :--: |
|
|
| Obtain the volume detected by microphone, ranging from 0 to 225. |
|
|
| If the key is pressed. |
(2) Program Analysis
[Source Code]()
① When the detected brightness is less than 5, the code the code in the block below will run. The comparison value should be adjusted according to your environment.
② When button A is pressed, the code in the block below will run.
③ The LED matrix will light up completely and make a sound, then turn off completely and make another sound. This cycle repeats twice before moving to the next main loop.
### 5.5.4 Play Music
* **Objective**
Press button A on the micro:bit to play music. Press it again to switch to a different song. Press button B to speed up the music playback tempo. Press buttons A and B together to restore the original tempo.
* **Project Principle**
The micro:bit expansion board has a buzzer that can be controlled directly using the micro:bit’s built-in music commands.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official
micro:bit website, which requires internet access to open.
:::
* **Project Outcome**
When the brightness is below a certain value and button A is pressed, the buzzer alarms and all LEDs on the LED matrix start flashing.
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--: | :--: | :--: |
|
|
| Play music |
|
|
| Change the tempo of the song by the specified value |
|
|
| Execute the program within module when press the specific key |
(2) Program Analysis
[Source Code]()
① When button B is pressed, increase the tempo by 20 to speed up the music playback.
② When buttons A and B are pressed together, fix the tempo at 120 and reset the song index to 0, restoring the original tempo.
③ When button A is pressed, run program as following. First increase the song index by 1 and select the corresponding song.
④ Then, check the current song index: If it’s 1, play the "Birthday Song", if it’s 2, play "Wedding", if it’s 3, play "Power down", then reset the song index to 0.
⑤ Resetting the song index to 0 ensures that next time button A is pressed, the index increments to 1 again.
### 5.5.5 Birthday Celebration
* **Objective**
When a sound louder than a certain threshold is detected, play the birthday song and scroll "I ❤ U" on the LED matrix. Normally, a heart icon is displayed.
* **Project Principle**
The micro:bit expansion board has a buzzer that can be controlled directly using the micro:bit’s built-in music commands.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official micro:bit website, which requires internet access to open.
:::
* **Project Outcome**
When a sound louder than a certain threshold is detected, play the birthday song and scroll "I ❤ U" on the LED matrix. Normally, a heart icon is displayed.
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--: | :--: | :--: |
|
|
| Obtain the volume ranging from 0 to 225 |
|
|
| Play music |
|
|
| A pattern is scrolled to display on LED dot matrix |
|
|
| Create a pattern |
(2) Program Analysis
[Source Code]()
① First, it checks whether the external sound level is greater than or equal to 50. If true, it means no music is currently playing. At the same time, check whether the "playing" variable is equal to 0. If both conditions are met, set the `playing` variable to 1 and start playing the birthday song.
After downloading the program, as soon as you make a sound above the threshold, the micro:bit will play the birthday song.
② When the melody starts playing, the code blocks inside the orange block are executed. At this time, the "playing" variable is set to 1, and the program continuously scrolls the "I ❤ U" pattern.
③ When the melody stops playing, the variable is set back to 0, and a heart icon is displayed.
### 5.5.6 Sound Activated Light
* **Objective**
Obtain the current volume value using the sound sensor. When the volume exceeds the set value, randomly change the color of the RGB light.
* **Project Principle**
There is a sound sensor on the micro:bit expansion board, which acts like a microphone. It receives sound waves and detects the intensity of the sound.
The sound intensity ranges from 0 to 255, where 0 means no sound.
* **Program Download**
(1) Drag the program file from the corresponding folder into the MakeCode webpage.
(2) Connect the micro:bit controller to your computer using a USB data cable.
(3) Click the **"..."** next to the Download button and select **Connect Device** from the dropdown menu.
(4) In the pop-up window, click **Connect Device** again.
(5) Choose the device shown in the window as shown in the figure and click **Pair**.
(6) Once connected, click **Download** to transfer the program. During download, the yellow indicator light near the USB port on the back of the micro:bit will blink rapidly—do not disconnect or move the USB cable at this time to avoid failure.
(7) When the download is complete, the indicator light will stay solid, and a confirmation prompt will appear on the screen.
:::{Note}
The micro:bit drive should only contain the two files shown in the figure: DETAILS.TXT – contains micro:bit parameters, which can be ignored. MICROBIT.HTM – a link to the official micro:bit website, which requires internet access to open.
:::
* **Project Outcome**
When powered on, the board displays a ❤ icon. At the same time, it reads the current volume value from the sound sensor. When the volume exceeds the set value, it randomly changes the color of LED1 and LED2.
* **Program Brief Analysis**
(1) Main Blocks
| **Block Coding** | **Class** | **Function** |
| :--- | :--- | :--- |
|
|
| Obtain the volume ranging from 0 to 225 |
|
|
| Initialize Startbit |
(2) Program Analysis
[Source Code]()
① When the analog sound value is greater than 50, generate a random number from 0 to 9.
② Set the obtained random number as the color value of the RGB light.
## 5.6 Handlebit Control Lesson
### 5.6.1 Introduction to Handlebit
* **Introduction**
Handlebit is a development kit designed for learning with micro:bit. It not only has a **"cool"** appearance but also integrates buttons, a joystick, RGB lights, and other electronic modules on its main board. By combining these with additional expansion modules, Handlebit supports a variety of creative ideas and imaginative projects. Through programming, it can even remotely control other micro:bit products, making it a great helper for learning micro:bit programming!
* **Function Diagram**
Figure: micro:bit Board Ports, RGB Light 1, RGB Light 2, Right Joystick, Left Joystick, LEGO Expansion Holes, Power Switch, Motor, Buzzer, Button B1, Button B2
### 5.6.2 Handlebit Control
* **Getting Started**
(1) Location of the wireless module on the micro:bit.
(2) You need two micro:bit controllers and a Handlebit joystick from Hiwonder.
* **Learning Objective**
(1) Understand the location of the wireless module on the micro:bit.
(2) Learn the wireless command blocks.
(3) Get familiar with the joystick control buttons.
* **Project Logic**
The micro:bit board has an integrated wireless transmitter and receiver module, which allows wireless data transmission by using its internal module. Each button on the Handlebit joystick corresponds to a number. When pressed, it sends that number wirelessly. When Qtruck receives the number, it executes the corresponding action based on the received number. This **"send and receive"** process achieves wireless transmission and control.
* **Program Writing**
(1) Extension Package Adding Method
Online programming: click | Block Coding | Class | Function |
|---|---|---|
![]() |
![]() |
The device initializes. |
![]() |
Detect the button press status on the joystick. | |
![]() |
Broadcast the number wirelessly to any connected micro:bit in the group. | |
![]() |
![]() |
When the micro:bit receives the number wirelessly, it will execute the program below. |
![]() |
The sending program is shown below:
The receiving program is shown below:
* **Project Outcome**
First, download the **"[Handlebit-Transmitter.hex](../_static/source_code/Handlebit-Transmitter.zip)"** program to the micro:bit controller of the Handlebit joystick. Next, download the **"[Qtruck-Receiver.hex](../_static/source_code/Qtruck-Receiver.zip)"** program to the micro:bit of the Qtruck robot. After the program is downloaded, insert to the corresponding expansion boards into the Handlebit joystick and Qtruck robot. After turning on the switch, you can control the Qtruck robot using the Handlebit joystick, allowing basic movements like forward, backward, and turning. Additionally, you can control the servo motors on the robotic arm.