Play a piece of music

Author: Yisong Xu, Si Xu

When an onboard button is pressed, the onboard buzzer will play Little Star.

Hardware requirements:

mBot/mCore Control Panel


Upload the program to Arduino

Example program download:

Play a piece of music.sb2


Example programs

Script description

This block indicates that if  is triggered, the following block scripts will be executed; if Event  is not triggered, the following block scripts will not be executed. In this example the following scripts are triggered when th onboard button is pressed.
Define the tone and rhythms of the buzzer.

Knowledge points
Point 1 Play the tone block
There are two drop-down menus in block  defining tone and rhythm respectively.
1) C/D/E/F/G/A/B in the tone menu define the name of the tone, mapping to Do/Re/Mi/Fa/So/La/Ti of C major. The number behind a tone stands for different pitches, C4 for standard middle C, and C5 for higher octave and C3 for low octave.
2) one second/one eighth/full/double length in beat menu are length, full length for 1 and its one second is 0.5, and its double is 2 seconds, and so on.

                              Pitch name table

Point 2 Can I use  instead of ?
No. If you rewrite the script as shown in the following figure and you use “If” and “then”, the program will quickly determine whether the on-board button is pressed; if not, it will proceed automatically without waiting until the condition is true. So you cannot achieve the example effects when you make the replacement. To achieve the example effects, you need to coat  with  block.

The example effects cannot be achieved when you make the replacement.

Extended tasks
Task 1 
Refer to Little Star and complete the other segments of the music.

Task 2
Refer to other music notations and write more music programs, such as “Are You Sleeping”.


Attached -mCore main control board buzzer and onboard button diagram



This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.