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, pyrtlsdr, in 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, pyrtlsdr, in the Raspberry Pi” but we’ll mention here, following the troubleshooting chronological order:
sudo apt-get install python3-numpy python3-scipy python3-matplotlib
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):
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):
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 🙂
I cut and pasted each step one-by-one to my raspbian terminal and each step there were exceptions. It is a pity that over time things have slipped. cairocffi was a not found. I have been in I.T. for over forty years so I am no young whipper-snapper. It would be nice to find one of these pages that actually worked. Yes, I used a freshly imaged sdhc card.
Hi Hugh! Thanks for your comment. Do you mean you are getting the error “cairocffi was not found”? You then need to install it with “pip3 install cairocffi”. If you want, you can send us an email with the errors/exceptions you get and we can guide you further.