The AM Forum

THE AM BULLETIN BOARD => Class E Forum => Topic started by: ka1tdq on November 23, 2020, 12:40:59 PM

Title: PWM on the Raspberry Pi
Post by: ka1tdq on November 23, 2020, 12:40:59 PM
I can muddle my way through the Raspberry Pi. Anyone know of a good cookie cutter program to do audio PWM on it?


Title: Re: PWM on the Raspberry Pi
Post by: ka1tdq on November 24, 2020, 09:51:45 AM
I've searched the web and found nothing.

If someone on the forum is able and willing, I'll pay a reasonable fee for someone to write code to modulate an LED with a square wave up to 4 KHz, with a variable carrier frequency, according to an analog input. It would be plug-and-play as a PWM generator.


Title: Re: PWM on the Raspberry Pi
Post by: w8khk on November 24, 2020, 10:22:49 AM
The Raspberry Pi does not have any analog inputs, only digital.  An external ADC would be needed to process analog inputs.  So you would be looking at both hardware and software development.

A maximum frequency of 4 kHz for the PWM clock would not provide even an approximate rendering of a typical audio signal.  Or did you mean an input analog signal sampled up to 4 kHz?

You might consider using either an ESP-32 or better yet a fast Teensy 4.1, as they both have analog inputs and PWM outputs, and would probably be a better fit for your application.

But a much simpler approach would be to implement the analog to PWM conversion using a switching power supply IC, as is commonly done on many PWM generator designs for amateur radio use. 

Steve has provided a tried and true solution on the Class-E site.

Title: Re: PWM on the Raspberry Pi
Post by: ka1tdq on November 24, 2020, 10:59:42 AM
I meant modulating the square wave up to 4 KHz, or the upper range of the human voice, not sampling rate of the analog signal.

The analog input board is cheap.

Surely someone should be able to do it. I'm not a code guy.

Yes, I can get Steve's board very easily or build it and I could analog modulate the rig. I'm just looking into maybe trying something new (for example every project I've ever done).


Title: Re: PWM on the Raspberry Pi
Post by: steve_qix on November 25, 2020, 01:21:04 PM
Hi Jon,

A 4kHz switching rate is WAY WAY too low to resolve even low quality voice.

You must switch, at MINIMUM at twice the highest audio frequency you want to resolve (Nyquist theorem).

Furthermore, the PWM generator ____MUST____ have a sharp, low pass filter ahead of it, or you will get aliasing, caused by harmonics of the modulating frequency that will mix with the switching frequency and cause really bad artifacts in the audio range.

The other problem comes about when you are converting the PWM to an analog signal.  The output filter can only be so sharp.  Even a 6 pole filter should have its corner frequency well above anything we really care about audio-wise.  I typically set this to somewhere in the vicinity of 12.5kHz.  The anti-aliasing filter corner (at least for my designs) is 5.7kHz.

However, if you are only going to use this to modulate visual devices (such as an LED),  _and_ never demodulate this into audio, you could use a low sample rate because distortion and quality are not important in this case.  You could probably get away with a very modest anti aliasing filter as well.

Talking about switching frequencies, the Gates (Harris) MW5 PWM transmitter used a 70kHz sample rate to resolve a high end of 10kHz.  I built such a transmitter way back in the day (a tube PWM transmitter), and I have to say, that 70kHz was hard to filter to the point where it was down at least 70dB.  The rate was kept low because tube PWM transmitters have issues at high switching frequencies....  If you're ever interested, I could elaborate.

With modern solid state devices, switching frequencies of hundreds of KHz or even MHz are possible and practical.

The designs I publish use a 160kHz switching frequency.  This is a very good compromise between easy output filtering (high frequency switching is easy to filter out with modest L/C filter designs), and still resolving a 1% duty cycle pulse without too much pulse distortion (pulse modification, which gets worse at high switching frequencies).

Title: Re: PWM on the Raspberry Pi
Post by: ka1tdq on November 25, 2020, 05:48:39 PM
Hey Steve,

Thanks for the detailed response. I mentioned modulating an LED because your design of the PWM output board uses an opto-isolator (like an LED but not quite). It was just a simple way to put the request.

I get what you're saying about the low pass input filtering and output filtering. And from looking at your PWM generator board schematic, the Raspberry Pi would only be replacing a small section of the whole board. It'd be engineering overkill for what needs to be done other than being able to tinker with sampling rates with just programming. For amateur use though, nobody would even notice. This probably isn't worth the hassle but the discussion is good.

The tube PWM's are neat. I can't think of a good horse and buggy / formula one race car analogy, but you get the idea. Quite the engineering feat.

My new E rig is coming slowly but getting there. Honestly, motivation comes in fits and starts. I'm about ready to test the scaled down VFO / phase splitter / duty cycle adjuster. That's really the hardest part and an area where you can make soldering mistakes. Once I get a good output from that, the rest is easy with just drilling and blasting (some soldering).

I'm hoping that the waveforms come out as clean as my last 8-FETter. They should since I'm being smarter about shielding and component coupling/placement.

AMfone - Dedicated to Amplitude Modulation on the Amateur Radio Bands