3.2.3 Sound-controlled Animal
3.2.3.1 Overview

Here we build a sound-controlled animal with a Micro:bit V2 board, a servo expansion board, a 360° LEGO servo and some LEGO parts. We program to control it to perform some movements through the Micro:bit V2 microphone.

3.2.3.2 Materials Required

3.2.3.3 Installation Steps





















































3.2.3.4 Code Blocks
1. “on start”: codes in it run only once.

2. “forever”: codes in it run repeatedly.

3. Set SK6812 RGB LED to gradient color within the range of 1~360.

4. if()…else…
When the condition in “if()” is satisfied, codes under “then” will execute; Or else, codes under “else” will run.

5. Read the sound level through the microphone in the Micro:bit V2 board, ranging from 0(quiet) to 255(noise).

6. The expression for making a judgment. When using it, two numerical values or variable modules that need to be judged should be added to the two ends of the block.


7. Delay.

3.2.3.5 Test Code
The threshold 150 in “if()” can be modified according to actual conditions.

⚠️ Special reminder: You can obtain the sample code through the following two methods.
Method 1: drag and drop code blocks to build the sample code
1. MakeCode programming environment
The online version of MakeCode: MakeCode editor
2. MakeCode extension library
⚠️ Special reminder: Copy and paste the link: https://github.com/keyestudio2019/pxt-creative-inventors-kit-master.git into the search box to import the library.

3. Write the sample code:

Method 2: download the sample code file directly
1. Download the code file
Click to download the code file: 3_3_3_Voice_Controlled_Animal
2. MakeCode programming environment
The online version of MakeCode: MakeCode editor
3. Load the sample code
Drag and drop the sample code to the MakeCode editor.


3.2.3.6 Test Result

Wire up and power on through Micro USB cable, and connect an external power source (four AAA-1.5V batteries installed in the battery box, ensuring sufficient power).
To download the sample code using an online browser, you need to send the downloaded “.hex” file to the Micro:bit V2 board. There are two ways as follows:
① Download through WebUSB
For browsers like Google Chrome / Microsoft Edge, you need to click “Connect Device” to pair the device first. After that, click “Download” to load the sample code to the Micro:bit V2 board.

② Download without WebUSB
For browsers like Safari / Firefox / Other, download and send the sample code to the Micro:bit V2 board.

After downloading the sample code, unplug the micro USB cable to the Micro:bit V2 board.
Turn the DIP switched to the RGB and ON.

If the sound level ≥ 150 detected by the microphone on the Micro:bit V2 board, the animal runs for 1s, these SK6812 RGB LEDs emit colorful light, and the 5×5 LED matrix displays
.
If the sound level < 150, the animal stops running, and these SK6812 RGB LEDs and the 5×5 LED matrix turn off.

