Skip to content

Include pico_tone library#70

Open
jcarranz97 wants to merge 2 commits into
raspberrypi:masterfrom
jcarranz97:include-pico-tone
Open

Include pico_tone library#70
jcarranz97 wants to merge 2 commits into
raspberrypi:masterfrom
jcarranz97:include-pico-tone

Conversation

@jcarranz97

Copy link
Copy Markdown

Fixes #69
This PR was previously made against pico-sdk but now requesting to include the library as part of pico-extras as per @lurch recommendation.

This PR includes tone functions to generate tones using PWM.

The tone function is similar to tone() from Arduino. I have also included a no_tone function to silence buzzer if needed.

Inspiration and References:

Comment thread src/rp2_common/pico_tone/tone.c Outdated
@jcarranz97

Copy link
Copy Markdown
Author

Hi! Just wanted to check in on this PR — it's been open for a while and I wanted to make sure it hasn't been forgotten. Is there anyone actively maintaining this repository? Do you think this change could eventually be merged, or would it be better to close it? No rush at all, just trying to keep things tidy. Thanks!

* Adds support for playing tones using PWM.
*
* Every sound humans encounter consists of one or more frequencies, and the
* way the ear interprets those sounds is called pitch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* way the ear interprets those sounds is called pitch.
* way the ear interprets those sounds is called pitch.

}

void tone(uint gpio, uint freq, uint32_t duration_ms) {
// Calculate and cofigure new clock divider according to the frequency

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Calculate and cofigure new clock divider according to the frequency
// Calculate and configure new clock divider according to the frequency

Comment on lines +26 to +27
// This formula is assuming we are running at 125MHz.
// TODO: Make this work for any frequency

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to fix this TODO ? You can use clock_get_hz(clk_sys) to get the actual system clock frequency.

pwm_set_clkdiv(slice_num, clkdiv);
// Configure duty to 50% ((2**16)-1)/2) to generate a square wave
pwm_set_gpio_level(gpio, 32768U);
sleep_ms(duration_ms);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit odd for this function to accept a duration_ms parameter, and then sleep for that time, but then leave the tone running afterwards?? Surely it makes more sense to just do

    pwm_set_gpio_level(gpio, 32768U);
    sleep_ms(duration_ms);
    pwm_set_gpio_level(gpio, 0);

and then get rid of the separate no_tone function?

@lurch

lurch commented May 26, 2026

Copy link
Copy Markdown
Contributor

Do you think this change could eventually be merged

It might get merged eventually, but pico-extras and pico-playground are very low on our priority list, and we have more important things to be working on at the moment 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include library to generate tones

2 participants