post

(Raspberry)Pi-rate radio

I’m late to the game, but I bet I’m not the only one. It turns out that there’s a very easy way to set up your old (version B, and presumably version A) Raspberry Pi as an FM radio transmitter.  Here’s how I did it and what I used it for.

Use your friendly search engine

If you search for pifm, you’ll find all the information you need to turn an older model Raspberry Pi into an FM transmitter.  In fact, you’ll find the same information, copied almost verbatim, in a number of places.  I won’t contribute to the “fair use” plagiarism and just note that I found my information at the Imperial College Robotics Society wiki and the website linked below.  Instead, I’ll summarize a couple ways I am using and tweaking my build.

OK, so what did you do?

pifm coupled with sox turns out to be a very useful combination for Pi-rate broadcasting.  I learned these tricks from Make Tech Easier.   From a terminal:

sudo apt-get install sox libsox-fmt-all
mkdir ~/pifm
cd pifm
wget http://omattos.com/pifm.tar.gz
tar zxvf pifm.tar.gz

Now, something like sudo ./pifm sound.wav 102.3  works just fine, so long as you are on an older model Pi (the one with the 26 GPIO pins). I’ve got several lying around (Ok, they are doing things, but they have space to do more).  Let me just say that since I name my raspberry Pi’s after droids, ships and planets from the Star Wars universe, I totally approve of the test wave file provided with pifm.

I had plenty of success attaching some female and male hookup wires on pin 7 (GPIO 4) to serve as an antenna.  Then I started to worry if I was going to run in to trouble with the law.  As far as I can tell, so long as I am broadcasting under 63 meters, am low power and not interfering with any licensed stations, I should be OK. To check the last part, I visited radio locator to look for vacant frequencies in my area.  87.5 FM is a good candidate for me.  I then checked it out to make sure the information was correct (nothing but static). I’m a bit concerned that the RPi might not be considered low power since it can be transmitting about 53 mW (assuming the current draw on the GPIO pin is a full 16 mA).

To check how far I was broadcasting, I grabbed my phone, tuned in to my station and started walking.  I live near the top of a 12-story apartment building and got great reception down the hallway and in the elevator until I hit the ground floor.  I had virtually no reception in the lobby, but once I got outside, I was back to a good, strong signal.  In fact, I could still get a clear signal 2 city blocks (approximately 2 tenths of a mile) away from my apartment.  Clearly, with the initial antenna length I was out of compliance with the FCC, so I dropped the antenna to about 70 cm and I appear to be within regulations.

Now, since I like to enjoy classical radio, in particular wcpe which streams on the internet, I decided to see how smoothly I could get it to work.

sox -t mp3 http://audio-mp3.ibiblio.org:8000/wcpe.mp3 -t wav -r 22050 -c 2 - | sudo ./pifm - 87.5 22050 stereo

Bam, up and running. The quality of the transmission is decent, but crank the volume and you can tell that the music is 22 kHz.  Now that reminds me that I haven’t donated to wcpe in a very long while, so excuse me while I go write a check.

Still not that impressed?

OK, how about this. I wanted to see if I could get my RPi to serve as a broadcaster, so I installed a text-to-speech engine.  Festival worked OK for this exercise (sudo apt-get install festival ) and then the following command works nicely:

sox "|echo 'hello bob, you are working hard today' | text2wave -otype wav -F 22050" -t wav -c 2 - | sudo ./pifm - 87.5 22050 stereo

Now I can create text files for my computer to speak and broadcast on “my” FM channel.  Using this link, I found some different voices that were slightly less robotic, but then again, I am asking a computer to broadcast, so what am I really expecting?

geek note 1: Looking closely, we shouldn’t need sox for this and simply pipe text2wave to pifm; however, the end of the message gets cut off if I do that, so I need to do a bit of debugging first to see what’s going on.

geek note 2: If you are familiar with the various tts engines, flite doesn’t work since it cannot pipe its output as of wheezy)

Where to go from here?

So now I’m thinking about other projects.  What if you were throwing a party in a space that made large speakers impractical?  Well you could stream your playlist to an unused channel and then place radios strategically around the venue.  As I mentioned at the beginning of this post, I’m a late-comer to this game, but in case my home town falls victim to a civil war, I’ll be ready to get the message out.  What I’m really thinking about, though, is setting up a solar-powered raspberry-pi for broadcasting while camping or out in the wild.  I think that would be fun (both the broadcasting and the being outdoors; I’ve been sitting behind a desk for too long this summer.)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.