piezo buzzer pcb wemos d1 mini esp8266 oled

A simple buzzer/piezo Lua library for NodeMCU/ESP8266

I was working on a project that involved adding a buzzer and playing different notes. I ended up writing something pretty easy to use so decided to put it on GitHub to save people some time.

Link to the repo: https://github.com/alexchumak/buzzkill-lua

Usage

Please note that in real life you would have a delay between the call below or initiate consecutive sounds from callbacks to make sure things are not playing in parallel.

local pin_number = 1
local buzzkill = require("buzzkill")
local buzzer = buzzkill.setup(pin_number)
local sounds = { { note = 'a', duration = 200 }, { frequency = 1000, duration = 100 } }

-- play two sounds, 'a' note for 200ms and a 1000Hz frequency for 100ms
buzzer(sounds)

-- play sounds and print a statement when done
buzzer(sounds, function() print("done!") end)

-- play one note
buzzkill.playNote(pin_number, sounds[1])

-- same as buzzer(sounds), if you don't want to use the 'setup' convenience wrapper
buzzkill.play(pin_number, sounds)

Only a few notes are supported at this point, but using thefrequency key you are in full control. Feel free to submit a pull request if you would like to contribute notes and their frequencies.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *