8

A while ago, a few guys figured out that they could transmit FM signals using the Raspberry Pi's GPIO ports, and another person realized that he could use the RasPi for controlling his home automation equipment:

http://www.skagmo.com/page.php?p=projects/22_pihat

This is the file that controls the radio for this:

https://github.com/s7mx1/pihat/blob/master/radio.c

Now, I would like to port this to a language that's easier than C for me to experiment with, like Go or Python. However, I am not really clear on how this is done.

Skagmo uses the harmonics to generate a frequency of 433 MHz. Say you only want a frequency that's 100 MHz, for simplicity, how do you generate that? From what I understand, it has something to do with a GPIO clock (or maybe SPI? I am not sure).

From what I can see in the file, he sets three bits on some GPIO function selection register, and then initializes the clock with a struct, and then sets a bit whenever he wants to transmit high or low.

Is this in the carrier wave? Where is the carrier wave?

I also found this Python script that claims to do the same thing, but I'm not sure if it uses the native transmitter or if the person connected an external one to the RasPi.

Basically, I would really appreciate an explanation or a short reference on how this works, exactly, and if Python/Go are fast enough to transmit signals that can mimic my garage door remote (ASK-modulated, it seems), or if I have to do it in C.

goldilocks
  • 56,430
  • 17
  • 109
  • 217

2 Answers2

3

There may be radio frequency bands in your country where very low power unlicensed radio emissions might be permitted (FCC Part 15 rules in the U.S.). However an unfiltered GPIO output won't broadcast in only one RF band.

The GPIO transmitter trick uses a periodic digital output to produce a radio signal. This is due to one of Fourier's theorems which proves that a non-sinusoidal but periodic signal can be decomposed into many sinusoidal components (harmonics). Connect a sinusoidal component to a half-wave length antenna and it will emit some RF energy. But, to cut-out energy in frequency bands other than the intended one, a low-pass filter or a band-pass filter needs to be used between the GPIO pin and the antenna to remove RF energy at all the unintended frequencies (all those other frequencies included in the Fourier decomposition of your Pi's periodic GPIO digital output waveform).

You don't want your Pi to interfere with any higher frequency emergency services radio bands, which could get one into legal trouble.

Added: FM means frequency modulation, e.g. changes (modulations) in the frequency of the RF carrier represent information about the modulating input (audio amplitude in one common case). The Pi has control registers that can change the periodic timing of the GPIO output pin. Changes in periodic timing are also changes in frequency. Change this digital frequency at just the right times (when the audio signal changes amplitude, etc.) and Fourier transform of this digital waveform will also change its frequency spectrum (higher or lower, e.g. be frequency modulated or an FM signal). DMA is sometimes used to change the Pi's timing registers, since DMA can happen often enough to match changes in an audio files amplitude at the audio sample rate, or a multiple thereof.

Some textbooks on radio design will contain far more details about how the spectrum needs to change to meet various broadcast standards (several chapters more than fit here). Knowledge of audio DSP and how the Pi's control registers and DMA works may also be necessary (again, a few chapters more than fit here).

hotpaw2
  • 261
  • 2
  • 9
  • 2
    Although this is the most correct answer, the explanation about the Fourier decomposition, does not explain how the *digital* GPIO signal is converted to FM, which people believe are analog. I'm sure a lot of people would like to visually better understand how the signal is modulated into FM. – not2qubit Mar 20 '17 at 09:19
2

while recognizing this as a funny and great hack, i'd advise against using it for anything except the proof of concept demo, because broadcasting on unlicensed frequencies might break the law and/or result in legal problems and/or heavy penalties.

please, use only licensed equipment to broadcast within the permitted frequency ranges and output power limits. you'll never know if there's a pacemaker-equipped person in your neighbourhood.

Raspberry Pi has clock generators, that can output square wave to the GPIO pins. If you program clock generator to the desired frequency you'll get the signal, and when you change the frequency, the signal becomes a frequency modulated (FM) radio. Bad points of this approach are: 1) square wave is very noisy -- plenty of harmonics and other frequencies are transmitted, 2) RasPi can output a lot of RF power, blocking some other transmissions in the very wide frequency spectrum.

Ghanima
  • 15,578
  • 15
  • 58
  • 113
lenik
  • 11,503
  • 1
  • 29
  • 37