7. Piezo Buzzer and music
Warning
7.1. Connections
7.2. Model
Place the buzzer first. It pins directly into the breadboard. Ignore the diagram suggestion that wires are used.
Connect with the jumper wires to the edge-connector using pin0 and ground, 0V.
7.3. Library
import music
at the top under from microbit import *
.from microbit import *
import music
7.4. Volume V2
- set_volume(volume)
Configure the output volume of the microbit speaker and pins.
- Parameters:
volume – An integer between 0 and 255 to set the volume.
break
.from microbit import *
import music
note = "c4:2"
while True:
for v in range(0, 255, 25):
set_volume(v)
music.play(note)
if button_a.is_pressed():
break
if button_a.is_pressed():
break
7.5. Speaker V2
- speaker.off()
Use off() to turn off the speaker. This does not disable sound output to an edge connector pin.
- speaker.on()
Use on() to turn on the speaker.
Note
speaker.is_on()
.print(help(speaker))
does not list it as being available yet (as of July 2022).- music.play(music, pin=pin0, wait=True, loop=False)
Play the music.
If music can be a string, such as ‘c1:4’, or a list of notes as strings, such as [‘c’, ‘d’, ‘e’]
The duration and octave values are reset to their defaults before the music is played.
The output pin can be used to override the default pin0. Use pin=None to prevent sounds being played.
If wait is set to True, playing is blocking, and the music will be played to the end.
If loop is set to True, the music repeats until stop is called.
- music.stop(pin=pin0)
Stops all music playback on the built-in speaker and any pin outputting sound.
An optional argument can be provided to specify a pin, eg. music.stop(pin1).
- music.reset()
Resets the state of the following attributes as listed:
ticks = 4; bpm = 120; duration = 4; octave = 4
- music.set_tempo(ticks=4, bpm=120)
Sets the tempo for playback.
A number of ticks, expressed as an integer, make a beat. The default is 4 ticks per beat.
Each beat is to be played at a certain frequency, beats per minute, expressed as an integer. The default is 120 bpm.
- music.get_tempo()
Gets the current tempo as a tuple of integers: (bpm, ticks).
from microbit import *
import music
music.set_tempo(ticks=4, bpm=240)
tempo_data = str(music.get_tempo())
display.scroll(tempo_data)
from microbit import *
import music
music.set_tempo(ticks=2, bpm=120)
tempo_data = music.get_tempo()
bpm = tempo_data[0]
ticks = tempo_data[1]
display.scroll(bpm)
display.scroll(ticks)
7.6. Tuple unpacking for advanced users
bpm, ticks = music.get_tempo()
from microbit import *
import music
music.set_tempo(ticks=2, bpm=120)
bpm, ticks = music.get_tempo()
display.scroll(bpm)
display.scroll(ticks)
7.7. Notes
NOTE[octave][:duration]
.a2:4
refers to the note “A” in octave 2 that lasts for four ticks (a tick is an arbitrary length of time defined by a tempo setting function). If the note name R is used then it is treated as a rest (silence).Ab
is A-flat and C#
is C-sharp.music.set_tempo(ticks=4, bpm=120)
music.play(note)
to play a note in the note
variable.from microbit import *
import music
speaker.off()
note = 'c4:8'
music.play(note)
music.play(notes)
to play a list of notes in the notes_list
variable.from microbit import *
import music
speaker.off()
notes_list = ['c4:1', 'e:4', 'g:8', 'c:2', 'e5', 'g4','f#','eb']
music.set_tempo(ticks=4, bpm=240)
music.play(notes_list)
Tasks
Play the 5 notes: c, e, g, e, c.
Play the 5 notes: c, e, g, e, c with a tempo of 120, 180 and 240bpm.
Design a function that takes a tempo list of 120, 240, 360 , 480 and 600 bpm as one parameter and a sleep_time with default value 1000. Use a repeat loop to set the tempo and play the notes_list.
Play the 5 notes: c, e, g, e, c.
from microbit import *
import music
speaker.off()
notes_list = ['c4:4', 'e', 'g', 'e', 'c']
while True:
music.play(notes_list)
sleep(1000)
Play the 5 notes: c, e, g, e, c with a tempo of 120, 180 and 240bpm.
from microbit import *
import music
speaker.off()
notes_list = ['c4:4', 'e', 'g', 'e', 'c']
while True:
music.set_tempo(bpm=120)
music.play(notes_list)
sleep(1000)
music.set_tempo(bpm=180)
music.play(notes_list)
sleep(1000)
music.set_tempo(bpm=240)
music.play(notes_list)
sleep(1000)
Design a function that takes a tempo list of 120, 240, 360, 480 and 600 bpm as one parameter and a sleep_time with default value 1000. Use a repeat loop to set the tempo and play the notes_list.
from microbit import *
import music
speaker.off()
notes_list = ['c4:4', 'e', 'g', 'e', 'c']
tempo_list = [120, 240, 360, 480, 600]
def tempo_play(tempo_list, sleep_time=1000):
for tempo in tempo_list:
music.set_tempo(bpm=tempo)
music.play(notes_list)
sleep(sleep_time)
while True:
tempo_play(tempo_list, sleep_time=1000)
7.8. Scales
from microbit import *
import music
speaker.off()
cmajor = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'c']
eminor = ['e', 'f#', 'g', 'a', 'b', 'c', 'd', 'e']
while True:
if button_a.is_pressed():
music.play(cmajor)
elif button_b.is_pressed():
music.play(eminor)
sleep(1000)
Tasks
Play the 8 notes of D major. See: https://www.pianoscales.org/major.html
Play the 8 notes of F minor. See: https://www.pianoscales.org/minor.html
Play the D major scale when the A-button is pressed and the F minor scale when the B-button is pressed.
Play the 8 notes of D major.
from microbit import *
import music
speaker.off()
dmajor = ["D", "E", "F#", "G", "A", "B", "C#", "D"]
while True:
music.play(dmajor)
sleep(1000)
Play the 8 notes of F minor.
from microbit import *
import music
speaker.off()
fminor = ["F", "G", "Ab", "Bb", "C", "Db", "Eb", "F"]
while True:
music.play(fminor)
sleep(1000)
Play the D major scale when the A-button is pressed and the F minor scale when the B-button is pressed.
from microbit import *
import music
speaker.off()
dmajor = ["D", "E", "F#", "G", "A", "B", "C#", "D"]
fminor = ["F", "G", "Ab", "Bb", "C", "Db", "Eb", "F"]
while True:
if button_a.is_pressed():
music.play(dmajor)
elif button_b.is_pressed():
music.play(fminor)
sleep(1000)
7.9. Built in music
music.
music.play(melody)
.from microbit import *
import music
speaker.off()
music.play(music.POWER_UP)
melodies_list
and play it.from microbit import *
import music
speaker.off()
melodies_list = [music.DADADADUM, music.POWER_DOWN]
for melody in melodies_list:
music.play(melody)
7.10. All Built in melodies
break
.from microbit import *
import music
speaker.off()
built_in_tunes = [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]
while True:
for tune in built_in_tunes:
music.play(tune)
sleep(1000)
if button_a.is_pressed():
break
if button_a.is_pressed():
break
Tasks
Play any 3 melodies using a list.
Use the choice function to randomly pick melodies from a melody list. See: https://www.w3schools.com/python/ref_random_choice.asp. Use https://python.microbit.org/v/3 instead of Mu editor.
Play any 3 melodies using a list.
from microbit import *
import music
speaker.off()
melodies_list = [music.POWER_UP, music.DADADADUM, music.POWER_DOWN]
for melody in melodies_list:
music.play(melody)
Use the choice function to randomly pick melodies from a melody list. See: https://www.w3schools.com/python/ref_random_choice.asp.
from microbit import *
import music
import random
speaker.off()
melodies_list = [music.POWER_UP, music.DADADADUM, music.POWER_DOWN]
while True:
music.play(random.choice(melodies_list))
sleep(1000)
7.11. Sound effects using pitch
- music.pitch(frequency, duration=-1, pin=pin0, wait=True)
Plays a pitch at the integer frequency given for the duration specified in milliseconds.
Only one pitch can be played on one pin at any one time.
If duration is negative the pitch is played continuously until either the blocking call is interrupted or, in the case of a background call, a new frequency is set or stop is called.
An optional argument to specify the output pin can be used to override the default of pin0. pin=None causes no sound to play.
If wait is set to True, this function is blocking.
from microbit import *
import music
speaker.off()
for freq in range(880, 1760, 16):
music.pitch(freq, duration=20)
Tasks
Modify the code to increase the pitch in steps of 32 with a duration of 40.
Modify the code to decrease the pitch instead.
Modify the code to increase then decrease the pitch.
Modify the code to increase the pitch in steps of 32 with a duration of 40.
from microbit import *
import music
speaker.off()
for freq in range(880, 1760, 32):
music.pitch(freq, duration=40)
Modify the code to decrease the pitch instead.
from microbit import *
import music
speaker.off()
for freq in range(1760, 880, -16):
music.pitch(freq, duration=20)
Modify the code to increase then decrease the pitch.
from microbit import *
import music
speaker.off()
for freq in range(880, 1760, 16):
music.pitch(freq, duration=20)
for freq in range(1760, 880, -16):
music.pitch(freq, duration=20)
7.12. Note frequencies
Note |
Frequency |
---|---|
A |
440 |
B flat |
466 |
B |
494 |
C |
523 |
C sharp |
554 |
D |
587 |
D sharp |
622 |
E |
659 |
F |
698 |
F sharp |
740 |
G |
784 |
A flat |
831 |
A |
880 |
B flat |
932 |
B |
988 |
C |
1046 |
C sharp |
1108 |
D |
1174 |
D sharp |
1244 |
E |
1318 |
F |
1396 |
F sharp |
1480 |
G |
1568 |
A flat |
1662 |
A |
1760 |
break
.from microbit import *
import music
speaker.off()
Am_freqs = [440, 494, 523, 587, 659, 698, 784, 880]
timing = 400
while True:
for freq in Am_freqs:
music.pitch(freq, duration=timing)
if button_a.is_pressed():
break
Tasks
Modify the code to play the pitches of the E minor scale. See: https://www.piano-keyboard-guide.com/e-minor-scale.html.
Modify the code to play the pitches of the D major scale. See: http://www.piano-keyboard-guide.com/d-major-scale.html.
Modify the code to play the pitches of the E minor scale. See: https://www.piano-keyboard-guide.com/e-minor-scale.html.
from microbit import *
import music
speaker.off()
Em_freqs = [659, 740, 784, 880, 988, 1046, 1174, 1318]
timing = 400
while True:
for freq in Em_freqs:
music.pitch(freq, duration=timing)
if button_a.is_pressed():
break
Modify the code to play the pitches of the D major scale. See: http://www.piano-keyboard-guide.com/d-major-scale.html.
from microbit import *
import music
speaker.off()
D_freqs = [587, 659, 740, 784, 880, 988, 1108, 1174]
timing = 400
while True:
for freq in D_freqs:
music.pitch(freq, duration=timing)
if button_a.is_pressed():
break
7.13. Custom tunes
from microbit import *
import music
speaker.off()
music.set_tempo(ticks=4, bpm=240)
notes_list = ['e', 'd', 'c', 'd', 'e', 'e','e','d','d','d','e','g','g','e','d','c','d','e','e','e','d','d','e','d','c']
while True:
music.play(notes_list)
sleep(1000)