6. Piezo_Buzzer_and_music
The examples below use definition blocks to better organize the code.
Warning
6.1. Connections
6.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.
6.3. Library
import music
at the top under from microbit import *
.from microbit import *
import music
6.4. V2 speaker
- 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.
6.5. Playing music notes
- music.play(music, pin=pin0, wait=True, loop=False)
- Play the music.If music can be a string, such as ‘c4: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.
6.6. Notes
NOTE[octave][:duration]
.Ab
is A-flat and C#
is C-sharp.a4:8
refers to the note “A” in octave 4 that lasts for eight 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).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 3 notes: ‘e4:4’, ‘f#’, ‘g’
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 3 notes: ‘e4:4’, ‘f#’, ‘g’
from microbit import *
import music
speaker.off()
notes_list = ['e4:4', 'f#', 'g']
while True:
music.play(notes_list)
sleep(1000)
6.7. Tempo
- 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.
Tasks
Play the 5 notes: c, e, g, e, c with a tempo of 120, 180 and 240bpm.
Design code that uses a tempo list of 120, 240, 360, 480 and 600 bpm and a sleep_time with value 1000. Use a repeat loop to set the tempo and play the notes_list, [‘c4:4’, ‘e’, ‘g’, ‘e’, ‘c’].
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 code that uses a tempo list of 120, 240, 360, 480 and 600 bpm and a sleep_time with value 1000. Use a repeat loop to set the tempo and play the notes_list, [‘c4:4’, ‘e’, ‘g’, ‘e’, ‘c’].
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)
6.8. Other music methods
- 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.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)
# (240, 4)
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)
6.9. Tuple unpacking of get_tempo()
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)
6.10. Scales
from microbit import *
import music
speaker.off()
c_major = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'c5']
e_minor = ['e', 'f#', 'g', 'a', 'b', 'c', 'd', 'e5']
while True:
if button_a.is_pressed():
music.play(c_major)
elif button_b.is_pressed():
music.play(e_minor)
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 C major scale combined with an animation of the heart beating.
Play the 8 notes of D major.
from microbit import *
import music
speaker.off()
d_major = ["D", "E", "F#", "G", "A", "B", "C#", "D"]
while True:
music.play(d_major)
sleep(1000)
Play the 8 notes of F minor.
from microbit import *
import music
speaker.off()
f_minor = ["F", "G", "Ab", "Bb", "C", "Db", "Eb", "F"]
while True:
music.play(f_minor)
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()
d_major = ["D", "E", "F#", "G", "A", "B", "C#", "D"]
f_minor = ["F", "G", "Ab", "Bb", "C", "Db", "Eb", "F"]
while True:
if button_a.is_pressed():
music.play(d_major)
elif button_b.is_pressed():
music.play(f_minor)
sleep(1000)
Play the C major scale combined with an animation of the heart beating.
from microbit import *
import music
c_major = ['c', 'd', 'e', 'f', 'g', 'a', 'b', 'c5']
# 1 beat every 500ms
while True:
music.play(c_major, wait=False)
for i in range(8):
display.show(Image.HEART_SMALL)
sleep(250)
display.show(Image.HEART)
sleep(250)
sleep(200)
6.11. 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)
6.12. 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.
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)
6.13. V2 volume
- 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.
from microbit import *
import music
note0 = "c4:4"
note1 = "e4:4"
note2 = "f#4:4"
while True:
set_volume(255)
music.play(note0)
set_volume(128)
music.play(note1)
set_volume(64)
music.play(note2)
6.14. 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)
6.15. 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