Simple Radio Recording with a Pi

One of my first full blown Raspberry Pi projects was a radio frequency recording system. I needed to record an FM signal, within the VHF band. I wanted the recordings to be in the wave format, one hour files and eleven day loop with old recordings being automatically deleted. I knew I was going to use a Raspberry Pi to manage the recording and storage process, but I was unsure of what type of device I was going to use for the receiver.

First, I started messing around with cheaper Software Defined Radio (SDR) dongles, which can be purchased for a very reasonable price on Amazon or eBay. I opted to not use the devices as I was having issues with inter-modulation and the squelch opening when adjacent frequencies were being used. These devices are useful in many applications. In fact, I use one as a panadapter (a visual reference or graph of what the radio is receiving) on one of my radios. Perhaps sometime I will explain that more.

I opted to use a Motorola Maxtrac 300 mobile radio. I have always been a fan of commercial grade Motorola radios, as they are durable, have sensitive receivers, and awesome rejection for inter-modulation.

The Maxtrac is a commercial grade radio, and therefore, is not front or field programmable. Luckily the individual I was purchasing them from was able to program my needed frequencies into the radios.

The Maxtrac has two different ways to pull received audio. I opted to use the front microphone port for this project. The port is an RJ45 jack, exactly what you would find on a computer for its ethernet port. The pinout in the jack, from top to bottom is:

  1. Not Used
  2. Not Used
  3. Hook
  4. Ground
  5. Mic Audio
  6. PTT
  7. Serial Programming Line
  8. RX Audio Output

For this project I only needed to use pins four and eight. For an analog mono audio line to work, we need a ground line and a hot line. I simply took a CAT5 ethernet cable, cut it in half and wallah, we have the radio end done. I needed to get a 3.5 mm audio connector attached to the other end. This is the plug I used. It is a stereo plug and has three different contacts; the sleeve, the ring, and the tip. The sleeve is the ground line, the ring is the right channel and the tip is the left. As the the audio coming out of the radio is mono, I soldered the the audio hot line to both the ring and tip terminals contained inside the jacks plastic body.

The Raspberry Pi does not come with an audio-in or mic port. To overcome this small problem, I used a USB sound card dongle. These are small devices that can be found all over Amazon, most under $15.00. Now that the physical connections were done, it was time to focus on writing scripts and getting the processes in order on the Pi.

First item up on the docket was the script to record the audio. First we need to make sure that the Pi is seeing the sound card and that it is selected. We will run:

pi@pi:~ $ sudo alsamixer

That will bring up this menu:

If we need to change the sound card, we would simply select F6 which brings up a menu:

Where we would just select the USB Audio Device from the list.

We will use rec to record the audio. Simply typing rec into the terminal or rec –help will bring up the options with the specific program. After a little trial and error I came up with the combination of settings:

pi@pi:~ $rec -r 8k -c 1 test.wav trim 0

If we run this command, rec will record the audio, if the USB sound card was selected correctly in the alsamixer. It will record till we tell it to stop. Time to write a bash script to get this up and running automatically:

#!/bin/bash

sleep 1
date=`date +%F-%R`
basePath="/home/pi/radiorecording"
rec -r 8k -c 1 $basePath/output/CH-$date.wav trim 0 840:00

As you can see, we have a script now that will start the recording, and name the file according to the date and time that it started. So if the recording for the specific file was started at 10 PM on the 20th of December 2020, it would be called “CH-2020-12-20-22:00.wav”. The output file will go into a folder called “output”. Then of course we need a script to make this process stop. As we want it to restart every hour and create a new audio file. So we write another bash script:

#!/bin/bash

pkill rec

Hey that was simple. But now we need to get them set to run automatically. To do this task we will use the time based process scheduler known as Cron. First we need to make sure that Cron has the proper permission to execute the files. To do this we need to set the permissions on our scripts. I want all system users to be able to read, write and execute the scripts.

pi@pi:~ $ sudo chmod 777 /home/pi/radiorecording/start.sh

pi@pi:~ $ sudo chmod 777 /home/pi/radiorecording/reset.sh

Disclaimer, chmod 777 is a bad idea to set as a permission. The Pi only has one user, and is in a secure closed network. So giving all users read, write and execute permissions doesn’t concern me that much.

So to edit our Cron processes, we will execute:

pi@pi:~ $ sudo crontab -e

Now mind you, a person has to be careful when entering contrab files, if a person enters in “contrab -d” deletes the entire contrab for the user. As you can see I added in our start and reset scripts. The “0” in the first column signifies that it will execute on the hour. To learn more about cron you can visit here.

Now that we have the start and reset scripts running, it is time to tackle the automatic removal of old files. To accomplish this all I had to do was add this line to the crontab:

* 0 * * * find /home/pi/CH50/output -mtime +10 -exec rm {} \;

This runs at “every minute past hour 0” according to Cron. This is a simply helpful tool for figuring out Cron process scheduling, if you feel the need to learn more.

I now have three of these systems running at my house for recording different frequencies. I have yet to have an SD card failure, but I am sure it is coming. Micro SD cards, as what the Pis have, are known to have failures from constant write, re-write situations. But we are coming up on a year now and I have not had one fail yet. In the end it is worth spending the extra to get a decent quality Micro SD card.

Discover more from WirelessBilly

Subscribe now to keep reading and get access to the full archive.

Continue reading