https://github.com/raspberrypi-go-drivers/passivebuzzer
Allows to control a passive buzzer connected to a GPIO pin
https://github.com/raspberrypi-go-drivers/passivebuzzer
driver go golang raspberry-pi raspberrypi-drivers robotics
Last synced: about 1 month ago
JSON representation
Allows to control a passive buzzer connected to a GPIO pin
- Host: GitHub
- URL: https://github.com/raspberrypi-go-drivers/passivebuzzer
- Owner: raspberrypi-go-drivers
- License: mit
- Created: 2020-12-18T22:48:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-18T22:59:02.000Z (over 5 years ago)
- Last Synced: 2025-09-12T13:01:51.177Z (9 months ago)
- Topics: driver, go, golang, raspberry-pi, raspberrypi-drivers, robotics
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Passive Buzzer
[](https://pkg.go.dev/github.com/raspberrypi-go-drivers/passivebuzzer)

[](https://goreportcard.com/report/github.com/raspberrypi-go-drivers/passivebuzzer)
[](https://opensource.org/licenses/MIT)
Package passivebuzzer is a driver allowing to control a passive buzzer from a GPIO pin
## Documentation
For full documentation, please visit [](https://pkg.go.dev/github.com/raspberrypi-go-drivers/passivebuzzer)
## Quick start
```go
import (
"math"
"os"
"time"
"github.com/raspberrypi-go-drivers/passivebuzzer"
"github.com/stianeikeland/go-rpio/v4"
)
const (
c4 float64 = 261.63 // Hz
d4 float64 = 293.66 // Hz
e4 float64 = 329.63 // Hz
f4 float64 = 349.23 // Hz
fd4 float64 = 369.99 // Hz
g4 float64 = 392.00 // Hz
a4 float64 = 440.00 // Hz
b4 float64 = 493.88 // Hz
c5 float64 = 523.25 // Hz
d5 float64 = 587.33 // Hz
e5 float64 = 659.25 // Hz
f5 float64 = 698.46 // Hz
fd5 float64 = 739.99 // Hz
g5 float64 = 783.99 // Hz
// https://pages.mtu.edu/~suits/notefreqs.html
)
type note struct {
note float64
duration float64
}
func main() {
err := rpio.Open()
if err != nil {
os.Exit(1)
}
defer rpio.Close()
buzzer := passivebuzzer.NewPassiveBuzzer(18)
melody := []note{
{e4, 1}, {g4, 1}, {b4, 1}, {e5, 1}, {b4, 1},
{g4, 1}, {e4, 1}, {g4, 1}, {b4, 1}, {g5, 1},
{g4, 0.5}, {e5, 0.5}, {b4, 0.5}, {g4, 0.5},
{fd5, 1}, {d4, 0.5}, {g5, 0.5}, {d5, 0.5},
{g4, 0.5}, {g5, 1}, {d5, 0.5}, {g4, 0.5},
{g4, 0.5}, {fd4, 0.5}, {e4, 2},
}
for _, n := range melody {
buzzer.Tone(n.note)
multiplier := int(math.RoundToEven(400.0 * n.duration))
time.Sleep(time.Duration(multiplier) * time.Millisecond)
buzzer.StopTone()
time.Sleep(33 * time.Millisecond)
}
}
```
## Raspberry Pi compatibility
This driver has has only been tested on an Raspberry Pi Zero WH using integrated bluetooth but may work well on other Raspberry Pi having integrated Bluetooth
## License
MIT License
---
Special thanks to @stianeikeland
This driver is based on his work in [stianeikeland/go-rpio](https://github.com/stianeikeland/go-rpio/)