micro:bit basic tutorial - sound-controlled small light production

29/09/2022
In the last issue we wrote a basketball scorer program, today we are going to learn to use sound sensors to simulate a sound switch, the real meaning of light control, which we will learn in a later course.

Sound sensors

There are a few important concepts to understand about sound sensors, namely loudness and threshold

Loudness

In micro:bit, we use a total of 256 levels from 0-255 to indicate the size of the sound passing through the microphone. 0 means no sound, 255 means a particularly loud sound, so we have to think about the question, how loud is a loud sound, and how small is a small sound. Here it is necessary to introduce me to our other concept of threshold (yu) value.

Threshold

Threshold is a standard value that we set artificially, for example, to facilitate our understanding, the threshold is like our test scores in the passing line, if it is greater than 60 points to pass, below 60 points is to fail, then 60 is our threshold to pass, the same reason we can get.

60 points, the threshold of passing.
80 points, the threshold of good.
90 points, the threshold of excellence.
Why set the threshold, it is because after setting the threshold, our complex problem will become two parts, below the threshold, above the threshold of two parts, for our sound-controlled small lights, above the threshold on, below the threshold on off. After understanding the loudness and threshold, we can conclude that the basic steps to get the sound of our sound-controlled lights are

Get the sound loudness
Set the sound threshold
So let's take a look at what blocks we have available.

Getting to know the blocks

The micro:bit has several blocks for the built-in sound sensor, namely

Reading sound loudness

Using this block we can directly read the loudness of the sound passing through the microphone.

To set the threshold for the sound loudness, the corresponding event is generated.

This block has two drop-down menus, Loud and Quiet. With these two blocks, we can respond to the sound level in a specific way.

Program design

For our project, we need to find a suitable sound level to light our lamp, so we use an infinite loop to monitor the loudness of the sound in our environment so that we can set a suitable threshold.

After finding the right threshold, we need to set the sound threshold, and since it is to light our lamp, we set the threshold here, assuming our threshold is 80

Since we don't have a light here, we use √ and X to indicate when the light is on and off.

To be more realistic, we can't turn off the light immediately after it lights up, so we need to set a time to let the light go on for a while before it goes off.

Finally, we get a program like this

PI-SUPPLY is a professional component supplier, providing everything you need for your component project. We also stock a huge selection of Raspberry piArduinomicro:bitrock pi and nvidia manufacturer accessories from your favorite brands.