Skip to content

Add PWM speaker output for RP2040 and RP2350 - #5

Merged
deadprogram merged 2 commits into
mainfrom
pwm-speaker
Oct 10, 2026
Merged

deadprogram merged 2 commits into
mainfrom
pwm-speaker

Conversation

@deadprogram

Copy link
Copy Markdown
Member

Adds a `pwm` package that plays audio on a speaker or passive piezo buzzer through a PWM pin, as an alternative to an I2S DAC. It implements `tinyplayer.Output` so the player needs no changes.

  • Mono, about 11 bits, with the PWM carrier at 40 kHz or above. Lower sample rates repeat each sample to keep the carrier inaudible.
  • DMA writes the duty values, paced by the PWM wrap DREQ, so there is no CPU work per sample.
  • An optional second pin on the same slice gets the inverted signal, for driving a piezo across two pins.
  • Build the examples with `-tags speaker` to use GPIO2 and GPIO3.
  • README covers wiring for a piezo, a transistor driven speaker and an amplifier, and notes that active buzzers only play tones.
  • Adds a Japanese speech example sound made with VOICEVOX.

Tested on a xiao-rp2350 with a DFRobot 386AMP module and speaker on D8. All example sounds play at the right speed at 8, 16 and 44.1 kHz. Unit tests cover the sample conversion and timing, and the smoketest builds the speaker example for pico and xiao-rp2350.

The pwm package plays mono audio as a PWM duty cycle, fed by DMA paced by
the PWM wrap. The carrier stays at 40 kHz or above. Build the examples with
-tags speaker to use it on GPIO2 and GPIO3.
Made with VOICEVOX. generate.sh makes it again when VOICEVOX is set.
@deadprogram
deadprogram merged commit a9d46d6 into main Oct 10, 2026
1 check passed
@deadprogram
deadprogram deleted the pwm-speaker branch October 10, 2026 08:11
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.

1 participant