How to get sound from the micro:bit using a piezo speaker or amp

What you’ll need:

  • A micro:bit, battery pack and micro USB cable, plus a suitable computer for programming.
  • 2 crocodile clip leads.
  • A piezo speaker or an audio amp or even an old pair of earphones (don’t use a good pair in case you wreck them).

Step One: The Basic Code

You’re going to use the Python editor on the micro:bit website for this jam.

Click here to open the editor.

Copy the basic Python script for this Jam below:

# http://python.microbit.org/editor.html
# Rebel MicroBits
import speech
from microbit import *
from microbit import sleep



# Rebel MicroBits

stormtrooper = [
    "How long have you had these micro-bits.",
    "Let me see your identification.",
    "We don't need to see his identification.",
    "These aren't the micro-bits we're looking for.",
    "You can go about your business.",
    "Move along. move along.",
]

rebelscum = [
    "about 3 or 4 seasons.",
    "You don't need to see his identification.",
    "These aren't the micro-bits you're looking for.",
    "He can go about his business.",
    "Move along.",
    "HAHA those totally are the microbits you're looking for",
]

# Loop over each line in the poem and use the speech module to recite it.

display.scroll('Ready in ..3..  ..2..  ..1.. ACTION', delay=100, wait=True, loop=False)

for line in range(0,6):

    display.show(Image.SKULL)
    speech.say(stormtrooper[line], speed=120, pitch=120, throat=100, mouth=200)
    display.scroll(rebelscum[line], delay=25, wait=True, loop=False)

When you’re done, download the .hex file and flash it to your micro:bit. Remember to give the script a memorable name!

Step Two: Connecting Speakers

The piezo speaker is a nice, robust, low cost device but in this application it’s going to be a little quiet.

piezo wiring

If you have a small speaker or a pair of old headphones, they have a much louder sound output. You can read up on how to connect your headphones to your micro:bit here.

A word of warning! The volume from the headphones is going to be quite loud – please do not put the headphones over your ears!

Another alternative is to use an audio amp. Since the output from the micro:bit is quite high, turn the volume all the way down then gradually turn the volume up as the micro:bit talks.

Step Three: Wiring Up the Project

Okay, let’s wire this up. First make sure your micro:bit is powered down. Use the crocodile clips to connect one of the piezo wires to Pin 0 and the other wire to Pin 1.

microbit piezo sound

If you’re using a headphone or amp, clip the tip of the jack plug to Pin 0 and clip the base of the jack plug to Pin 1 using the crocodile clips.

microbit attached to amp

Connect power and after displaying a short countdown the micro:bit should start reciting some familiar dialogue.

Have a look at the code and make the micro:bit say whatever you want..

Finishing Up

Quite robotic and monotonous, right? Take a look at the Python documentation for speech and see what happens when you alter some of the speech parameters.

You can even get the thing to sing! Maybe your project could be a micro:bit musical…?

download a printable version

2017-07-19T00:12:01+00:00 July 16th, 2017|All Blogs, How to|