Raspberry Pi FM radio transmitter with Buttons
Published: 04-02-2015 | Author: Remy van Elst | Text only version of this article
Table of Contents
The PiFM project allows you to use a Raspberry Pi to send out a WAV file onthe FM band.
This is awesome because every normal radio then can receive your music/podcastwithout expensive (sonos) equipment. It also allows you to listen in any carwithout 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 roomfor three buttons. These are hooked up to a python script which allows me tostart and stop the transmission, and skip or go back songs. It also allows me toshut down the Pi instead of just pulling the power cable.
Please do note that transmitting radio like this might be restricted in yourcountry. In The Netherlands this setup (on FM bands 87,5 to 108 MHz) forbiddenbecause it has too much transmission power. Transmitting on those frequencies isallowed if your transmittor has a maximum capacity of 50nW, and this setup hasway 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 initiallybought this case because it reroutes the USB power to the front of the Pi. Imostly used it as a server back then, so having a power and a network cable atone side of the device is quite nice. The case also looks beautifull. I boughtmy case at Pi Supply.
I decided to replace the default top plate on the Short Crust with my own PMMAversion which should have place for the three buttons and easy access to theGPIO. I don't find a flat ribbon cable easy access. After receiving theinstructions and doing a few test cuts in plywood I fired up Inkscape and foundmyself a good ruler. I'd also never used Inkscape before but that is quite easyto figure out after some fiddling around.
The SVG file of the top case can be downloaded here. It was cut at power60% and speed 1.5. Below is a PNG version of the SVG:
My first attempts were not that great. Some measures were gambled the firsttime, as you can see in the below picture.
- A: the rounding on the corners was way off
- B: the big access point to the GPIO pins was in the wrong spot
The holes for the screws were also a gamble, but, that was a lucky one, the arein exactly the correct spot. The button holes were measured up and since it arethree standard R13-57 buttons that was easy.
After two attempts and correct measurements I succeeded in printing the correcttop plate. It also has my website logo in it, the resistor symbol. That is justa nice touch.
I used three R13-57 push buttons for the input. They have wires soldered on tothem and a few pieces of electrical tape so that they do not short the Pi. Thereis little space in the case.
The buttons are wired up to GPIO pin 18, 23 and 24 (and ground of course). I'veused the built in Raspberry Pi Pull Up resistor. If you are using another boardof 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, youwill get drifting otherwise.
The Python Script
The only part used from that script is the config and the ffmpeg to pifmconverting. That script has no concept of a global playlist so implementingstart, stop and skip functions in it would be harder.
I've used GPIO callbacks to handle the button input. The button functions arelisted below, where bottom is where the network and USB ports are:
- Top button: Previous Song
- Middle button: Stop / Start playing
- Bottom button: Next song
If you first press the Middle (Stop) button and then the Top (Previous) buttonthe 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 withoutcorrupting your SD card.
- Wire up three buttons to GPIO pins 18, 23, 24 and ground.
- Wire up a simple wire on GPIO pin 4 for the antenna.
- Load up Raspbian on your SD card.
- Create a folder
/rasplayerfm/and place all your music there.
Place a config file there:
[rasplayerfm] frequency = 101.0 shuffle = False repeat all = True stereoplayback = True music_dir = /rasplayerfm
Place the compiled
pifmbinary and the
RasplayerFM.pyscript there as well.
Add the following to
/usr/bin/python3 /rasplayerfm/RasplayerFM.py &
/etc/rc.localfile 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 setfrequency. With the buttons you can start, stop and skip music as describedabove.
This was an interesting project for me because it upped my knowledge andexperience with a Lasercutter, Inkscape, GPIO, soldering and Python programmingwith callbacks and external inputs.
All the code (plus SVG file) from this article is released under the GPLv3. Theimages are Creative Commons BY-NC-SA.Tags: articles, backup, debian, fm, ham, lasercutter, pifm, pirate-radio, pmma, raspberry-pi, revspace