Build an FM radio using an RTL-SDR dongle
Pi FM Radio
Low-cost RTL-SDR dongles can read frequencies between 24 and 1,766MHz. We built a simple FM radio with a Raspberry Pi, a USB dongle based on the RTL2832U chipset, an LCD HAT, and some Python code.
FM radio projects that use chipsets with a low-level Inter-Integrated Circuit (I2C) interface, like the RDA5807M and TEA5767, can work, but I found that both solutions have some drawbacks. The RDA5807M chip is poorly documented, with only Arduino C libraries, and the TEA5767 chip has no volume control.
Software-defined radio (SDR) offers a higher level interface that allows access to mixers, filters, amplifiers, modulators/demodulators, and detectors on the hardware. A wide range of hardware supports SDR, and the RTL-SDR [1] USB dongles based on the RTL2832U chipsets are well-priced at $10-$15.
SDRs have a large list of supported applications; some of the cooler projects include tracking airplanes, free-to-air TV, and monitoring satellite data.
Getting Started
To install the basic software, enter:
sudo apt-get install rtl-sdr
For my tests, I used a basic Raspberry Pi 1 Model B, but I also tested it on the Rasp Pi versions 2 and 3 and on an old, low-end PC running Lubuntu.
An important difference between the RTL-SDR dongle and an FM tuner module is that the FM tuner module needs the speakers to be directly connected to the tuner module. When you are using the RTL-SDR dongle, the audio is generated on the Raspberry Pi (or PC), so you will use the sound output of your computer. For my Raspberry PI setup, I used powered speakers (Figure 1), and for my laptop testing I used the internal speakers in the laptop.
The RTL-SDR dongle includes an externally connected antenna; if possible, you should try to place this antenna close to a window.
The rtl_fm
command-line utility is an FM demodulator that is used to read and sample a selected frequency. If you are looking to do more serious applications, see the GNU Radio Project [2].
A number of options can be passed to rtl_fm
; the key ones are the frequency (-f
), the sample rate (-s
), and the output rate (-r
). The output of rtl_fm
needs to be directed to an audio player program. I used aplay
, which is a command-line Advanced Linux Sound Architecture (ALSA) player, but other players could be used. I matched the sample rate (-r
) for aplay
with the rtl_fm
output sampling rate and used the 16-bit little-endian (S16_LE
) aplay
sample format (-f
).
The syntax with the options required to play an FM radio station at 107.9MHz is as follows:
rtl_fm -f 107.9e6 -s 200000 -r 48000 | aplay -r 48000 -f S16_LE
The rtl_fm
application needs to be stopped when you want to play a new radio station. If rtl_fm
is running in the background, the ps
command can be used to find process IDs. The kill
command can then be used to terminate the task. An example of finding and terminating the rtl_fm
task is:
pi@raspberrypi:~ $ ps -e | grep rtl_fm 1709 pts/0 00:00:33 rtl_fm pi@raspberrypi:~ $ kill 1709 pi@raspberrypi:~ $ ps -e | grep rtl_fm pi@raspberrypi:~ $
Adjusting the Volume
For Raspberry Pi applications, you can force the audio connections to use either the HDMI port or the phone jack on the Pi in raspi-config
by selecting the Advanced menu option and then Audio (Figure 2).
You have a few ways to adjust the audio volume. One method is to use the amixer
utility. To change the audio output volume on the Pi (or laptop) speakers, the Pulse Code Modulation (PCM) device is addressed. An example using amixer
to set the volume to 70 percent would be:
amixer sset "PCM" 70%
Python Test Program
For my basic testing, I created a simple Python command-line application that I could run on both my Raspberry Pi and on my old laptop running Lubuntu (Figure 3).
A few Python libraries were used, including the subprocess
library to launch rtl_fm
and return a process ID, the os
library to kill a process, and the time
library to add a delay (sleep), so the rtl_fm
task was given enough time to shut down cleanly before restarting.
A newstation()
function was created to stop a running rtl_fm
FM station and then restart it with a new FM station frequency, and a setvolume()
function was created to pass new volume settings to amixer
.
To run the test program (Listing 1) [3], I entered the command,
$ python FM_radio.py Simple FM radio test program
which then accepts a volume level as a percentage (e.g., 50%) or a radio station frequency (e.g., 107.9).
Listing 1
Command-Line FM Radio Utility
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.