MagicCode Lesson 08: Generating Tones
MagicCode Lesson 8

Generating Tones

Generate tones using the onboard buzzer

Components Required



In this example, you are learning to generate a tone using the onboard buzzer on the Magicbit.

Learning Outcomes

From this example, you’ll get an understanding about,
  • generating square waves


Piezo buzzers are commonly used in embedded systems to give audible tones. It works well with a square wave as input.


For this example, we use the piezo buzzer wired to pin 25 of the Magicbit.

Magicbit inbuilt Piezo buzzer

We could specify the frequency & duration of the tone with the “play tone, [Frequency] Hz, duration: [Duration] ms” block.

Drag and drop the following blocks from the block palette and create the following script.


Sample script


This program starts by clicking the green flag. The forever block continuously runs the code held inside the block. Frequencies 4186 Hz and 5274 Hz represent C and E notes, respectively. This program plays note C for 500ms and after that plays E note 500 ms continuously.” play tone, [Frequency] Hz, duration: [Duration] ms” block generates PWM corresponding to the given parameters.

Note: Create a program that plays one frequency when one push button on the board is pressed, and another frequency when the other push button when pressed.

Related Posts
Leave a Reply