This blog post is a combination tutorial and record of progress written as I built a new LDR around a Raspberry Pi 2 mini computer. While the initial effort was ultimately successful, the power requirement of the Raspberry Pi 2 is so great that a battery powered recorder was just not feasible (not for the durations I'm striving to achieve).
Fortunately the good folks at Raspberry released the Raspberry Pi Zero just as I was completing my build. And the zero has much lower power requirements then its earlier RasPi brethren. Plus the fact that it should sell for just $5, once initial demand is met, the RasPi Zero is a great option for building LDRs that can last for weeks in the field.
The following describes the steps I worked through and resources I leveraged to build a RasPi LDR. A significant portion of the content below was developed for the RasPi 2 build, and I will reserve it for the record. But in the end, I abandoned the RasPi 2 and built a very solid LDR on the RasPi zero. The notes below reflect that process. And RasPi 2 unique notes are moved to their own section at the bottom of this post.
Setting up the Raspberry Pi Zero as follows:
1. Connect a USB hub to the USB port of the RasPi Zero. I used the Sabrent model HB-UMLS.
2. Connect a wireless Logitech (model K400) keyboard/touchpad via the USB hub.
3. Plug an Edimax wireless USB adapter into the USB hub.
4. Connect a VGA monitor to the HDMI port via a HDMI to VGA converter (Ebay, product labelled Moread). Interface the HDMI converter to a mini HDMI converter (ordered from Adafruit as part of the Raspberry Pi Zero Basic Package) and plug the mini HDMI into the RasPi Zero.
5. Power the RasPi Zero via a 5v 1 amp power supply, also ordered from Adafruit. (For the RasPi2 you can provide power through the USB hub with its switch-controlled ports, thus creating an on/off switch for the RasPi2.)
6. Download Raspbian Jessie Lite from https://www.raspberrypi.org/downloads/raspbian/
7. Burn Raspbian Jessie Lite to an 8GB (or larger) memory card using the instructions found at:
use http://elinux.org/RPi_Easy_SD_Card_Setup#Using_the_Linux_command_line
(Alternatively, and probably easier on the RasPi 2, install the Raspbian OS from an 8GB memory card preloaded with NOOBS. Can be bought on Ebay or from a RasPi vendor.)
8. After burning the OS to the MMC card, insert it into the Raspi Zero card reader and connect the power to boot the computer. The boot should scroll across the screen and eventually stop at a login prompt.
9. Login with the default userid "pi" and default password "raspberry"
10. Run the command "sudo raspi-config" from the command line. In the raspi-config tool work through the menu options to:
10a. Expand the filesystem
10b. Change the default pi user password
10c. If you did not install Jessie Lite, choose boot option to boot into console, no desktop and no autologin, just a login prompt leading to a command line. This is the default in Raspbian Jess Lite.
10d. Set internationalisation options
10d1. Set up language and regional settings. Select en_US.UTF-8 UTF-8 from the list of options presented. Deselect the default UK setting.
10d2. Change the timezone setting.
10d3. Change keyboard layout. Select>Logitech Generic>English US
11. Select Advanced Options
11a. SSH - enable SSH to allow secure connections from another computer.
11b. I2C - enable I2C to support installation of hardware clock (only needed if you plan to install a hardware clock. This RasPi Zero based LDR does note need it).
12. Configure the wireless network from the commandline using the guidance from this page: https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md
13. Set the date with the command: sudo date -s YYYYMMDD
14. Check the time.is web page to see the precise time, then issue the following command with the correct time pre-configured to sync with the time.is web page: sudo date -s HH:MM:SS
15. Issue "sudo apt-get update" to update the system.
16. Ensure you're in your home directory by issuing the command: cd
17. Create three directories under your home directory with thes commands (note directory names are case sensitive):
mkdir Scripts
mkdir Logs
mkdir Audio
18. CD into the Scripts directory and create a shell script named MyBoot.sh with the command "nano MyBoot.sh"
19. Enter the following commands into the shell script to kill the triggerhappy process and the dbus process at bootup:
# Stop the triggerhappy service
sudo service triggerhappy stop
# Stop the dbus service.
sudo service dbus stop
20. Exit nano and save the file, then make the shell script executable with chmod u+x MyBoot.sh
21. Issue the command "sudo nano /etc/rc.local" and add this line to run the script at boot time:
sh /home/pi/Scripts/MyBoot.sh
22. Plug the USB mic adapter (aka external sound card) into the USB hub.
23. Plug mono microphone into USB mic adapter
24. Configure sound to take input from the USB soundcard, and not from the processor chipset's default built in sound card. Use nano to create the file "asoundrc" in the Scripts directory and ensure it contains the following:
pcm.!default {
type hw
card 1
}
ctl.!default {
type hw
card 0
}
25. Edit Scripts/MyBoot.sh and add these lines near the top, to ensure the correct .asoundrc file gets loaded at bootup:
#Put the correct .asoundrc file in the home directory.
cp /home/pi/Scripts/asoundrc /home/pi/.asoundrc
26. Install sox audio software by issuing the command:
sudo apt-get install sox libsox-fmt-mp3
27. Issue the command "sudo reboot" to run boot scripts and recognize the sound card.
28. Start alsamixer to configure and check recording levels.
28a. Hit Fn F6 (Function Key F6) to select the "USB Audio Device"
28b. Hit Fn F3 to show playback controls
28b1. Set the speaker down to 0 and hit M to mute (shows MM).
28b2. Set the mic down to 0 and hit M to mute (yes, this is counter intuitive).
28b3. If necessary hit M to turn on Auto Gain Control (00 is "on" and MM is "off".
28b4. Hit Fn F4 and set Capture up to 100.
28b5. Hit Esc to exit and save.
29. Issue the command "sudo alsactl store" to store these ALSA settings for use after reboot
30. With the mono mic plugged in, issue this command:
arecord -d 10 -f S16_LE -Dplug:default test1.wav
This should capture a 10 second long, 8000 bit resolution audio clip, and store it in the current directory.
31. Use nano to create a shell script under the Scripts directory named "Record1Hours.sh", and ensure it contains the following code:
#!/bin/bash
FILENAME=$(date +"%Y%m%d-%a%H%M")
rec -c 1 -r 44800 -b 16 /home/pi/Audio/${FILENAME}.mp3 bandpass 720 600 highpass 120 gain 20 trim 0 3598
When invoked, Record1Hour.sh will record just under one hour of audio to a file named with the date and time that the recording began. Note the file is two seconds short of an hour, to give time for sox to release the sound card and make it available to the next recording session.
32. To help reduce power consumption, put the following command into /etc/rc.local, before the line "exit 0" at the end.
echo powersave > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
33. Set up crontab to run the Record1Hour.sh script on an hourly schedule. Issue the command:
crontab -e
Add the following lines to the crontab:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SHELL=/bin/bash
58 22 * * * sudo reboot
00 21 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 22 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 23 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 00 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 01 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 02 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 03 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 04 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
00 05 * * * /bin/bash /home/pi/Scripts/Record1Hour.sh
Be sure to end each line with a carriage return. The cron daemon expects this. The "sudo reboot" may no longer be necessary, as it was used to turn the USB bus back on in the RasPi 2 version of the LDR. It's left in here because it might serve a useful purpose and improve system stability over time.
34. At this point the LDR is close to being deployable. But some power tweaks need to take place. This first will disable the HDMI port to save power. That means an alternative method must be used to log in and work within the system. That alternative is a Secure Shell (SSH) session from another computer, connecting over the wireless network. From another computer on the same wireless network issue the command:
ssh -l pi 10.0.0.18
Replace this example IP address with the actual IP address your RasPi Zero has been assigned.
35. After logging back into the RasPi Zero via SSH, turn off the HDMI circuitry by adding the following command to the file /etc/rc.local, somewhere before the line "exit 0":
#Shut down the hdmi circuitry
/usr/bin/tvservice -o
36. To save still more power, enter these lines in MyBoot.sh to turn off the LED after the system finishes bootup
# Set the Pi Zero ACT LED trigger to 'none'.
echo none | sudo tee /sys/class/leds/led0/trigger
# Turn off the Pi Zero ACT LED.
echo 1 | sudo tee /sys/class/leds/led0/brightness
37. The configuration work is basically complete, so it's now time to backup the memory card and save its image. This will be useful to recover from if you LDR's memory card should become corrupted. Or the image may be used to quickly set up new LDRs on additional RasPi Zero systems. Assuming you have another Linux desktop to create the images on, begin the process by shutting down the RasPi Zero with this command:
sudo shutdown -h now
38. Remove the MMC card from the RasPi and transfer it to a Linux desktop. After inserting the MMC card be sure to "unmount" it before writing to it with the dd command. The imaging process itself is beyond the scope of this page, but useful links outlining the process are listed below. The basic command used to create the image on my system was:
sudo dd if=/dev/mmcblk0 of=RasPiZeroLDR.img bs=4M
Useful links include:
http://raspberrypi.stackexchange.com/questions/311/how-do-i-backup-my-raspberry-pi
http://lifehacker.com/how-to-clone-your-raspberry-pi-sd-card-for-super-easy-r-1261113524
39. After inserting (and then unmounting) a new MMC card of equal or larger size to the original (8GB or better), write the LDR image to the new card with the dd command:
sudo dd if=RasPiZeroLDR.img of=/dev/mmcblk0 bs=4M
If you write the LDR image to a card that is larger than the original LDR card, be sure to run "raspi-config" and select the menu option to "Expand the filesystem". This will ensure you have the entire card's free space available to you, and not just the space of the original 8GB image.
40. With the LDR MMC card returned to RasPi Zero it's time to do some testing. Take the time you need to ensure everything is operating as expected.
40a. Boot the RasPi Zero and watch the green LED. It should flash during bootup, but after boot completes it should turn off. The LED will also flash again when you issue the shutdown or reboot commands.
40b. Leave the system powered on overnight. In the morning use SSH to log in to the RasPi Zero. The "Audio" folder under the home directory should contain 9 new audio files, each labelled with the date and time that they were created by the sox recording program, run by cron.
40c. Shutdown the RasPi Zero and then connect it to a battery power supply (minimum 5 volts and 1 amp). Allow the RasPi Zero to run and record over night, and then validate the audio files have been captured the next morning.
40d. Once the system's performance has been confirmed, enclose it securely in a weather resistant housing (PVC pipe is a good option). Connect a fully charge battery power supply to the system last and secure everything within the housing. Then deploy it to the field for a test run.
40e. Configued as described above, the RasPi Zero should consume as little as 2080 milli amp hours of power during each 24 hour peried. A 16000 mAh pwer bank should keep the LDR running for 6 or more days.
Beware that power loss to the RasPi (as in the battery losing all power) has the potential to cause corruption to the MMC card, with data loss occuring as a result. The best option for avoiding this situation is to ensure your power supply exceeds the amount of power you'll need for the duration of the LDR's deployment.
Miscellaneous notes and references to be cleaned up at a future date:
$Doing some power consumption tests:
+headless, no keyboard, no wireless, with soundcard and mic plugged in - 5.02v at 130 mA
http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work
#--sudo nano /etc/crontab and add
#--- 00 16 * * * pi /home/pi/Scripts/Record1Hour.sh #to run this script daily at 4p.m.
At this point you have a functioning LDR capable of recording audio unattended for one hour at a time, each time you start the recording from crontab.
-Links for power reduction...
--https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=93463
--includes link to github tool that can power off individual USB ports.
-Links to building a Raspberry Pi audio recorder...
--http://sonof8bits.com/automated-raspberry-pi-audio-recorder/2014/09
#To secure copy all files from remote system's USB drive folder...
#scp pi@10.0.0.19:/mnt/USB/* .
#Use for naming files: date +%Y%m%d-%a%H%M%S
#rtcwake no hardware support in Raspberry Pi for rtcwake command.
#Power draw measurements
Headless and no peripherals plugged in: 220 mA
Headless and sound card plugged in: 250 mA
Headless and sound card and mic plugged in: 250 mA
Headless, sound card, mic and USB drive: 310 mA
Headless, sound card, mic, USB drive & keyboard: 330 mA
Headless, sound card, mic, USB drive, keyboard and wifi: 360 mA
Monitor, sound card, mic, USB drive, keyboard, wifi: 540 mA
#--Create a mount point for removable USB thumb drive
#---sudo mkdir /mnt/USB
#---sudo chown pi /mnt/USB
#---sudo chgrp pi /mnt/USB
#To mount a writeable USB drive use:
---sudo mount -o uid=pi,gid=pi /dev/sda1 /mnt/USB
#To mount it at boot time, put that into a bootable script, like MyBoot.sh
using guidance from this link:
+--http://sonof8bits.com/automated-raspberry-pi-audio-recorder/2014/09
+---kill the built-in sound card: $ sudo nano /etc/modules
+---And comment out: snd-bcm2835 (e.g. # snd-bcm2835) if it is found there.
+---Not necessary in Raspbian-Jessie-Lite
#Not necessary in Raspbian-Jessie-Lite
#+---Force ALSA to pick USB Mic over the built-in one, even if #we’ve just killed it.
#+---$ sudo nano /etc/modprobe.d/alsa-base.conf
#+---And comment out: options snd-usb-audio index=-2 (e.g. # #options snd-usb-audio index=-2)
#--install sox with mp3 support using "sudo apt-get install sox libsox-fmt-mp3"
#--record 10 seconds of mono at 44100 and gain of +20 with:
$#---"rec -c 1 -r 44100 -b 16 outfile.mp3 gain 20 trim 0 10"
#--to filter low end frequencies from a clip use:
#---sox foo.wav bar.wav highpass 315.0
#--to record while filtering out high and low frequencies use:
#---rec recording.mp3 highpass 315.0 lowpass 1250.0 trim 0 10
#--better yet a bandpass filter centered at 720 Hz:
#---rec recording.mp3 bandpass 720 310 trim 0 10
#--best of both worlds, combine and use:
#---rec recording.mp3 bandpass 720 310 highpass 150.0 trim 0 10
The following notes are only relevant if you want to install a hardware clock on a RasPi 2, that comes with the 40 pin GPIO. The RasPi Zero does not include the GPIO preinstalled, but the breakout pins can be purchased and soldered into place if so desired. See the Adafruit website for a video tutorial on how to accomplish this.
+--To enable i2c prior to installing a real time clock see:
#--https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
+--Run the commands:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
-To get the real time clock working I used...--https://learn.adafruit.com/downloads/pdf/adding-a-real-time-clock-to-raspberry-pi.pdf
+--To set up i2c support for the real time clock
+--sudo nano /etc/modules
and add these two lines to the end of the file:
i2c-bcm2708
i2c-dev
+-- Then do:
+--sudo nano /boot/config.txt
and add these two lines to the end of the file:
dtparam=i2c1=on
dtparam=i2c_arm=on
+--To install the hardware clock module
+--Issue command sudo shutdown -h now
+--With the power removed from your RasPi, plug the DS1307 Real Time Clock module onto the inside corner of the GPIO pins, with the RTC board pointing in toward the interior of the RasPi board.
--http://thepihut.com/blogs/raspberry-pi-tutorials/17209332-adding-a-real-time-clock-to-your-raspberry-pi
+--Repower the RasPi and log back in. Test that i2c found your rtc module with this command:
sudo i2cdetect -y 1
+--This will produce a table, and position 68 should show the number "68", indicating something was detected at that location (the location of your rtc module).
http://raspberrypi.stackexchange.com/questions/38186/how-to-install-real-time-clock-rtc-on-raspian #This one had the best info...
+--Next, set the time in the new hardware clock module. First, issue the following command to load the RTC module:
+--sudo modprobe rtc-ds1307
+--Then issue the command:
+--sudo bash
+--This puts you into a root user shell. Be very careful with the commands you issue as the root user. You could accidentally corrupt your machine.
+--Issue this command:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
+--Then issue this command to get out of the root shell:
exit
+Next, to test that your hardware clock was loaded, issue the command:
sudo hwclock -r
+--You should see a date and time that does not match the system time.
+--To sync the system time to the rtc issue this command:
sudo hwclock -w
+--To check that the hwclock has been updated to the current time issue:
sudo hwclock -r
+--And then issue:
date
+The output of both commands should only be a few seconds apart
+--Next, to ensure your rtc module will reload at every system boot, edit /etc/modules and add rtc-ds1307 to the bottom of the file. Issue the command:
sudo nano /etc/modules
+--Type in rtc-ds1307 below any other modules that already appear in the file.
+Finally, ensure the rtc is recognized during each system boot by adding the following to /etc/rc.local. Issue command:
+--sudo nano /etc/rc.local
+--Add this command into the file, but before "exit 0" at the end of the file:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
+--https://learn.adafruit.com/downloads/pdf/adding-a-real-time-clock-to-raspberry-pi.pdf
+--use time.is web page and sudo date -s to set precise time, then hwclock -s to set rtc
Resources for reducing power consumption on the RasPi
Turn off the HDMI circuitry with this page:
http://www.midwesternmac.com/blogs/jeff-geerling/raspberry-pi-zero-conserve-energy