Wednesday, December 19, 2012

Raspberry Pi as FM transmitter

Found this on the web and had to try it out:
http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter

By oscilating one of the GPIO output pins on the Raspberry Pi at high frequency the board becomes an FM transmitter. However the oscilation is based on frequency modulation with turning the pin ON and OFF, and therefore requires alot of CPU power, and limits the output sound resolution to 6 bit or so. So you can play the starwars melody fine. Speech is ok, but noisy and music is not enjoyable.



However there are talks about enabling DMA and improving the quality. But that's something for the future.

Here is how you get it up and running:
1. Download this file:
http://www.icrobotics.co.uk/wiki/images/c/c3/Pifm.tar.gz
It's a zipped folder, containing the files you need, including a soundclip of the starwars melody for you to test with.
2. Unzip the "Pifm" folder
3. Transfer the folder to your Raspberry Pi
4. SSH into the Raspberry Pi
5. Go to your Pifm folder
cd Pifm
6. Oh btw, you need to download gcc in order to compile Pifm
sudo apt-get install gcc
7. Now that's installed. Compile pifm.c
gcc -lm -std=c99 pifm.c
This will create a compiled program called "a.out"
8. Then run the file as described in the helpfile:
Usage: program wavfile.wav [freq] [sample rate]

Where wavfile is 16 bit 22.5kHz Mono. Set wavfile to '-' to use stdin.
freq is in Mhz (default 103.3)
sample rate of wav file in Hz

So for example:
sudo ./a.out sound.wav 100.1

For those new to linux:
sudo                means "do as superuser:"

./                  means "run this file:"

a.out               is the executable

sound.wav 100.1     these are the run-parameters, telling a.out to play sound.wav and output                     it at 100.1MHz FM

If succsessfull the command prompt will not display anything, and hang untill finished playing the wav-file. You can allways exit by pressing CTRL+C.
When finished playing, the program will return
exiting

By connecting a wire to the GPIO output pin (GPIO pin 4 by default) You will get very improved range. Just the pin will transmit only about 20cm, While a 20cm wire will give you about 10 to 50meter range.

Here is the pinout, as it's not really that obvious what pin it is. The bottom row, pin four from the left.
Here we are, finished and broadcasting:

My antenna is that red and white wire, leftovers from an old pc.

Oh, btw:
The wave file (.wav) is 16 bit 44.1kHz Mono format.

3 comments:

  1. I've been trying to get this working but all i get is a quite signal on the radio. When I try to play a file it start to transmit weird noise.

    When I compile the pifm.c using gcc -lm -std=c99 pifm.c command I get this output:

    pifm.c: In function playWav:
    pifm.c:192:9 warning: implicit declaration of function usleep [-Wimplicit-function-declaration]

    I don't think I should be getting that but I tried everything already. Deleting everything and downloading files again and nothing seems to work. Any help with be great. Thanks!

    ReplyDelete
    Replies
    1. The first time I tried pifm I also got noise. Found that I had several instances of the program running along with some audio files running. Did a reboot and then it worked. The message you got was a warning, I had it also but program worked could get reception on the other side of the house.

      Delete
  2. Yea, i get that warning too. Dont really know what it means, but i dont really care as the output works fine (the a.out executable).

    You have tried running the a.out, right? (sudo ./a.out sound.wav 100.1)

    You should also try holding your FM radio really close to the raspberry pi, just in case you have connected the antenna wire to the wrong pin. I did this the first time, as i thought the GPIO4 pin was the pin with the number 4, and not the pin with GPIO4 written on the side off it (as seen on the pinout picture)
    Even with no antenna you should be able to broadcast about 20cm.

    ReplyDelete