Build an FM radio using an RTL-SDR dongle

Raspberry Pi FM Radio

For the Pi FM radio project in this article, I tried a few different arrangements, but I found that an LCD HAT (hardware attached on top) with buttons (Figure 4) worked best. However, other options such as a PiFace digital HAT (Figure 5) or a button HAT could also be used.

Figure 4: LCD FM radio control.
Figure 5: PiFace FM radio control.

The LCD Python libraries will vary according to the hardware used; however, most LCD HATs are based on the Adafruit libraries [4].

The Python code for the LCD HAT (Listing 2) is based on the simple test code with some additional logic for an LCD button interface and some predefined radio frequencies. The main code loops through, looking at each of the buttons with the lcd.is_pressed() function. The LCD.UP and LCD.DOWN buttons are used for volume control, and the LCD.LEFT and LCD.RIGHT buttons cycle through the predefined radio stations. The lcd.clear() and lcd.message() functions show the new radio station information on the LCD HAT. In my testing, I used a 0.25-second delay on all the button presses, although you might need to tune this value for your hardware and usage.

Listing 2

Pi FM Radio with an LCD HAT

# - FM Radio with an LCD Shield for controlling the stations and volume
import os, subprocess, signal
import time
import Adafruit_CharLCD as LCD
def newstation(direction):
    global stnum, stations, process
    print 'stnum=',stnum,'direction=',direction
    part1 = "rtl_fm -f "
    part2 = " -s 200000 -r 48000 | aplay -r 48000 -f S16_LE"
    if (stnum + direction < (len(stations)  )) and (stnum + direction > -1):
        stnum = stnum + direction
        print('Playing station :', stations[stnum])
        cmd = part1 + stations[stnum] + part2
        if process != 0:
            process = int(subprocess.check_output(["pidof","rtl_fm"] ))
            print "Process pid = ", process
        # start the new fm connection
        print cmd
        process = subprocess.Popen(cmd, shell=True)
def setvolume(voldif):
    global thevolume
    if (thevolume + voldif > 0) and (thevolume + voldif <100):
        thevolume = thevolume + voldif
        os.system('amixer sset "PCM" ' + str(thevolume) + '%')
        print 'volume = ' , thevolume
lcd = LCD.Adafruit_CharLCDPlate()
# Add your own stations and station info
stations = ['95.3e6','94.7e6','102.9e6','107.9e6']
sinfo = ['95.3 country', '94.7 light','102.9 easy','Y108 Rock\nHamilton ']
thevolume = 40  #initial volume
stnum = 1         #pick a starting station
process = 0
print 'Press Ctrl-C to quit.'
while True:  #look for button presses
    if lcd.is_pressed(LCD.UP):
    if lcd.is_pressed(LCD.DOWN):
    if lcd.is_pressed(LCD.LEFT):
    if lcd.is_pressed(LCD.RIGHT):


Creating a homemade FM radio is just one of the many interesting applications for SDR utilities and the low-cost RTL-SDR dongle [5]. In the future, I would like to add a Python Tkinter or web interface to my FM radio application.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Software-Defined Radio

    Armed with a US$ 20 hunk of hardware and a free software-defined radio tool, Konstantin starts the hunt for radio-transmitted data from a weather station.

  • FSFE and FFII to Radio Station Winners: "rOGG on"

    Radio Free Deutschland: For Document Freedom Day, March 31 2010, a couple of European radio stations were granted awards for using open standards.

  • Replace with VLC
  • Internet Radio Intro

    Did you think the Internet was out of revolutions? With Internet radio, anyone can be a broadcaster. If you want to start broadcasting or podcasting, or even if you're just looking for the perfect station, check out this month's Internet Radio cover story.

  • Ripping Audio Streams

    FM no longer has the biggest selection of radio stations. Instead, you need an Internet connection. But how can you save a copy of your favorite web broadcast?

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95