You must first complete Looping Your Code before viewing this Lesson

Playing a Beep

Introduction

The Little Python Brain comes equipped with a piezo buzzer. Using this component, we can make it play a wide variety of tones! For this project, we will be making a police siren, which flashes a light from red to blue and produces an oscillating soundwave! Let’s start by first making the buzzer play one note.

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Importing the Pin and Buzzer Libraries

Similar to the previous projects, we first need to have a way to interface with the Brain’s Pins. In our case, we also need to import the library responsible for producing the sounds: the buzzer. 

2

Setting Up the Buzzer

Recall how we set up the NeoPixel in previous projects. We will do the same thing with the Buzzer here. By inspecting the Little Python Brain that you have, we can see that the buzzer is connected to Pin 4. That’s what the 4 in the block means!

3

Learning more about the bz.playNote block

This is the main block we will be using for playing music notes.

  • Note – denotes what musical note the buzzer will play. 
  • Note Duration – how long (in milliseconds) the note will be played before turning it off for the time set by Rest Duration.
  • Rest Duration – how long (in milliseconds) to turn the buzzer off before executing the next code. By default, we can leave this at 0.

Note that both Note Duration and Rest Duration has elements of sleep. Hence, if we place other codes before this block, we can effectively use the bz.playNote block as a stand-in for the sleep block too!

4

Playing a single note

Now that we learned about the bz.playnote block, simply leave the note at c4, change the Note Duration to 500 and leave the rest duration at 0 then run the code!

The Complete Code

Back to: Beginner > Police Siren

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account