# How does the RaspberryPi radio hack convert digital GPIO signals to an FM signal?

After having looked at various explanation of how the RaspberryPi FM transmitter hack works, I realize there are some fundamental pieces missing. Most explanations talk about how to construct a digital signal from an analog one by Fourier's theorem, but never how the digital GPIO (3.3 Vpp) is constructed and used to modulate an FM signal.

1. How the reverse operation is obtained, if even necessary?
2. How is the digital carrier used (it's frequency)
3. How is the "analog" audio modulated on top of that?

I think a picture or more of how the resulting FM signal is built by the non-amplitude varied digital signal would be a helpful answer. I wish I had a proper DSO to see this myself, but I don't.

Note: In that hack audio transmission can be made specifically in the 1-250 MHz region, but the harmonics can be clearly heard all the way up into 1.1 GHz region. From comments here.

The closest explanation I can find is that of using PWM to Analog like in this picture:

(Figure 11.5. Credit: Mark Fedkin modified after Dunlop, 2010)

But this doesn't halp much since it doesn't explain how the other half of the sine wave (the negative values) are generated?

• There are a number of FM modes and generation techniques. The document does not specify them either, for example are they generating narrowband FM or wideband FM? Which methd do they implement (as an approximation) such as direct FM generation or via indirect methods? Basically an on off switching or a PWM switching would result in a multiplication which produces the fundamental band + the harmonics. Even though a very exciting idea, I would like to append an FM tranmission hardware for proper way of analog transmission. These days FM tranmission hardware is surprisingly abundant! – Fat32 Mar 20 '17 at 10:19
• I got an additional hint here: Modulation is done by adjusting the frequency using the fractional divider between 103.325Mhz and 103.275Mhz, which makes the audio signal. – not2qubit Mar 20 '17 at 10:38

I just skimmed

https://github.com/SaucySoliton/PiFmRds/blob/master/src/pi_fm_rds.c#L454

and from what that code looks like, it initializes a clock generator to run at an adjustable clock.

Then, it uses the audio amplitudes to modify that clock's frequency in real time. PWM doesn't seem to be involved, aside from the program using the PWM unit to generate the regular event needed to trigger the reprogramming of the fractional interpolator (which is the aforementioned clock generator) by triggering a DMA transfer of the control word to the PLL control register.

This probably doesn't tell you overly much; think of it as

• like in an analog FM transmitter, the audio amplitude is used to in- or decrease the oscillator's frequency
• because you need exact timing to do that, the program makes use of the Pi's PWM unit to generate an event every audio sample period
• because getting data from CPU core to memory-mapped things like the oscillator's controller costs a lot of CPU cycles you need for other things, there's a dedicated unit in the Pi that does that transfer for you, and can be kicked off when you need it.
• How does this answer coincide with that of Dan? A first impression seem contradictory. – not2qubit Mar 20 '17 at 17:52
• I think Marcus' answer is more specific to what they actually did, while my answer was how it could have possibly been done. Looking at it, the Pi has a PWM as a spread spectrum clock control which was used to generate the FM signal directly. The SD modulator I show is a similar PWM control, except in this case the Pi must limit the range of the zero crossings to a narrow range. Such control is typically used to reduce emissions by spreading clock signals (phase mod as we want in FM), and reducing the power density. So with this PWM they were able to use it directly as an FM tx- clever. – Dan Boschen Mar 20 '17 at 18:18
• @DanBoschen Thanks for your great feedback. I accepted Marcus answer, but have up-voted you both as you have excellent comments and valuable explanations. – not2qubit Mar 21 '17 at 9:34

First, the answer to why you do not see a negative voltage is that the output being digital will range from 0 to the maximum digital voltage at the output (+Vs). This will have a DC offset of +Vs/2 which is simply filtered out with a high pass filter (series cap) resulting in a bipolar waveform with negative voltage after the series cap.

UPDATE: Based on Marcus' link that he provided in his answer, what follows is NOT how they actually did the FM transmitter in the Rasberry PI, but still valid as a possible approach. The actual implementation appears to be much simpler, leveraging an existing PWM spread-spectrum clock controller that is in the Rasberry-Pi. This device is typically used to help pass emission standards by reducing maximum transmitted emissions by spreading them, reducing overall power spectral density. Thus such a device could be used for FM transmission, in that it achieves it's spreading by changing the zero crossings (slightly) of the clock signal being spread. Thus if you can access the control mechanism for how the zero crossings are varied, you can achieve an FM transmitter.

The original response is below to show what could be done if a dedicated spread spectrum PWM clock controller was not available:

The implementation could be done with a numerically controlled oscillator in combination with a sigma-delta DAC. The NCO can provide precise frequency control as well as AM, FM, or PM modulation (you are using FM), and the sigma delta DAC can provide a multi-level analog output from just 1 digital bit.

Since FM is only concerned with zero crossings, it is also feasible that the sigma-delta is completely omitted for a very simple demonstration application in which case the NCO is implemented with a 1 bit output. This would have significant jitter however as the zero crossing will be limited to the crossings of your actual clock vs what is needed in the emulated waveform (as well as high spectral noise requiring more stringent analog filtering). Higher clock rates will reduce this but much more efficient to implement the sigma-delta architecture and in the process minimize that jitter; which is phase noise, which in turn is FM noise; however that jitter is easily quantified/predictable and perhaps with a sufficiently high clock can be shown to not be a concern.

In this post I give more details on the NCO specifically.

Numerically Controlled Oscillator (NCO) for phasor implementation?

As for generating the analog output with modulation, the NCO is combined with a DAC as shown in the figure below which also shows all the modulation knobs (then commonly called a DDS or Direct Digital Synthesizer):

I have not reviewed the Rasberry pi implementation specifically to know if you are making use of an actual n-bit DAC, or if that portion is implemented with a PWM style DAC (Sigma-Delta Modulator), or if the MSB of the phase accumulator is used directly (either of the latter two is what I suspect based on the sketch you made describing the output). In the case of a Sigma Delta DAC, it could be implemented with a pass-band architecture such that the noise shaping is done to maximize the effective number of bits in your FM band (rather than a low-pass approach which would necessitate a very high sampling rate).

Further considerations for DAC reconstruction are given below, not directly related to a Sigma Delta approach but do show how it is possible to generate higher frequency outputs above the sampling rate of the DAC, based on post-filtering after the DAC output.

A very simple Sigma Delta DAC implementation is shown in the figure below. Combining this with the NCO structure above to generate the sample by sample digital word, using the FCW port as the FM modulation control can create an FM signal. As suggested above however, instead of a digital accumlator which is a low pass structure, an implementation can be done with a passband structure that would then allow selective noise shaping to suppress the quantization noise within your FM band of interest:

• That's one heck of an answer. Thank you. I'll have to read this a few dozen times and I still wish I could see the wave forms in each stage to better understand what happens. – not2qubit Mar 20 '17 at 17:51
• Looks like from Marcus' answer and the reference he gave that they did not need to go through this trouble since the device had a dedicated PWM controller that could be used directly as the FM transmitter. The PWM in this regards provides a direct zero crossing control based on the amplitude of the modulated signal and can be set to be centered on any carrier frequency in the FM band (it is a spread spectrum clock controller, so instead of spreading the clock with pseudo-random data, you can "spread" (move the phase) with your signal. – Dan Boschen Mar 20 '17 at 18:21