Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
.. _buzzer-sample:
Buzzer
######
Drive a buzzer or speaker using the PWM API.
Overview
********
This is a sample app which drives a buzzer or speaker using the
:external+zephyr:ref:`PWM API <pwm_api>`.
The sample plays a short beep with 1108.73 ㎐ (music note `D♭₆`_) for the
time of a ⅛ `music note`_ at boot time and offers the special shell command
:program:`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
:dts:`pwm-buzzer` compatible (part of Zephyr upstream) and setting its node to
the alias :dts:`buzzer0`. You will need to do something like this:
.. code-block:: devicetree
/ {
aliases {
buzzer0 = &pwm_buzzer0;
};
pwm_buzzer0: pwm_buzzer0 {
compatible = "pwm-buzzer";
pwms = <&pwm0 1 PWM_HZ(2700) PWM_POLARITY_NORMAL>;
label = "PWM_BUZZER";
};
};
Note that a commonly used period value is 880 ㎐, twice the concert pitch
frequency of 440 ㎐.
The sample supports the following platforms (located
in :bridle_file:`samples/buzzer/tests.yaml`):
.. table-from-tests-yaml::
Building and Running
********************
.. zephyr-keep-sorted-start re(^\* \w)
* On |Maker Nano RP2040| board:
.. zephyr-app-commands::
:app: bridle/samples/buzzer
:build-dir: buzzer-cytron_maker_nano_rp2040
:board: cytron_maker_nano_rp2040
:snippets: "usb-console"
:west-args: -p
:flash-args: -r uf2
:goals: flash
:host-os: unix
* On |Maker Pi RP2040| board:
.. zephyr-app-commands::
:app: bridle/samples/buzzer
:build-dir: buzzer-cytron_maker_pi_rp2040
:board: cytron_maker_pi_rp2040
:snippets: "usb-console"
:west-args: -p
:flash-args: -r uf2
:goals: flash
:host-os: unix
* On |PicoBoy| board:
.. zephyr-app-commands::
:app: bridle/samples/buzzer
:build-dir: buzzer-picoboy
:board: picoboy
:west-args: -p
:flash-args: -r uf2
:goals: flash
:host-os: unix
.. zephyr-keep-sorted-stop
Sample Output
=============
(text in bold is a command input, text in angle brackets are keys to press)
.. container:: highlight highlight-console notranslate
.. parsed-literal::
\*\*\* Booting Zephyr OS build |zephyr_version_em|\ *…* \*\*\*
[00:00:00.003,000] <inf> buzzersh: Buzzer shell is ready!
:bgn:`uart:~$` **<Tab>**
:bcy:`bridle buzzer clear date device devmem gpio help`
:bcy:`history kernel log pwm rem resize retval shell`
:bgn:`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.
date : Date commands
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.
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer -h**
buzzer - Buzzer related commands
Subcommands:
info :Get buzzer info
beep :Use buzzer to beep
play :Play one of predefined sounds
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer info**
Warning: not yet implemented.
:bgn:`uart:~$` **buzzer beep**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`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
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play folksong**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play xmastime**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play funkytown**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play mario**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play golioth**
.. container:: highlight highlight-console notranslate
.. parsed-literal::
:bgn:`uart:~$` **buzzer play tiacsys**
References
**********
.. target-notes::
.. _D♭₆: https://en.wikipedia.org/wiki/D%E2%99%AD_(musical_note)
.. _music note: https://en.wikipedia.org/wiki/Musical_note
55022eb50This file is pinned to the selected release. Download the original to open it in your design tool.