Let's test the piezospeaker using calls to the cyberbot module's tone function. True to its name, this function instructs an I/O pin to alternate high/low electrical signals at a specific frequency, allowing a piezospeaker to emit a tone.
The tone function allows you to specify the frequency with the f parameter, and the duration of the tone in milliseconds with the ms parameter.
bot(22).tone(f, ms)
This piezospeaker is designed to play 4.5 kHz tones for smoke alarms, but it can also play a variety of audible tones and usually sounds best in the 1 kHz to 3.5 kHz range. The start-alert tone we’ll use is:
bot(22).tone(3000, 1000)
That will make P22 send a series of high/low signals repeating at 3 kHz (3000 times per second). The tone will last for 1000 ms, which is 1 second. The micro:bit will wait until the tone function is complete before moving on to the next command.
Hardware Setup
- Set the cyber:bot board's power (PWR) switch to Position 0.
- Make sure the battery holder is loaded with 5 AA batteries.
- Make sure the battery holder's barrel plug is firmly plugged into the cyber:bot board's barrel jack.
- Connect your micro:bit module to your computer with a USB cable.
Example script: piezospeaker_test
This example script makes a beep when it starts running, then it sends the message “Waiting for Reset” scrolling across the micro:bit LED matrix. These messages will continue indefinitely because they are in the while True: loop. If the reset button is pressed, the speaker will replay the sound.
- In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
- Make sure the cyberbot.py module is added to the Project Files.
(See Add modules to your micro:bit). - Set the project's name to piezospeaker_test, enter the script below, and then click Save.
(See Save & Edit Scripts and Flash Scripts with Python Editor.) - Click Send to micro:bit.
(See Flash Scripts with Python Editor.)
# piezospeaker_test from cyberbot import * bot(22).tone(3000, 1000) while True: display.scroll("Waiting for Reset")
- Set PWR to 1.
- Verify that the piezospeaker makes a tone for one second.
- Press and release the micro:bit module's reset button to hear the tone again. The reset button is underneath the micro:bit, by the USB connector.
Try this: for loops for sound effects
It is easy to make sound effects by using a for loop.
- Set the project's name to sound_effect, enter the script below, and then click Save.
- Click Send to micro:bit.
# sound_effect from cyberbot import * for freq in range (500, 3100, 100): bot(22).tone(freq, 100)
You should hear a series of tones that increase in pitch.
- Set PWR to 0.
Keep PWR set to 0 while you are entering code or modifying circuits.
Set PWR to 1 to test the script (or 2 to test the script with servos).
When you are done for the day, set PWR to 0 AND unplug the batter holder's barrel plug from the cyber:bot board's barrel jack..
Your Turn
Just for fun, modify the sound_effect script by varying the values in the range function's start, stop, and step parameters.
- Try making the tones cover a greater range of frequencies.
- Try making the tones vary only a little from one to the next, for a smoother sound.
- Try making the tones go from a higher pitch to a lower pitch, so it sounds like something is falling.
- Try making a script that sounds like a cricket.