Making music with micro:bit - MicroPython
What you will need:
- Web enabled device (PC, Tablet, Phone) with an up-to-date browser (Internet Explorer, Safari, Chrome)
- BBC micro:bit MicroPython editor (https://python.microbit.org/v/1.1)
- BBC micro:bit
- Speaker or headphones & two crocodile clips
Making music with MicroPython:
The built-in music library in MicroPython allows us to play music on your micro:bit.
Before you start, you need to import the music library. The music library contains the methods used to make and control sound. Use the import music command to load the music library.
The built-in music library in MicroPython allows us to play music on your micro:bit.
Before you start, you need to import the music library. The music library contains the methods used to make and control sound. Use the import music command to load the music library.
import music
Once you have imported the music library, it is possible to programme your device to play notes (or tones).
In Micro Python, we use lists to play a sequence of notes. Here is the code for the opening bars of ‘Frere Jaques’:
tune = ["c4:4", "d4:4", "e4:4", "c4:4", "c4:4", "d4:4", "e4:4", "c4:4", "e4:4", "f4:4", "g4:8", "e4:4", "f4:4", "g4:8"]
Each note is expressed as a string of characters like this:
“c4:4”
Where C = note, 4 = octave and :4 = duration (beat or ticks). Note names are case sensitive.
Example
Try the following example:
Try the following example:
from microbit import *
import music
tune = ["c4:4", "d4:4", "e4:4", "c4:4", "c4:4", "d4:4", "e4:4", "c4:4", "e4:4", "f4:4", "g4:8", "e4:4", "f4:4", "g4:8"]
music.play(tune)
Did you recognise the tune?
Transcribing songs from sheet music
If we want to re-create our favourite songs on our micro:bit, we first need a basic understanding of sheet music.
Here’s a reminder of the most common notes used in a musical score:
The Treble Clef
If we want to re-create our favourite songs on our micro:bit, we first need a basic understanding of sheet music.
Here’s a reminder of the most common notes used in a musical score:
The Treble Clef
Below is the score for a very popular piece of music - Can you can guess what it is?
Answer: Grande Valse (You may know it as the ‘Nokia Ringtone’)
Did you notice the special # symbols at the start of the score. This shows that some of the notes are sharps. In this case, the sharp notes are:
F#, C#, and G#
In MicroPython, sharp notes are identified by adding the sharp symbol (or hashtag) # directly after the note, followed by the octave number. Equally, flat notes are denoted using a b.after the note.
Example:
Did you notice the special # symbols at the start of the score. This shows that some of the notes are sharps. In this case, the sharp notes are:
F#, C#, and G#
In MicroPython, sharp notes are identified by adding the sharp symbol (or hashtag) # directly after the note, followed by the octave number. Equally, flat notes are denoted using a b.after the note.
Example:
“c#4:4”
“f#5:4”
Below is the music score for the Nokia Ringtone (this time with notes underneath).
It’s all about the timing
If we look at the notes in a music score again, you will notice that they’re different shapes and colours. These different shapes and colours denote the timings. (See below)
Notice that some of the notes have a dot (or full stop) after them. For these notes, we need to multiply the duration by 1.5.
Below is the score again however, this time, with timings (duration).
Rests
Rests are natural pauses or breaks in a piece of music. Rests can be added to your code using the letter r followed by the duration. For example:
Rests are natural pauses or breaks in a piece of music. Rests can be added to your code using the letter r followed by the duration. For example:
“r:4”
“r:2”
Rests are identified by the following symbols:
Challenge
- Program your micro:bit to play the Nokia ringtone.
- Use a loop to repeat the ringtone 4 times (or forever if you want to be really annoying!)
- Modify your code so that the ringtone only plays when you press the ‘A’ button
Resources
Sheet music:
Playing music through your micro:bit
Option 1 - Buzzer
Wiring instructions
Option 1 - Buzzer
Wiring instructions
- Disconnect the micro:bit from your computer and disconnect the battery pack.
- Using crocodile clips, connect the longer leg (or + side) to Pin 0, and the shorter leg to GND on the BBC micro:bit.
Option 2 - Headphones
Wiring instructions
- Disconnect the micro:bit from your computer and disconnect the battery pack.
- Using crocodile clips, connect the tip of the mini-jack to Pin 0, and the base of the mini jack to GND on the BBC micro:bit.
Useful links:
Micro:bit Music Fest - All the tools you need to make your own micro:bit band.
MicroPython - MicroPython music reference guide.
Play Tone - Reference guide for making music with Make Code.
Somebody to You: The Vamps - Learn how to play 'Somebody to You' by the Vamps on your BBC micro:bit using MicroPython.
Licence:
Unless otherwise specified, everything in this repository is covered by the following licence:
BBC micro:bit "Magic 8-Ball" lesson is licenced under a Creative Commons Attribution 4.0 International License.