Buzzer
Drive a buzzer or speaker using the PWM API.
Overview
This is a sample app which drives a buzzer or speaker using the PWM API.
The sample plays a short beep with 1108.73 ㎐ (music note D♭₆ [1]) for the time of a ⅛ music note [2] at boot time and offers the special shell command buzzer for playing other jingles or short songs.
Requirements
The sample requires a buzzer or speaker whose signal pin is connected to a pin
driven by PWM. The buzzer or speaker must be defined in Devicetree using the
pwm-buzzers
compatible (part of the sample) and setting its node to the
alias pwm-buzzer0
. You will need to do something like this:
/ {
aliases {
pwm-buzzer0 = &pwm_buzzer0;
};
pwm_buzzers {
compatible = "pwm-buzzers";
status = "okay";
pwm_buzzer0: pwm_buzzer0 {
pwms = <&pwm0 1 PWM_HZ(880) PWM_POLARITY_NORMAL>;
label = "PWM_BUZZER";
};
};
};
Note that a commonly used period value is 880 ㎐, twice the concert pitch frequency of 440 ㎐. See one of the following development boards:
Building and Running
On Maker Nano RP2040 board:
west build -b cytron_maker_nano_rp2040 -p -S usb-console -d build/buzzer-cytron_maker_nano_rp2040 bridle/samples/buzzer west flash -r uf2 -d build/buzzer-cytron_maker_nano_rp2040
On Maker Pi RP2040 board:
west build -b cytron_maker_pi_rp2040 -p -S usb-console -d build/buzzer-cytron_maker_pi_rp2040 bridle/samples/buzzer west flash -r uf2 -d build/buzzer-cytron_maker_pi_rp2040
Sample Output
(text in bold is a command input, text in angle brackets are keys to press)
[00:00:00.003,000] <inf> buzzersh: Buzzer shell is ready! uart:~$ <Tab> bridle buzzer clear device devmem gpio help history kernel log pwm rem resize retval shell uart:~$ help Please press the <Tab> button to see all available commands. You can also use the <Tab> button to prompt or auto-complete all commands or its subcommands. You can try to call commands with <-h> or <--help> parameter for more information. Shell supports following meta-keys: Ctrl + (a key from: abcdefklnpuw) Alt + (a key from: bf) Please refer to shell documentation for more details. Available commands: bridle : Bridle commands. buzzer : Buzzer related commands clear : Clear screen. device : Device commands devmem : Read/write physical memory Usage: Read memory at address with optional width: devmem address [width] Write memory at address with mandatory width and value: devmem address <width> <value> gpio : GPIO commands help : Prints the help message. history : Command history. kernel : Kernel commands log : Commands for controlling logger pwm : PWM shell commands rem : Ignore lines beginning with 'rem ' resize : Console gets terminal screen size or assumes default in case the readout fails. It must be executed after each terminal width change to ensure correct text display. retval : Print return value of most recent command shell : Useful, not Unix-like shell commands. uart:~$ buzzer -h buzzer - Buzzer related commands Subcommands: info :Get buzzer info beep :Use buzzer to beep play :Play one of predefined sounds uart:~$ buzzer info Warning: not yet implemented. uart:~$ buzzer beep uart:~$ buzzer play play - Play one of predefined sounds Subcommands: folksong : Play the 'folksong' song xmastime : Play the 'folksong' song funkytown : Play the 'funkytown' song mario : Play the 'mario' song golioth : Play the 'golioth' song tiacsys : Play the 'tiacsys' song uart:~$ buzzer play folksong uart:~$ buzzer play xmastime uart:~$ buzzer play funkytown uart:~$ buzzer play mario uart:~$ buzzer play golioth uart:~$ buzzer play tiacsys