The cyber:bot tone block
Let’s test the piezospeaker using calls to the cyber:bot module’s tone block. True to its name, this block instructs an I/O pin to alternate high/low electrical signals at a specific frequency, allowing a piezospeaker to emit a tone.
The tone block allows you to specify the frequency with the freq parameter, and the duration of the tone in milliseconds with the dur parameter.

This piezospeaker is designed to play 4.5 kHz tones for smoke alarms, but it can also play a variety of audible tones and usually sounds best in the 1 kHz to 3.5 kHz range. The start-alert tone we’ll use is:

That will make P22 send a series of high/low signals repeating at 3 kHz (3000 times per second). The tone will last for 1000 ms, which is 1 second. The micro:bit will wait until the tone block is complete before moving on to the next command.
Example project: piezospeaker_test
This example project makes a beep when it starts running, then it sends the message “Waiting for Reset” scrolling across the micro:bit LED matrix. These messages will continue indefinitely because they are in the forever loop. If the reset button is pressed, the speaker will replay the sound.
- In a browser, go to makecode.microbit.org to open the micro:bit Makcode Editor.
- Make sure the cyberbot extension is added.
(See Add extensions to your micro:bit). - Set the name to piezospeaker_test and enter the project below.
(See Save & Edit Projects and Flash Projects with MakeCode Editor.) - Click Download.
(See Flash Projects with MakeCode Editor.)

- Set PWR to 1.
- Verify that the piezospeaker makes a tone for one second.
- Press and release the micro:bit module’s reset button to hear the tone again. The reset button is underneath the micro:bit, by the USB connector.
Try this: for loops for sound effects
It is easy to make sound effects by using a for loop and a range block from the support subcategory.
- Set the name to sound_effect and enter the project below.
- Click Download.

You should hear a series of tones that increase in pitch.
- Set PWR to 0.
Your Turn
Just for fun, modify the sound_effect project by varying the values in the range block’s start, end, and step parameters.
- Try making the tones cover a greater range of frequencies.
- Try making the tones vary only a little from one to the next, for a smoother sound.
- Try making the tones go from a higher pitch to a lower pitch, so it sounds like something is falling.
- Try making a project that sounds like a cricket.