Sample Code for MakeCode - Sound
Last revision 2026/01/26
This article explores the principles of sound waves, auditory processing, and sound sensors, providing sample code for micro:bit to dynamically measure and display sound strength, enhancing programming skills through engaging projects.
Principle
1.The principle of sound
Sound is a sound wave produced by the vibration of an object. It is a fluctuation that transmitted by a medium (air or solid, liquid) and is perceived by a person or animal's auditory organ.
When playing an instrument, tapping a door or a tabletop, their vibration will lead to a rhythmic vibration of the air, causing the surrounding air to change dense, which will form a dense vertical wave, so sound waves produced and this phenomenon will continue until the vibration disappears.

2.How could we hear sound?
The occurrence and transmission of sound in nature is a kind of vibrational sound wave. The vibrational sound wave received through the outer ear, passed through the ear canal to the eardrum. The vibration of the eardrum causes the hammer bone, the shin bone, the tibia three listening bone in the inside of the ear to vibrate one by one, and the sound wave turns into a solid vibration.
Then, the "inner ear" cochlear receives vibrations from the middle ear and activate the body's most important auditory receptor the Corti’sorgan to turn vibration into nerve impulses.
Neural impulses along the "listening nerve" to the brain's auditory center, the auditory center to complete the tasks of auditory information, analyzing, processing, integration, processing, hereafter the auditory produced.
Sound sources → sound waves (gas vibrations) → ear profiles (collect sound waves) → outer ear canals (sound wave conduction) → the eardrum (convert sound waves into solid vibrations) → cochlear (convert vibrations into nerve impulses) → listening nerves (pass nerve impulses) → the auditory center of the brain (integrate information and produce hearing).

3.Principle and Applications of Sound Sensor
We have already known the principle of sound and how the ear hears sound. But how does sound sensors work? Where will sensors be used?
Introduction and Principle
A sound sensor is a sensor that detects the size and frequency of a sound by the vibration of the vibratory membrane and converts the vibration into an electrical signal. Through a series of conversions and analysis of electrical signals by chip, the strength of sound can be detected intuitively.
Application
Sound sensors are widely used in daily life, industrial agriculture and military. E.g. KTV, command traffic shouters, mobile phones, walkie-talkies…

Preperation
So, which is the sound sensor on the natural science board? Let's see.

Here we can see a small sound sensor in the board which with a logo speaker. Small but sensitive, it can grasp the clapping, speaking sound and input the sound to micro: bit to calculate.
For more information, refer to the Getting Started guide.
Sample Code 1
Let's program to get the sound strength.

Result 1
In the above program, we convert the sound to the moving position of the micro: bit dot matrix, the louder the sound, the more the dot matrix lighting up scrolls to the right.
Sample Code 2
Programming as follows.

Result 2
Record the number of 1 to 9 pat times and display the corresponding number on the dot matrix, and display the current sound strength in real time on the OLED screen. In this program, you will learn how to use the multithreaded programs as well as to judge and use sound strength.
Conclusion
Through the learning above, we have learned the following:
- The definition and principle of sound.
- How people hear sound.
- The principle and application of sound sensors.
- Programming to dynamic display the sound strength on the dot matrix screen.
- Programming to make an interesting applause number recorder.
Was this article helpful?
