< All Topics
Print

Micro:Bit Buzzer Music Tutorial

Tutorial Aim:

To create a musical melody using the different tones on the active buzzer

Requirements:

This tutorial makes use of the Micro:bit Electronics Learning Package

  • Microsoft MakeCode for micro:bit
  • Micro:bit V2
  • Micro:bit Robot:bit Expansion Shield V2
  • Breakout Board
  • 2 FM Jumper Wires
  • Active Buzzer Module

Pin Layout:

Micro:Bit Pins:Sensor Pins:
GNDGND
0VCC

Setup:

Please refer to the diagram below for wiring. Please note that the circuit diagram uses a different Micro:bit shield than the supplied in our package, despite this, the wiring is the same.

  • Place the active buzzer on the breakout board and connect it to pin 0 on the micro:bit using the FM jumper wires
  • Open Microsoft Makecode
  • Plug in the Micro:bit to the computer
Clickt to expand

Code Walk Through:


Setting the buzzer tone:
We can make the buzzer play a tune by setting the buzzer to ring at certain frequencies/music notes while pausing the code so that the tone can play for that specified amount of time.

  • Setting the buzzer to ring Middle C: In the Music tab select “ring tone (Hz) (note)” line and change it to “ring tone (Hz) Middle C” before placing it in the forever block.

  • Pausing the code: In the basic tab select “pause (ms)” and set it to 100, then place it inside the forever block.
Click to expand

Playing the tune:

Repeat the above 2 steps but change the notes each time. The notes can be changed by selecting the Note on the code snippet and then selecting the new note by pressing the desired piano key on the keyboard that opens up.

  • Ring Middle E: Two keys right of Middle C
  • Ring Middle G: Two keys right of Middle E
  • Ring Middle E: Two keys left of Middle G
  • Ring Middle C: Two keys left of Middle E
Click to expand

Flashing the code onto the Micro:Bit:

  • Make sure the Micro:Bit is connected to the computer
  • On the bottom left corner, click the “Download” button and follow the prompts

Python Code:

Entire python code for the tutorial:

def on_forever():
    music.ring_tone(262)
    basic.pause(100)
    music.ring_tone(330)
    basic.pause(100)
    music.ring_tone(392)
    basic.pause(100)
    music.ring_tone(330)
    basic.pause(100)
    music.ring_tone(262)
    basic.pause(100)
basic.forever(on_forever)

Tutorial Simulation

Click to expand

Downloadable Content:

Please find this tutorial’s python & hex file for microsoft makecode on our Github.

Credits:

  • Microsoft
  • The Micro:bit Community
  • The STEM Community
Table of Contents