You can use the Raspberry Pi for a variety of geeky projects. But I recently found out that you could use the puny little device as a FM transmitter as well. And that do without much tinkering.
A bunch of folks over at Code Club pihack wrote a program to use hardware on the Raspberry Pi that’s meant to generate spread-spectrum clock signals to instead output FM Radio signals.
Here’s a bit of background. Besides the familiar Audio, Ethernet, HDMI, and USB ports on the Raspberry Pi, the device also has interfaces that are designed to to connect more directly with other chips and modules.
These General Purpose Input/Output (GPIO) “ports” are the 26-pins (arranged in a 2×13 strip) you see on the board. These interfaces are not plug-and-play but can controlled through software.
To transmit a surprisingly strong FM signal, all you need is to attach a wire to the GPIO 4 pin. Even without the wire, the FM signal broadcast by the Raspberry Pi will be picked up by nearby FM receivers.
A word of caution: The laws for broadcasting FM signals varies from country to country. Please check your local regulations before attempting this project.
Get, set, go
Power up the Pi and head to the command-line either via SSH or by directly logging into the device.
We’ll now grab the code written at the Code Club hackfest and extract it:
mkdir ~/pifm cd pifm wget http://omattos.com/pifm.tar.gz tar zxvf pifm.tar.gz
The directory now contains six files. Surprisingly that’s all there’s to it. You can now broadcast the included “sound.wav” file:
sudo ./pifm sound.wav 102.3
Now grab a FM receiver and set it to FM 102.3 and you should hear the Star Wars theme music. You can actually change the broadcast frequency from anywhere between 88 MHz and 108 MHz simply by appending the channel frequency at the end of the command.
You can play other audio files as well, but they must be 16-bit 22050 Hz mono and in the WAV format only. That might seem like a real limitation but it really isn’t. All you need is the SoX sound exchange audio editor which will process your MP3 file and convert it into the WAV file on-the-fly.
Install the audio editor and its dependencies with:
sudo apt-get install sox libsox-fmt-all
When it’s done, type in the following command, substituting “SomeSong.mp3” with the name of the MP3 file you wish to play:
sox -t mp3 SomeSong.mp3 -t wav -r 22050 -c 1 - | sudo ./pifm - 102.3
The first part of the command translates the MP3 file into a WAV file, changes its audio sampling rate to 22050 and down-mixes the track to mono. The converted track is then sent to the standard output, denoted by the hyphen sign (-) and is then piped (|) into the standard input of the pifm command.
The only difference in the
pifm command in the above example is that instead of specifying the name of the file to broadcast, we are asking the script to instead broadcast the standard input.
If you’ve still got your FM receiver tuned to the 102.3 frequency, you should now hear your MP3! Again, just like the original example, feel free to replace the frequency to anywhere between 88 MHz and 108 MHz.
You can do some wonderful things with SoX. You can, for example, use it to broadcast your favourite streams live from the Internet.
To broadcast the Linux Voice podcast:
sox -t mp3 http://www.linuxvoice.com/episodes/lv_s02e01.mp3 -t wav -r 22050 -c 1 - | sudo ./pifm - 102.3
The only difference between this command and the previous example is that instead of pointing to a local MP3, you are now pointing to one that resides online.
You can similarly broadcast an online radio station. Many stations publish a M3U file and you can point to it:
sox -t mp3 http://www.ndr.de/resources/metadaten/audio/m3u/ndrloop5.m3u -t wav -r 22050 -c 1 - | sudo ./pifm - 102.3
Not all stations broadcast MP3 streams. Some use other formats such as OGG. All you need to change the type of the input file you are converting with sox. Such as:
sox -t ogg http://network.absoluteradio.co.uk/core/audio/ogg/live.pls?service=a6bb -t wav -r 22050 -c 1 - | sudo ./pifm - 102.3
If you’re looking for online radios, here’s a huge list of European radio stations that stream online.
Image credit: Robert Ashworth