Fast change of PWM amplitude

Hello, I was attempting to create an analog to pwm converter using Arduino that could be usable to drive sound. I managed to find code for very fast analog reads for my arduno zero allowing for hundreds of thousands of reads per second, and now I am wondering how could I use pwm or pwm like signal to output these values.

Well, for one thing, there is no way to change a PWM every fraction of a pulse, which is what you are asking for.

Not really, as I would increase Pwm frequency which is not too difficult to do, bigger issue I’d configuring duty cycle fast enough.
I plan to run at about 44100 kHz or as close as I can get. The MCU is in theory fast enough as 44100khz * 255 is below 48MHZ it runs at

But, unless you change coherently at the end or beginning of a PWM pulse, you automatically create a glitch that will be heard.

Indeed. That’s why I’d want to use an interrupt function after each pwm cycle ends. Which may decrease duty cycle a bit but how much I’ll see. Still I want to try it.

For proper use of PWM, we use a fixed, constant frequency. I think what you want can't be done with PWM.

I do want to use a constant fixed frequency, just change duty cycle every cycle. I was thinking of using PWM interrupt and fastpwm but neither work with Arduino zero out of the box

So I managed to get a working code but connecting a slightly higher power output device makes it stuck.

Sorry, but I cannot see what you have done.

I think you would be better to write a function to output pulses of various widths rather than using PWM. (It just seems to me that PWM is the wrong tool to use.)

PWM (Pulse Width Modulation) is a technique used in electronics to control the power output of a circuit by rapidly switching between a high and low voltage state. The amplitude of the PWM signal can be changed by varying the duty cycle, which is the percentage of time that the signal is in the high state.

Bit late for a reply, but I ended up finding out that fortunately the way samd21 handles timer registers, it only applies changed every cycle, no matter when do I write them, so no glitches happen.
I managed to get fully working code with configurable duty cycle and frequency, and it’s not bad at reproducing sounds, I published it GitHub - Bexin3/Speeduino: Make your SAMD21 Arduino functions faster. , though right now I mostly switched to writing low level code for stm32h747 in the new giga.

1 Like

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