Raspberry Pi Pico Synth_Dexed? – Part 6

One last thing I definitely wanted for my PicoDexed was the option for PWM output. This post is by way of a short coda detailing how to do PWM on a Raspberry Pi Pico.

  • Part 1 where I work out how to build Synth_Dexed using the Pico SDK and get some sounds coming out.
  • Part 2 where I take a detailed look at the performance with a diversion into the workings of the pico_audio library and floating point maths on the pico, on the way.
  • Part 3 where I managed to get up to 16-note polyphony, by overclocking, and some basic serial MIDI support.
  • Part 4 for the full MIDI implementation, voice loading, SysEx control and USB-MIDI.
  • Part 5 details different options for building hardware to run PicoDexed.

The latest code can be found on GitHub here: https://github.com/diyelectromusic/picodexed

Warning! I strongly recommend using old or second hand equipment for your experiments.  I am not responsible for any damage to expensive instruments!

If you are new to microcontrollers, see the Getting Started pages.

PWM Output Circuit

The official “Hardware Design with the RP2040” guide includes a PWM circuit as follows (see section 3.4.1).

Several others have used a slightly simplified version of this circuit, essentially omitting the buffer stage (U3) and perhaps only implementing a single channel. See for example – Raspberry Pi Pico PWM Audio Output Circuit (Jeremy S. Cook) and “pico-pwm-audio” (Robin Grosset).

An alternative is this circuit used by Tod Kurt for his Circuitpython-tricks“The output circuitry to get line-out levels is a simple 10:1 voltage-divider and a 1uF capacitor to recenter the signal around 0 volts”:

I’m going with Jeremy S. Cook and Robin Grosset and using the following (diagram from here):

This is my solderless breadboard version of the above:

Raspberry Pi Pico Audio PWM

There appears to be two ways of getting audio style PWM signals out of a Pico:

  • Use the PWM peripherals.
  • Use the PIO.

It would appear that Raspberry Pi’s pico_audio library in the pico-extras repository uses PIO. I haven’t found a clear explanation as to why the built-in PWM peripherals haven’t been used.

Here are some other resources that show alternative descriptions of PWM for audion on a Pico:

As I’m using the pico_audio library for I2S audio, I’m going to use the default PWM pico_audio library too.

To initialise the pico_audio PWM library requires the following:

const audio_pwm_channel_config_t config =
{
.core = {
.base_pin = base_pin,
.dma_channel = dma_ch,
.pio_sm = pio_sm
},
.pattern = 3,
};

const struct audio_format *output_format;
output_format = audio_pwm_setup(pAudioFormat, -1, &config);
bool status = audio_pwm_default_connect(pBufferPool, false);
audio_pwm_set_enabled(true);

Everything else is the same as for I2S.

At present, overclocking the Pico causes the PWM frequencies to be messed up, so for now the recommended configuration is 8-note polyphony, 24000 sample rate and no overclocking.

The build uses GPIO 20 for PWM.

Closing Thoughts

I’ve uploaded a prototype PWM UF2 file to GitHub too now in case anyone wants to give that a go too: https://github.com/diyelectromusic/picodexed

Hopefully there is enough information in the above to get something up and running.

Kevin

Leave a comment