MicroPython on the BBC micro:bit comes with a music and sound module. It’s very easy to generate bleeps and bloops from the device when you attach a speaker.
Attaching a speaker
Use crocodile clips to attach pin 0 and GND to the positive and negative inputs on the speaker - it doesn’t matter which way round they are connected to the speaker.
playing music:
import music
music.play(music.NYAN)
import the music module. It contains methods used to make and control sound.
MicroPython has quite a lot of built-in melodies.
• music.DADADADUM
• music.ENTERTAINER
• music.PRELUDE
• music.ODE
• music.NYAN
• music.RINGTONE
• music.FUNK
• music.BLUES
• music.BIRTHDAY
• music.WEDDING
• music.FUNERAL
• music.PUNCHLINE
• music.PYTHON
• music.BADDY
• music.CHASE
• music.BA_DING
• music.WAWAWAWAA
• music.JUMP_UP
• music.JUMP_DOWN
• music.POWER_UP
• music.POWER_DOWN
There is also a simple tone generator software called music
To play the note A in an octave number 1 for a duration of 4 type music.play("C1:4")
opening of “Frere Jaques”:
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)
you could also use tune = ["C4:4", "D", "E", "C", "C", "D", "E", "C", "E", "F", "G:8",
"E:4", "F", "G:8"]
music.pitch
music.pitch uses frequency and duration in milliseconds
Middle C=440 Hz
music.pitch(440,6)