Arduino Leonardo Setup for PCM-Audio (8-Bit-PWM,62.5kHz) with fSR=12.5kHz?


Whileas I can set this up (see subject) for an Arduino Uno (PWM-Out on D11 with 62.5kHz, Timer1 for the sample-output with 12.5 kHz (*)) I seemingly cannot for the Leonardo. I suppose that this is not such an uncommon setup so I did a search for a working example on IRQ-driven audio-out on the Leonardo, so I can see where my errors/misassumptions are… but… well… I didn’t find anything… hmm,… Maybe someone here has used the Leonardo for a similar setup and can help me out?


(*) My experiments with the Uno clearly show, that for clear pcm-pwm-audio you will need to stay in sync with the PWM-rate. So you MUST use fSR = (fPWM/n) with n=[1,2,3,4…] as otherwise you will have really bad aliasing in the output… Using phase-correct PWM or not does not matter at all. The later is an urban-myth…

OK, I found it out by myself…

To prevent anyone else from extensive research here is a setup for using the Leonardo like described above.

The reason for all this: I want to port a MIDI-controlled speech-synth from the Arduino UNO to the Leonardo for having direct MIDI over USB without the hassle…


leonardo_irq_pcm.ino (12.5 KB)

Using phase-correct PWM or not does not matter at all. The later is an urban-myth...

Not a myth at all, this is easily simulated and very obvious in distortion or IM plots.
You can do the maths too if you want - not for the squeamish though!

BTW if you want the best quality from ultrasonic PWM audio output, noise-shaping is usually needed,
this will reduce audible noise and increase the effective number of bits of resolution.

See my post #5 in this thread:

I ran a simulation of fast mode v. phase-correct mode to show the spectral difference,
this is with a notional 48kSPS, 48kHz PWM at 256 levels, and two-tone input (1.1kHz + 1.5kHz):

green is the input signal, cyan is fast mode out, red is phase-correct mode out. Very
different levels of 2nd and 3rd order distortion products as might be expected with the
high levels of phase-intermodulation from unbalanced PWM.

The worst peak for fast mode is only 28dB down, compared to 55dB down for phase-correct...

Not urban myth at all, plain DSP maths.

[ edit: for completeness, a single-tone test:


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.