Saturday, 1 November 2014

Using PiFM to transmit audio on any frequency up to 250 MHz

The first "hack" which allowed experimenters to use the Raspberry Pi's clock pin (GPIO 4) to transmit audio directly from the Raspberry Pi came from the Imperial College Robotics Society . Their "PiFM" module has since been modified and improved and is now capable of transmitting in 16 Bit Stereo on any frequency from about 1 MHz up to 250 MHz.

It sounds too good to be true.... so I decided to try it out for myself!

WARNING - DO NOT connect your Raspberry Pi's clock pin (GPIO 4) to any kind of antenna (even a short length of wire) when performing any of these tests!

Full details of the "PiFM"  module may be found here, and you can download it as a ".tar.gz" archive here. Once you have downloaded the archive to your Raspberry Pi, extract the files, open a terminal window on your Raspberry Pi and go to the folder containing the extracted files (in my case the Downloads folder). The folder should contain the flollowing six files:

left_right.wav           - sample stereo ".wav" file
sound.wav               - sample music ".wav" file (Star Wars theme)
pifm.c                       - pifm "c" source code
pifm                          - compiled pifm module
PiFm.pyc                 - Python bytecode for pifm                   - Python script for pifm

Position a VHF portable radio close to your Raspberry Pi and tune it to a frequency where there is no transmission (in my case 100.0 MHz). Type the following at the command line prompt in the terminal window (but enter whatever frequency you want to use) and press Enter:

pi@raspberrypi ~/Downloads $ sudo ./pifm sound.wav 100.0_

Almost immediately you should hear the Star Wars theme playing on your portable radio. You can stop playback by pressing "Ctrl+C" or you can let the music play right to the end of the ".wav" file. However.....PLEASE NOTE that in either case your Raspberry Pi will continue to transmit a carrier on the frequency you have chosen until it is rebooted or turned off, so please DO NOT leave your Raspberry Pi transmitting a carrier after you have finished experimenting!

I decided to have a look at the spectrum of the transmitted RF signal, however my spectrum analyzer only covers up to 60MHz so I set the Raspberry Pi to generate a 30MHz signal. The spectrum is shown below:

You can see the "fundamental" signal at 30 MHz, but there are multiple strong subharmonic and harmonic spikes all over the RF spectrum. I also noted that if I tuned my receiver to any multiple of 30 MHz (e.g. 60MHz, 90MHz, 120MHz etc) I could clearly hear the transmitted signal, sometimes very strongly.

Therefore please DO NOT connect your Raspberry Pi's clock pin (GPIO 4) to any kind of antenna when performing any of these tests, because you will generate unwanted and potentially dangerous RF energy right across the electromagnetic spectrum.

Whilst it is very easy to use the Raspberry Pi's clock pin (GPIO 4) to generate an RF signal, and even transmit audio, the usefulness of this hack is severely limited by the huge amount of harmonic and subharmonic distortion in the signal (because it is basically a square wave). I've read many articles which suggest the Raspberry Pi may be a used as a low power transmitter for the amateur radio bands simply by using a low pass filter on the output, but I'd say even this is insufficient (due to the subharmonic distortion in the generated signal). To make any practical use of any RF signal generated by the Raspberry Pi in this way would require very good band pass filtering.