Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Buzzer on GPIO pin 17.
Raspberry Pi
from gpiozero import Buzzer buzzer = Buzzer([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong GPIO pin number.
Passing the pin number as a string instead of an integer.
✗ Incorrect
The Buzzer is connected to GPIO pin 17, so we pass 17 to the Buzzer constructor.
2fill in blank
mediumComplete the code to turn the buzzer on.
Raspberry Pi
from gpiozero import Buzzer buzzer = Buzzer(17) buzzer.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using off() instead of on().
Using toggle() which switches state but does not guarantee on.
✗ Incorrect
To turn the buzzer on, call the on() method.
3fill in blank
hardFix the error in the code to play a tone of 440 Hz for 1 second using TonalBuzzer.
Raspberry Pi
from gpiozero import TonalBuzzer, Tone import time buzzer = TonalBuzzer(17) buzzer.play([1]) time.sleep(1) buzzer.stop()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer directly instead of a Tone object.
Using a string like '440Hz' which is invalid.
✗ Incorrect
The play() method requires a Tone object with frequency=440 to play the tone correctly.
4fill in blank
hardFill both blanks to create a TonalBuzzer on pin 18 and play a 523 Hz tone.
Raspberry Pi
from gpiozero import TonalBuzzer, Tone buzzer = TonalBuzzer([1]) tone = Tone([2]) buzzer.play(tone)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up pin numbers and frequencies.
Passing frequency to TonalBuzzer instead of Tone.
✗ Incorrect
The TonalBuzzer is created on pin 18 and the Tone is set to 523 Hz (C note).
5fill in blank
hardFill all three blanks to create a dictionary of tones with their frequencies and play the 659 Hz tone.
Raspberry Pi
from gpiozero import TonalBuzzer, Tone buzzer = TonalBuzzer([1]) tones = {'E': Tone([2]), 'G': Tone(784)} buzzer.play(tones[[3]])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong pin number.
Using the wrong frequency for the 'E' tone.
Not using quotes around the dictionary key.
✗ Incorrect
The buzzer is on pin 17, the 'E' tone frequency is 659 Hz, and we play the tone labeled 'E'.