adrianhesketh.com

Raspberry Pi piezo buzzer with Go

I’m building a burglar alam with my son, using a Raspberry Pi Zero as the micro-controller, and using Go as the programming language.

That’s led to me needing a few libraries along the way. Today’s library is to play sounds using a piezo buzzer.

You can grab the code from https://github.com/a-h/beeper

It’s easy enough to make a beep:

pin := rpio.Pin(12)
beeper.Beep(pin, 440, time.Millisecond*500)

Or you can play a tune using musical notes and a tempo.

pin := rpio.Pin(12)
bpm := 140
m := beeper.NewMusic(pin, bpm)
m.Note("A5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("D5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("F#5", beeper.Crotchet)
m.Note("F#5", beeper.Crotchet)
m.Note("E5", beeper.Crotchet)
m.Rest(beeper.Crotchet)
m.Note("A5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("D5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("E5", beeper.Crotchet)
m.Note("E5", beeper.Crotchet)
m.Note("D5", beeper.Quaver)
m.Note("C#5", beeper.Quaver)
m.Note("B4", beeper.Quaver)
m.Rest(beeper.Crotchet)
m.Note("A5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("D5", beeper.Quaver)
m.Note("B5", beeper.Quaver)
m.Note("D5", beeper.Crotchet)
m.Note("E5", beeper.Quaver)
m.Note("C#5", beeper.Quaver)
m.Rest(beeper.Quaver)
m.Note("A4", beeper.Quaver)
m.Note("E5", beeper.Crotchet)
m.Note("D5", beeper.Crotchet)