Multi-function Bat Recorder/Detector

(Last updated 4th September 2016)

A multi-function bat recorder/detector is a bit more fun to make than the simple bat recorder, but has its challenges.

This type of recorder is not meant to replace a professional bat recorder/detector. These can more than £1000, which makes them beyond the reach of someone wanting to learn a bit more about bats, echolocation, spectrograms, fast fourier transforms etc.  Using a Raspberry Pi together with a Cirrus Logic Audio Card, a relatively cheap alternative can be made for about one-tenth of the cost. The frequency range is limited to 96kHz (due to the maximum sample rate of 192kHz – see ‘Nyquist theorem‘), but this will allow you to detect most bats found in the UK to some degree.  Examples of recordings made using this type of detector can be found here.

The following steps will hopefully provide enough information to get you going.  Some knowledge of the Raspberry Pi and Python scripting is assumed, but don’t let this put you off trying to make this type of recorder/detector – the rewards are worth it.

If you need help, guidance, or more information, feel free to contact me via the contact page.

tinker_image

3D Printed Case version

The above image shows the type of hand-held recorder that I am currently using.  This has been developed over the last two and a half years (summer 2014 – spring 2016).  It uses a bespoke 3D printed case, and is battery powered with a touch screen display (shown below) providing the various functions.  Alternative Raspberry Pi cases can be used, but some modification will probably be necessary.

main_screen_new

Multi functions available using a touch screen and tkinter GUI

The Raspberry Pi can easily be powered from a Power Bank – the type used for re-charging mobile phones.


What You Will Need

Apart from soldering skills, and maybe a Dremel, here’s what you will need:

  • Raspberry Pi 2 Model B (or Pi 3 Model B)
  • Cirrus Logic Audio Card
  • USB TFT Touch Screen Display (Pimoroni)
  • Electret microphone (Panasonic WM-61A)
  • 3.5mm 4 pole jack (for mic in)
  • 0.25W 8 Ohm Speaker (slim version)
  • Real Time Clock (RTC) Module (so that log files can be time-stamped)
  • Various bits of wire (for the microphone and to connect to the RTC)
  • USB Cable (for the screen)
  • USB Cable (power supply direct to the Audio card – 5.5mm barrel type)
  • 5V 2A Power bank
  • 16GB (or larger) micro SD card
  • Suitable case (see below)

Optional things:

  • GPS module
  • 6 x AA battery holder
  • UBEC (for battery power option only)

The end result

Here are some images showing what can be achieved.  The top image shows the Raspberry Pi and Cirrus Logic Audio Card mounted into a 3D printed case.  The speaker is attached to one of the Audio Card speaker connections (soldering required). Top left is the Real Time Clock module. Also shown is the optional battery-powered feature which makes things a bit more complicated.  To start with, using power from a Power Bank and USB power cable plugged directly into the Cirrus Logic Audio Card is the best option.

3D_PiBat

Bat detector/recorder housed in a 3D printed case, with optional battery holder

The image below shows everything mounted into a “standard” Raspberry Pi case, that allows spacers to be used between the case and the lid.  This version shows the touch-screen display neatly mounted into the lid, which required some careful cutting.  Alternatively, the display can be mounted directly on top of the lid.

Bat recorder/detector housed in a Raspberry Pi case from ModMyPi

Bat recorder/detector housed in a Raspberry Pi case from ModMyPi

Inside the ModMyPi case. This version has the optional GPS module and no RTC module

Inside the ModMyPi case. This version has the optional GPS module and no RTC module


Installing the code for Wolfson/Cirrus Logic Audio Card

Installing the code for the Wolfson/Cirrus Logic Audio Card is one of the trickiest parts of making this project.

Fortunately, there is some good advice on the internet:

Outdated method:

https://github.com/CirrusLogic/rpi-linux/wiki/Building-the-code

Recommended method:

http://www.horus.com/~hias/cirrus-driver.html

Element 14 also provides an image that can be downloaded from here. It’s a large download, so allow time for it to complete.  Installing images on SD cards is explained on the Raspberry Pi Foundation web-site.

The user manual for the Cirrus Logic Audio Card is here:

https://www.element14.com/community/docs/DOC-72078?ICID=CirrusLogicAudio-topMain-usermanual

When you have the drivers installed, check that the card is recognised by the Pi.  In a terminal window, type the following (that’s a lower case “L”):

aplay -l

You should see something like the following.  It’s the “sndrpiwsp” bit that’s important – it means the audio card has been recognised.

**** List of PLAYBACK Hardware Devices ****
card 0: sndrpiwsp [snd_rpi_wsp], device 0: WM5102 AiFi wm5102-aif1-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 8/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 1: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0

Now is a good time to make sure that the bootloader is not replaced during any future RPi updates, otherwise the audio card will stop working.  At the terminal prompt, enter the following:

sudo apt-mark hold raspberrypi-bootloader

Setting up the RoboPeak/DFRobot 2.8″ USB TFT Driver

Follow the instructions here for setting up the drivers for the display:

https://github.com/pimoroni/rp_usbdisplay

http://forums.pimoroni.com/t/dfrobot-2-8-usb-tft-pi-2-raspbian-setup-guide-advanced/594


Setting up the Real Time Clock module (RTC)

I have tried various RTC modules together with the Cirrus Logic Audio Card.  For some reason, I could only get one type to work.  It is generally called “SODIAL(R) Tiny DS3231 AT24C32 I2C Module Precision Real Time Clock Module For Arduino” and can be bought here.

Connect the RTC to the 2×10 Expansion Header (J2) on the Cirrus Logic Audio Card:

Fritzing diagram showing RTC connections to Cirrus Logic Audio Card

Fritzing diagram showing RTC connections to Cirrus Logic Audio Card

At at terminal prompt, enter:

sudo nano /etc/modules

Add “rtc-ds1307” at the end. Ctrl-O and Ctrl-X to save and exit.

/etc/modules

/etc/modules


sudo nano /boot/config.txt

At the end of the file add


dtoverlay=i2c-rtc,ds1307

Check that the RTC module is loaded (UU at position 68):

sudo i2cdetect -y 1

RTC_loaded

Next edit /lib/udev/hwclock-set


sudo nano /lib/udev/hwclock-set

Comment out the following lines with # like this:


#if [ -e /run/systemd/system ] ; then
#exit 0
#fi

Reboot the Raspberry Pi.


sudo reboot

Set the time by following the instructions here.


 Install some Python libraries

These Python libraries will need to be installed.  Enter each at the terminal window prompt (except for the highlighted lines).  Don’t forget to expand filesystem before installing.

sudo apt-get update
sudo apt-get install python-qt4 pyqt4-dev-tools
sudo apt-get install python-qwt5-qt4
sudo apt-get install libsndfile1-dev libasound2-dev python-dev
Ignore warnings during the next step.
sudo pip install scikits.audiolab
sudo apt-get install python-numpy python-matplotlib python-scipy
sudo apt-get install python-imaging-tk
sudo apt-get install git
sudo git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo apt-get install python-dev
cd pyaudio
sudo python setup.py install
sudo apt-get install build-essential
The next step takes some time to complete:
sudo pip install numpy --upgrade
sudo apt-get install sox libsox-fmt-mp3
sudo apt-get install python-qwt5-qt4
sudo apt-get install python-psutil

Make a microphone

Details about how to make the microphone can be found in the instructions for the Simple Bat Recorder. Take care when soldering the wires onto the electret microphone.  Use shielded cable for best results (connect the shield to ground at one end of the cable).

Using a right-angled 3.5mm 4 pole jack plug saves space.  These things are not easy to solder wires on to :-).


Make a Spectrogram

There are several ways to make a spectrogram. Using SoX is probably the easiest.
Before doing anything though, read the Cirrus Logic Audio Card user manual. Make sure you are happy everything is working OK.
In a terminal window, reset the paths, then set to record from the microphone.

./Reset_paths.sh
./Record_from_Headset.sh

If you want to do this from a Python script, use this method.

import subprocess
subprocess.call ("/home/pi/Reset_paths.sh", shell=True)
subprocess.call ("/home/pi/Record_from_Headset.sh", shell=True)

Record 4 seconds of audio.


arecord -Dhw:sndrpiwsp -r 192000 -c 2 -f S16_LE -d4 /home/pi/test.wav

Use SoX to create a Spectrogram. Note that the number of channels are remixed to one (mono).


sox /home/pi/test.wav -n remix 1 rate 192k spectrogram -o /home/pi/spec.png

Experiment with some of the SoX functions. For example, use -h to create a high colour spectrogram.


sox /home/pi/test.wav -n remix 1 rate 192k spectrogram -h -o /home/pi/spec.png

The arecord and sox commands can be used in a loop to continuously display a new spectrogram every 4 seconds.


Sound Activated Recording/Spectrogram Display

From my work with sound activated recording, I decided to incorporate this feature into the multi-function bat recorder (June 2016).  There are several advantages to this method over just displaying a new spectrogram every 4 seconds:

  • the bat detector waits until a bat call triggers a recording
  • the recorded .wav file is saved for later analysis if required
  • with a Pi2 or Pi3 it is possible to quickly analyse the .wav file to determine peak frequency of the call (which is also useful to filter out sounds with a frequency below 17khz for example which may not be bats)
  • a spectrogram is created of the bat call – so only bat call spectrograms are displayed, rather than just an updated spectrogram every 4 seconds.
  • the peak frequency can also be displayed

A typical result of a Serotine call is shown below.

SAR_Sono


Time Expansion Playback

It is possible to slow down the audio being picked up by the microphone.  If it is slowed down by a factor of 10, a bat call at 45kHz will become 4.5kHz which is audible to humans.

This function took me ages to get working.  The recorded sound is piped to SoX for processing and then piped back to alsa for playback.  There is probably a much neater solution.

For the Cirrus Logic Audio Card to play back to a speaker, it needs to be powered directly through the aux power input on the card.  Fortunately, this also powers the Raspberry Pi. Use a 5.5mm/2.1mm 5V DC Power USB Cable.

5.5mm Barrel

5.5mm 5V DC USB Power Cable

Connect a slim 0.25W 8 Ohm Speaker to one of the outputs on the audio card.

speaker

Slim Speaker

Use this command to playback sound picked up by the microphone out to the speaker.


arecord -Dhw:sndrpiwsp -r 192000 -c 2 -f S16_LE | sox -t wav -e signed-integer -b16 -r 192000 -c 2 /dev/stdin -t wav -e signed-integer -b16 -r 192000 -c 2 /dev/stdout speed 0.1 | aplay -D hw:sndrpiwsp -


Real Time Peak Frequency Display

Displaying real-time frequency of bat calls is useful to determine not only the presence of bats, but to give an idea about the peak frequency of their calls. This can aid in species identification.

Fortunately, Scott W Harden, a biomedical student in Florida, has done the hard work in displaying real time FFT audio visualisation using Python.

Some minor modifications are required to Scot’s code to extend the visualisation into the bat audio spectrum.

RT_Graph

Real-time Frequency Display

A short video showing real-time frequency display of actual bat calls is provided below. Audio is being recorded from a separate heterodyne bat detector whilst videoing the display on the Pi Bat Detector (both devices are picking up the same calls).


Make a case

Finding a ready-made case for this project is almost impossible.  This case from ModMyPi, with two spacer plates, can be modified to house the Raspberry Pi, Cirrus Logic Audio Card, Speaker, USB Display and RTC module.

The USB display can either be mounted on top of the lid, or recessed into the lid.  Cutting out a rectangular hole for the display is tricky, but can be done by chain-drilling small holes and then filing the edges smooth.

I also designed a case for this project that can be 3D printed.  I’m still tweaking it, but if you would like details, please contact me.

3D_case2

3D Case Designed using TinkerCAD


 Power

To enable the Raspberry Pi Bat Recorder/Detector to be used outside at night, a suitable power supply is needed.  A Power Bank is ideal.  The current consumption using a Raspberry Pi 2 Model B, Cirrus Logic Audio Card, RoboPeak USB TFT Display and an RTC Module is between about 400mA to 450mA.  A 16,000mAh Power Bank will therefore power this device for around 30 hours.  For 4 or 5 hours use, 2,500mAh should be sufficient.

power_bank

16,000mAh power bank from ModMyPi


Future updates

GPS

I will be adding details on how to integrate a Ublox NEO-6M GPS module into the design.  This can allow location data to be shown and/or logged when recordings are made.

Sound Activated Recording

I have already had some success in setting up sound activated recording using SoX, so that bat calls trigger the start of recording, and recording automatically stops when a period of silence following the call is detected.

Visit my Sound Activated Recording page for more details.