[Logo for The C Shore Landing Page]
The C Shore

Using a Buzzer

Table of Contents

Using a Buzzer

There are two main types of buzzer: active and passive.

A passive buzzer emits a tone when a voltage is applied across it. It also requires a specific signal to generate a variety of tones. We will be using a passive buzzer.

Connecting a buzzer

An buzzer can be connected just like an LED, but as they are a little more robust, you won’t be needing a resistor to protect them.

Set up the circuit as shown below:

buzzer

  • Add TonalBuzzer to the from gpiozero import... line:

     from gpiozero import TonalBuzzer
     from gpiozero.tones import Tone
     from time import sleep
  • Add a line below your creation of button to add a TonalBuzzer object:

     buzzer = TonalBuzzer(17)
  • In GPIO Zero, a TonalBuzzer emits the tone of your choice using play method.

  • The complete program will be:

     from gpiozero import TonalBuzzer
     from gpiozero.tones import Tone
     from time import sleep
    
     while True:
         buzzer.play(Tone("A4"))   # Musical note
         sleep(1)
         buzzer.play(Tone(220))   # Hz
         sleep(1)
         buzzer.stop()

Did you catch what is happening with the use of Tone() in the play method?