In the previous lesson, we used the on-board RGB lights for an interactive light show. In this lesson, we'll design comprehensively with the on-board speaker and A/B buttons. Different buttons will control different types of music playback, creating a music player.
Project Objectives
Knowledge Objectives
1.Understand the difference between buzzers and speakers.
2.Learn the relation between frequency and sound.
3.Play audio on the UNIHIKER K10.
Practical Objective
Make a music player using buttons and the on-board speaker: When Key A is pressed, the UNIHIKER K10 plays musical notes; when Key B is pressed, it plays built-in music or music stored on the TF card.
Materials List

Preparations
Hardware
Connect the UNIHIKER K10 to the computer using a USB Cable.

Software
Open Mind+ 2.0, switch to“Upload Mode”, connect the UNIHIKER K10 as shown in the figure below, and load the UNIHIKER K10 library.

Hands-on Practice
Next, we'll use buttons to control the UNIHIKER K10 to play music from different sources.
Task 1: Play Musical Notes
First, we'll use Button A to control UNIHIKER K10 to play specific musical notes.
Task 2: Play Music
Then, we'll use Button B to control UNIHIKER K10 to play built-in music or music stored on the TF card, and display the playback status on the screen.
Task 3: Switch Music
Last, we'll use Buttons A/B to play the previous/next track respectively, and display the name of the currently playing music on the screen.
Task 1: Play Musical Notes
Code
The UNIHIKER K10 uses its on-board speaker for audio playback. Its location is shown below:

To play musical notes on the UNIHIKER K10, we need to use the "play tone (Low C/C3) for (1) beat" block under the "Audio" of UNIHIKER K10 , as shown below:

Meanwhile, to control music playback with buttons, we also need to use the "Button (A) (pressed)?" block, which is in the "Sensors" of UNIHIKER K10 .
The complete program code is shown below(twinkle twinkle little star" (1155661) as an example)):

Run the Code
Click the“Upload”button,wait for the upload to complete, then press Button A. The UNIHIKER K10 will play musical notes and stop automatically after playback.
Code Review
Category | Blocks | Function |
![]() | Plays note sounds of a specified pitch, and we can set the number of beats to play. |
Task 2: Play Music
Code
The UNIHIKER K10 supports two music playback methods: playing built-in music and playing music stored on the TF card.
Method 1: Play Built-in Music
Playing and pausing built-in music require the use of "play built-in music (DADADADUM) in background " and "Stop background playback" blocks under the "Audio" of UNIHIKER K10 respectively, as shown below:

Meanwhile, to control music playback with buttons, we also need to use the "Button (A) (pressed)?" block.
The complete program code is shown below:

Note: Set the "auto-clear" option for cached display text to "No" to prevent all content after the coordinates from being fully cleared to the background color, which would compromise the overall appearance.
Method 2: Play Music Stored on the TF Card
When playing music via a TF card, pre-save the desired music to the TF card first. The specific steps are as follows:

Playing and pausing music stored on the TF card require the use of the "play TF card audio ("music.wav") in background" and "stop TF card audio" blocks under "Audio" of UNIHIKER K10 respectively, as shown below:

The complete program code is shown below:

Run the Code
Click the“Upload”button, wait for the upload to complete, then press Button B. The UNIHIKER K10 will play built-in/TF card-stored music and stop playback after 4 seconds.

Code Review
Category | Blocks | Function |
![]() | Play built-in music in the background. | |
![]() | Stop playing the built-in playback music. | |
![]() | Play the specified audio file from the TF memory card in the background, currently only supports two-channel stereo audio in wav format. | |
![]() | Stop playing TF card audio. |
Task 3: Switch Music
Code
Switching music stored on the TF card essentially involves switching the "serial number" part in the file name. We can set a variable named "serial" and adjust its value using buttons to achieve this function. The specific logic is as follows (taking six music files as an example): 
The complete program code is shown below:

Run the Code
Click the“Upload”button, wait for the upload to complete, then press Button A/B to switch to the previous/next audio track for playback respectively. Meanwhile, the screen will display the name of the corresponding audio file.
Knowledge Base
Next, let's learn and summarize the hardware knowledge used in this lesson.
Buzzer and Speaker
On the UNIHIKER K10, the functions of both the buzzer and the speaker (loudspeaker) can all be realized by the on-board internal cavity speaker.
When the internal cavity speaker outputs fixed tones or simple sound sequences (e.g.Task 1), it acts as a buzzer. When it plays TF card music or other complex audio (e.g. Task 2), it functions as a speaker (loudspeaker).
When used as a speaker , the corresponding blocks are as follows–simply modify the name of the audio file to be played.

When using the internal cavity speaker as a buzzer, the corresponding blocks are shown below (left). You can adjust the pitch and beat of the notes. When doing so, the corresponding code parameters to modify in the code area are shown below (right). These two parameters control the pitch by adjusting the frequency and the beat by adjusting the playback duration.

Among them, 1 beat equals 8000, and values for other beats are calculated proportionally. The relationship between pitch values is complex, and we'll elaborate on it below.
The relationship between frequency and sound
1.What is Sound
Sound is a mechanical wave that requires a medium to propagate. For example, when a person speaks, their vocal-cord vibration makes the surrounding air (a gaseous medium) vibrate, and this vibration reaches another person’s ear like a relay race. When diving, one can hear shore sounds as sound travels through water (a liquid medium). However, sound can't travel in a vacuum due to the absence of a medium.
2.The relationship between frequency and sound
Frequency refers to the number of vibrations an object makes per second (the "vibration speed" of sound). Pitch is the human ear’s subjective perception of sound frequency.
The nature of sound is sound waves produced by object vibration. Frequency directly determines whether the sound we hear is high or low, and there is a causal relationship between them:
- Higher frequency → Faster vibration → Denser sound waves → Higher pitch (e.g., bird chirps, whistle sounds);
- Lower frequency→ Slower vibration → Sparser sound waves → Lower pitch (e.g., elephant roars, bass drum).
For example:

Among them, Middle C/C4 (frequency of 262) has a higher pitch than Low C/C3 (frequency of 131).
3.Frequency range of the human ear
The frequency range of sound audible to humans is 20 Hz to 20,000 Hz.Sounds below 20 Hz are called infrasound (e.g., earthquakes, elephant communication); they cannot be heard but can be felt.Sounds above 20,000Hz are called ultrasound (e.g., bats, ultrasonic teeth cleaning), which are also inaudible to the human ear.
4.Music playback process via TF card

Note: The UNIHIKER K10 currently only supports playing audio files in stereo WAV format. In addition, if an audio file fails to be read, you can refer to the link https://www.unihiker.com/wiki/k10/TFcard for revisions and adjustments.
Differences Between Built-in Music and TF Card Stored Music
Comparison Dimension | Built-in Music | TF Card-Stored Music |
Source | Pre-installed in the device firmware at the factory, serving as the official fixed configuration | External music files imported independently by users, supporting personalized selection |
Quantity & Types | Limited in quantity and fixed in types, only meeting the requirements of basic function demonstration | No fixed quantity limit (dependent on TF card capacity), covering music of any style and application purpose |
Modification & Update | Cannot be directly modified or newly added | Can be replaced, deleted, or newly added at any time via a computer |
Storage Location | Integrated in the device’s built-in storage chip | Stored in an external TF card, without occupying the device’s built-in storage space |
Applicable Scenarios | Default functions of the factory-programmed software (e.g.mode switch prompt tones, basic interactive feedback tones) | User-defined projects (e.g.AI voice-controlled playback of designated music, personalized audio triggered in IoT scenarios, soundtracks for creative programming works) |
In conclusion, the built-in music of the UNIHIKER K10 can only meet basic trial needs. In contrast, music stored on a TF card resolves the limitations of the built-in music, such as its small quantity and monotonous styles, allowing users to create distinctive projects like campus announcements and smart music boxes according to their own requirements.
Challenge Task
A small science exhibition will be held at the school. A dual-mode music on-demand player is required to meet two playback needs:
- When visitors press Button A, the full built-in "WEDDING" clip is played, and "Built-in music: WEDDING" is displayed on the screen at the same time.
- When Button B is pressed, the pre-stored music file "musicX.wav" (X=1-6) on the TF card is played randomly, and "TF card music: musicX" is displayed on the screen. Playback stops automatically after 6 seconds, and "Playback ended. Welcome again!" is shown when playback stops.
The reference program code is as follows:

















