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. The active buzzers are a lot simpler to use, so these are covered here.
Connecting a buzzer
An active 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:
Image source: Raspberry Pi Foundation
-
Modify the
examples/buzzer.rs
file:extern crate rust_gpiozero; use rust_gpiozero::*; use std::thread::sleep; use std::time::Duration; fn main() { // Tell the Pi which GPIO pin you are using let mut buzzer = Buzzer::new(17); }
-
In
rust_gpiozero
, aBuzzer
works exactly like anLED
, so try adding abuzzer.on();
andbuzzer.off();
into your loop:# #![allow(unused_variables)] #fn main() { loop{ // Make the buzzer switch on buzzer.on(); // Wait for one second sleep(Duration::from_secs(1)); // Make the buzzer switch off buzzer.off(); // Wait for one second sleep(Duration::from_secs(1)); } #}
-
A
Buzzer
has abeep(on_time,off_time)
method which works like anLED
'sblink
. Try it out:# #![allow(unused_variables)] #fn main() { // Let the buzzer beep buzzer.beep(1,1); #}