Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses

Sound for the cyber:bot

Curriculum

  • 1 Section
  • 5 Lessons
  • Lifetime
Expand all sectionsCollapse all sections
  • Sound for the cyber:bot
    5
    • 1.1
      Introducing the Piezospeaker
    • 1.2
      Place the Piezospeaker
    • 1.3
      The cyber:bot tone function
    • 1.4
      Notes from Frequencies
    • 1.5
      Songs with Lists

The cyber:bot tone function

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.

 


Printer-friendly version
Place the Piezospeaker
Prev
Notes from Frequencies
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress