04-02-2015 | Remy van Elst | Text only version of this article
The PiFM project allows you to use a Raspberry Pi to send out a WAV file on the FM band.
This is awesome because every normal radio then can receive your music/podcast without expensive (sonos) equipment. It also allows you to listen in any car without the need for a radio that handles USB or Bluetooth.
I've used a lasercutter to craft a new top plate for my Pi which has room for three buttons. These are hooked up to a python script which allows me to start and stop the transmission, and skip or go back songs. It also allows me to shut down the Pi instead of just pulling the power cable.
If you like this tutorial and want to support my website, use this link to order a Digital Ocean VPS. You will get $10 free credit, which is equal to a free VPS for two months: https://www.digitalocean.com/?refcode=7435ae6b8212
Please do note that transmitting radio like this might be restricted in your country. In The Netherlands this setup (on FM bands 87,5 to 108 MHz) forbidden because it has too much transmission power. Transmitting on those frequencies is allowed if your transmittor has a maximum capacity of 50nW, and this setup has way more than that. Source: Agentschap Telecom.
A small demo, shot with a phone and an Android app for lack of better equipment:
The base of my case is a Short Crust case for the Model B. I initially bought this case because it reroutes the USB power to the front of the Pi. I mostly used it as a server back then, so having a power and a network cable at one side of the device is quite nice. The case also looks beautifull. I bought my case at Pi Supply.
I decided to replace the default top plate on the Short Crust with my own PMMA version which should have place for the three buttons and easy access to the GPIO. I don't find a flat ribbon cable easy access. After receiving the instructions and doing a few test cuts in plywood I fired up Inkscape and found myself a good ruler. I'd also never used Inkscape before but that is quite easy to figure out after some fiddling around.
The SVG file of the top case can be downloaded here. It was cut at power 60% and speed 1.5. Below is a PNG version of the SVG:
My first attempts were not that great. Some measures were gambled the first time, as you can see in the below picture.
The holes for the screws were also a gamble, but, that was a lucky one, the are in exactly the correct spot. The button holes were measured up and since it are three standard R13-57 buttons that was easy.
After two attempts and correct measurements I succeeded in printing the correct top plate. It also has my website logo in it, the resistor symbol. That is just a nice touch.
I used three R13-57 push buttons for the input. They have wires soldered on to them and a few pieces of electrical tape so that they do not short the Pi. There is little space in the case.
The buttons are wired up to GPIO pin 18, 23 and 24 (and ground of course). I've used the built in Raspberry Pi Pull Up resistor. If you are using another board of some kind please check if there is a pull up/down resistor built in, otherwise you need to place one in between the button and your GPIO pin, you will get drifting otherwise.
The only part used from that script is the config and the ffmpeg to pifm converting. That script has no concept of a global playlist so implementing start, stop and skip functions in it would be harder.
I've used GPIO callbacks to handle the button input. The button functions are listed below, where bottom is where the network and USB ports are:
If you first press the Middle (Stop) button and then the Top (Previous) button the Raspberry Pi will shut itself down using the
shutdown -h now command. After a few seconds you can safely disconnect the power of the Pi without corrupting your SD card.
/rasplayerfm/and place all your music there.
Place a config file there:
# /rasplayerfm/rasplayerfm.conf [rasplayerfm] frequency = 101.0 shuffle = False repeat_all = True stereo_playback = True music_dir = /rasplayerfm
Place the compiled
pifm binary and the
RasplayerFM.py script there as well.
Add the following to
/etc/rc.local (above the
exit 0 line):
/usr/bin/python3 /rasplayerfm/RasplayerFM.py &
/etc/rc.local file as executable:
chmod +x /etc/rc.local
Tune your FM radio to the frequency set in the config file.
Reboot the Pi.
Wait a few moments.
When you boot the Pi up it will automatically start playing on the set frequency. With the buttons you can start, stop and skip music as described above.
This was an interesting project for me because it upped my knowledge and experience with a Lasercutter, Inkscape, GPIO, soldering and Python programming with callbacks and external inputs.
All the code (plus SVG file) from this article is released under the GPLv3. The images are Creative Commons BY-NC-SA.