Welcome to Tiny:bit Pro repository
10.Sound sensor
Learning goal:
This lesson learns how to use sound sensor by Python programming.
The position of the Voice sensor module in the robot.
! ! ! Note: In this experiment, we need to install the jumper cap in the position shown below.
Code:
1) Import the library needed for this lesson from micro:bit, display is used for dot matrix display, Image calls the built-in image, pin12 is the pin of the body colorful lights, neopixel drives the body colorful lights, and tinybit controls the car.
display.show (Image.HAPPY): Display a smile on micro:bit dot matrix.
item = 0: Declare item as a global variable, and initialize it to 0.
voice = tinybit.getVoicedata():Read the data of the sound sensor and save it to the variable voice.
If the variable voice> 100, it means that the light needs to be switched. First, a random number of 1 to 6 is generated and stored in the variable item. Then the color of the front RGB searchlight and the body colorful light are randomly set.
If the variable voice> 100, it means that the sound sensor detects the signal, and a random number of 1 to 6 is generated and stored in the variable item. If the test response is not sensitive in the experiment, you can change the number 100 to a smaller value;
Then judge the value of item. Different values correspond to different patterns and are displayed on the micro:bit dot matrix.
According to the hardware interface reference manual, the sound sensor connect to P1 of micro:bit.

Programming and downloading:
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, and the last line must be a space.
2.You can click the “Check” button to check if our code has an error. If a line appears with a cursor or an underscore, the program indicating this line is wrong.
3.Click “REPL” button,check whether the tinybit library has been downloaded. If not, please refer to the [preparation before class]---> [Python programming]
4.Click the “Flash” button to download the program to micro:bit board.
If the program is wrong or the experimental phenomenon is wrong after downloading, please confirm whether you have downloaded the Buildingbit libraryhex file we provided to the micro: bit board.
For the specific method of adding library files, please refer to 【1.Preparation before class】---【Python programming】
Experimental phenomena:
After download is complete, open the power switch. Micro:bit will display a smile on dot matrix, we need to put robot car on the desk.When we Shooting table, the dot matrix will display different pattern.