LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Sound for the cyber:bot

Sound for the cyber:bot

What it’s about

This tutorial will show you how to add a small speaker to your cyber:bot board that can emit different frequencies. Using the cyberbot module’s play note or tone blocks, you will learn to create programs that make sounds and even write simple songs using lists (similar to arrays).

Before you start

You will need:

  • Your programming software set up, with your micro:bit ready to use the cyber:bot module
  • A fully assembled cyber:bot robot
  • A piezospeaker from your cyber:bot kit’s electronic component pack.

After you finish

You will be ready to continue with the main cyber:bot tutorial sequence. You will also be equipped to add your own custom audio alerts and sound effects to personalize your cyber:bot projects.

Introducing the Piezospeaker

To emit sound, the cyber:bot uses a device called a piezoelectric speaker (piezospeaker). This speaker can make different tones depending on the rate of high/low electrical signals it receives. The schematic symbol and part drawing are shown below.

A piezoelectric element is a crystal that changes shape slightly when voltage is applied to it. Applying high and low voltages alternating at a rapid rate causes the crystal to rapidly change shape. The resulting vibration in turn vibrates the air around it, which is what the ear detects as sound.

Frequency Facts

  • The rate or speed at which something changes is said to be its frequency.
  • Frequency can be measured in hertz (Hz) which is the number of times a signal repeats itself in one second. 
  • One kilohertz is one-thousand-times-per-second, abbreviated 1 kHz.
  • Every rate of vibration makes a different tone or pitch.
  • The human ear is able to detect frequencies in a range from very low pitch (20 Hz) to very high pitch (20 kHz or 20,000 Hz). 

Did you know?

Piezoelectric elements have many uses. As opposed to sending voltage to the piezoelectric element to make it change shape, when force is applied to a piezoelectric element (which impacts its shape), it actually creates voltage.

Some electronic drum sets use piezoelectric elements to detect when, and how hard, a drummer has hit a sensor.

Some barbeque grills have push-button lighters that use a piezoelectric element to cause a spark to start the grill. A spring-loaded hammer inside the button impacts a piezoelectric element to generate a small spark.

Piezoelectric elements, depending on their shape and thickness, have a frequency at which they naturally vibrate. This natural vibration can be used to create voltages at set frequencies that function as the clock oscillator for many computers and microcontrollers. If you have ever seen a clock or watch that says “quartz”, there is a piezoelectric quartz crystal inside.

Adapted Wikimedia Commons image “Inside Quartz Crystal Tuning Fork” by Chribbe76 [Public domain]

Place the Piezospeaker

It is time to add a piezospeaker to your cyber:bot board.

Parts Required

(1) piezospeaker from the Small Robot Electronics Component Pack. (Just peel off the “Remove the seal after washing” sticker if it has one).

The picture above shows the schematic for the piezospeaker connected to P22. Notice that the cyber:bot board has two slots that fit the piezospeaker, and will connect it to P22.

Long Leads or Short?

The cyber:bot board was designed for a piezospeaker with long leads, like the one on the left. But due to supply chain challenges, some kits may include a piezospeaker with short leads, like the one shown on the right. Either will work!

For Long-Lead Piezospeakers

  • Look for the (+) symbol on the top of the piezospeaker case. The positive leg of the speaker is approximately underneath.
  • Gently insert the positive leg into the cyber:bot board hole by the P22 label. The other leg goes into the hole near the GND label. The pins will go through the board and into sockets underneath.
  • Put the cyber:bot board power switch in position 1 (or 2) when you want to run a program that uses the piezospeaker.

The speaker should stay in place for the rest of the activities as it does not interfere with any other circuits since it’s not positioned on the breadboard.

For Short-Lead Piezospeakers

If your piezospeaker has short leads, it may fall out of the socket when inserted from the top of the cyber:bot board. But that is okay, it can still be plugged into the socket from the bottom of the board.

  • Look for the (+) symbol on the top of the piezospeaker case. The positive leg of the speaker is approximately underneath.
  • Look for the 4-pin socket on the underside of the cyber:bot board, directly underneath the piezospeaker label. The piezospeaker leads will go in the outermost holes of this socket.
  • Gently insert the piezospeaker leads into the socket, with the aligning positive lead with the plus symbol on top of the board.
  • Put the cyber:bot board power switch in position 1 (or 2) when you want to run a program that uses the piezospeaker.

Note: you can also build a piezospeaker circuit on the breadboard. To use a piezospeaker on the breadboard, make sure its legs go in different breadboard rows. Using jumper wires, connect its (+) leg to an I/O pin socket (P4 for example) and connect its other leg to a GND socket. In your projects, you will need to change Pin22 to Pin4 or the number for whichever pin you used.

The cyber:bot tone block

Let’s test the piezospeaker using calls to the cyber:bot module’s tone block. True to its name, this block instructs an I/O pin to alternate high/low electrical signals at a specific frequency, allowing a piezospeaker to emit a tone.

The tone block allows you to specify the frequency with the freq parameter, and the duration of the tone in milliseconds with the dur parameter. 

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:

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 block is complete before moving on to the next command. 

Example project: piezospeaker_test

This example project 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 forever loop. If the reset button is pressed, the speaker will replay the sound.

  •  In a browser, go to makecode.microbit.org to open the micro:bit Makcode Editor.
  •  Make sure the cyberbot extension is added.
    (See Add extensions to your micro:bit).
  • Set the name to piezospeaker_test and enter the project below.
    (See Save & Edit Projects and Flash Projects with MakeCode Editor.)
  • Click Download.
    (See Flash Projects with MakeCode Editor.)

  • 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 and a range block from the support subcategory.

  • Set the name to sound_effect and enter the project below.
  • Click Download. 

You should hear a series of tones that increase in pitch.

  • Set PWR to 0.

Your Turn

Just for fun, modify the sound_effect project by varying the values in the range block’s start, end, 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 project that sounds like a cricket.

Notes from Frequencies

You can use musical note frequencies in the chart below for the tone block’s f parameter to play music.

Example:

This block plays a B6 note for half of a second. Using this chart, you’re able to create songs with the piezospeaker and the cyber:bot.

The notes to the first line of the Happy Birthday song go in the following order: D – D – E – D – G – F. Matching the notes with their frequencies, we can make the following program.

Example project: happy_birthday

This example project plays out the first line of “Happy Birthday.”

  • Set the project’s name to happy_birthday, enter the blocks below, and then click Download.
  • Set PWR to 1.

Did you hear your cyber:bot play Happy Birthday?

  • Set PWR to 0.

Songs with Lists

A good way to keep track of notes in a song is to use a list. Sometimes, in other languages, these structures are referred to as an array. For now, they can be thought of as the same thing but there are some behind-the-scenes differences between these two which will be covered at some other time.

A list is a collection of values that can be referred to as a single, cohesive unit. Think about how a song is a single collection comprised of several individual notes. While each note is important, it is all the notes together that make up the song.

Taking the previous example, each of the notes in the project happy_birthday are grouped in order into a list called notes. The lengths of each note are grouped, in order, into another list called lengths.

How Lists Work

To make use of data stored in a list, each element of a list is referred to by its index. The index of a list is simply the number of each element beginning with zero. So, the first element is referred to as element 0. To refer to element 0 of notes would be the notes get value at (0) block. The code to refer to the fifth element, would be the notes get value at (4) block. Notice how the index appears to be “off by one”. When working with lists, this can be a common mistake.

Element 0 1 2 3 4
Contents “Do” “Re” “Me” “Fa” “So”
Block

Example project: song_with_list.

With lists, you do not have to hard code numbers for the index. A variable, like index, can stand in for a number and then that variable can be changed as the program runs. Doing this allows blocks to “walk” through a list.

  • Enter and flash song_with_list.

How song_with_list works

In the example above, the variable index is created after the two lists. The while loop checks the value of index each time it repeats, or iterates. As long as index is less than or equal to 5, the loop continues. Inside the loop, the tone block gets the values from each of the arrays depending on the current value of index. The second block in the loop increases the value of index by 1. This occurs over and over until the value of index becomes 6 and the loop stops. (And it’s a good thing it stops there because there is not an element number 6 in either of these lists!)

Your Turn

  • Create a project that plays the first several notes of a song that you choose.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/sound-for-the-cyberbot_makecode/
Links