The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 6 Physical computing with Scratch and Python 139

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


Controlling a buzzer in Scratch
Recreate the same program as for making the LED flash – or load it, if you saved it before you
created the button project. Use the drop-down in the set gpio to output high blocks to select
number 15, so Scratch is controlling the correct GPIO pin.


when clicked

forever

setgpio^15 to output high

wait^1 secs

setgpio^15 to output low

wait^1 secs

Click the green flag, and your buzzer will begin to buzz: one second on, and one second off.
If you only hear the buzzer clicking once a second, you are using a passive buzzer rather than
an active buzzer. Where an active buzzer generates the rapidly changing signal, known as an
oscillation, to make the metal plates vibrate itself, a passive buzzer needs an oscillating signal.
When you simply turn it on using Scratch, the plates only move once and stop – making the
‘click’ sound until the next time your program switches the pin on or off.
Click the red octagon to stop your buzzer, but make sure to do so when it’s not making
a sound, otherwise the buzzer will continue to buzz until you run your program again!


CHALLENGE: CHANGE THE BUZZ
How could you change the program to make the buzzer
sound for a shorter time? Can you build a circuit so the
buzzer is controlled by a button?
Free download pdf