How to set up Raspberry Pi 3 to use RTL-SDR USB dongles and run Python scripts

In this post you will learn how to use one or more RTL-SDR dongles in your Raspberry Pi and set up to use a Python library to develop your own scripts. If you are newbie in using Raspberry Pi, we recommend you have a look at our Beginners guide to Raspberry Pi.

In order to do that, we’ll do 3 things:

  • Installing the libraries needed to use the Python 3 library, pyrtlsdrin the Raspberry Pi
  • Making sure your RTL-SDR USB dongles are working properly in your Raspberry 3
  • Making sure Python has permissions to access your dongles

Installing the libraries needed to use the Python 3 library in the Raspberry Pi 3

If you try to run, by pressing the F5 key, the demo example of the pyrtlsdr library, the demo_waterfall.py, you will get an error in the Python shell saying that importing matpolib failed. Then, in the Raspberry Pi command line, you need to download it and install it by typing:

git clone https://github.com/matplotlib/matplotlib
cd matplotlib
python3 setup.py build
sudo python3 setup.py install 

Then, if you run the demo_waterfall.py script again, you may get this error:

ImportError: Cairo backend requires that cairocffi or pycairo is installed

so now you need to upgrade the setup tools, install the libffi6 and the libffi-dev library and then install the cairocffi:

 
sudo pip3 install --upgrade setuptools
sudo pip3 install libffi-dev libffi6
sudo pip3 install cairocffi

You can also do “sudo apt-get install…” instead of “sudo pip3 install…”.

Making sure your RTL-SDR USB dongles are working properly in your Raspberry 3

The first thing to do, from the Pi command line is to install rtl-sdr:

sudo pip3 install rtl-sdr 

Now, you can check your RTL-SDR devices detected in the Pi. In our case, we have 2 dongles, so we do:

pi@raspberrypi:~ $ rtl_test
Found 2 device(s):
0: , 8��v , SN: � �v
1: , 8��v , SN: � �v

Using device 0: Generic RTL2832U OEM
usb_open error -3
Please fix the device permissions, e.g. by installing the udev rules file rtl-sdr.rules
Failed to open rtlsdr device #0

Here, we found how to solve this permissions issue. However, if you don’t find the file rtl-sdr.rules in the folder /etc/udev/rules.d/ then, keep reading the following step 🙂

Making sure Python has permissions to access your dongles

If you haven’t got any file called rtl-sdr.rules in the folder /etc/udev/rules.d/ run the following commands in the Pi cmd:

cmake ../ -DINSTALL_UDEV_RULES=ON
make
sudo make install
sudo ldconfig 

Now, instead of running the script from the Python shell (pressing the F5 key), in the Pi cmd, type

cd /route_to_your_pyrtlsdr_folder
sudo python3 demo_waterfall.py

You will probably get this error then:

ImportError: No module named pylab

which corresponds really to our previous section “Installing the libraries needed to use the Python 3 library, pyrtlsdrin the Raspberry Pi” but we’ll mention here, following the troubleshooting chronological order:

sudo apt-get install python3-numpy python3-scipy python3-matplotlib
 ** (demo_waterfall.py:1871): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files
 
 Kernel driver is active, or device is claimed by second instance of librtlsdr.
 In the first case, please either detach or blacklist the kernel module
 (dvb_usb_rtl28xxu), or enable automatic detaching at compile time.
 
 usb_claim_interface error -6
 Traceback (most recent call last):
   File “demo_waterfall.py”, line 102, in <module>
     sdr = RtlSdr(serial_number=’00000001′)
   File “/home/pi/Desktop/pyrtlsdr-master/rtlsdr/rtlsdr.py”, line 103, in __init__
     self.open(device_index, test_mode_enabled, serial_number)
   File “/home/pi/Desktop/pyrtlsdr-master/rtlsdr/rtlsdr.py”, line 125, in open
     % (result, device_index))
 OSError: Error code -6 when opening SDR (device index = 0)

So check what you’ve got in /etc/udev/rules.d/rtl-sdr.rules and compare with the output of:

 rtl_test 

which should looks similar to our output:

Found 2 device(s):
0: Realtek, RTL2838UHIDIR, SN: 00000001
1: Realtek, RTL2838UHIDIR, SN: 00000002

Using device 0: Generic RTL2832U OEM
Detached kernel driver
Found Fitipower FC0012 tuner
Supported gain values (5): -9.9 -4.0 7.1 17.9 19.2
Sampling at 2048000 S/s.

Then, check the list of USB devices connected to your Pi:

 lsusb

And you should get your list (note that we have the 2 RTL-SDR dongles, the mouse, the keyboard and the in-built ports of the Pi):

 Bus 001 Device 009: ID 0bda:2838 Realtek Semiconductor Corp. RTL2838 DVB-T
 Bus 001 Device 008: ID 0bda:2838 Realtek Semiconductor Corp. RTL2838 DVB-T
 Bus 001 Device 007: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
 Bus 001 Device 006: ID 046d:c52f Logitech, Inc. Unifying Receiver
 Bus 001 Device 005: ID 04f2:0116 Chicony Electronics Co., Ltd KU-2971/KU-0325 Keyboard
 Bus 001 Device 004: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
 Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
 Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
 Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

You should identify your RTL-SDR devices in the file rtl-sdr.rules. In ours, we have:

# RTL2832U OEM vid/pid, e.g. ezcap EzTV668 (E4000), Newsky TV28T (E4000/R820T) etc.
 SUBSYSTEMS==”usb”, ATTRS{idVendor}==”0bda”, ATTRS{idProduct}==”2838″, MODE:=”0666″

After verifying all of this is correct, let’s check the status of modules in the kernel:

 lsmod 

We get this ouput (yours can be slightly different):

Module                  Size  Used by
.
.
.
snd                    70032  8 snd_timer,snd_bcm2835,snd_pcm
bcm2835_gpiomem         3940  0
fc0012                  5560  1
ir_lirc_codec           5590  0
lirc_dev               10583  1 ir_lirc_codec
r820t                  18488  1
rtl2832                10828  2
i2c_mux                 5879  1 rtl2832
dvb_usb_rtl28xxu       20308  0
dvb_usb_v2             16424  1 dvb_usb_rtl28xxu
dvb_core               99970  2 dvb_usb_v2,rtl2832
.
.
.

In order to detach the module, as the error suggested, we can do:

 sudo rmmod dvb_usb_rtl28xxu rtl2832 

Now, if you do

 sudo rmmod dvb_usb_rtl28xxu rtl2832 

it should work perfectly now 😀

Conclusion

We hope you find this troubleshooting guide useful. All that we did was running the script and researching the proper command to solve the issues we found. You can find a demo in our next post. If any of these doesn’t work, please, write a comment below or just drop us an email 🙂

2 Comments

Add a Comment

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.

Show Buttons
Hide Buttons