How to build a Raspberry Pi dash cam.txt - Notepad

How to build a Raspberry Pi dash cam

I used the below steps to create my own Raspberry Pi dash cam that I use in my car to record my drives. The camera is mounted on my windscreen and is powered by a USB charger from the car's cigarette lighter.

I installed the headless version of Raspbian, Raspbian Lite, as it has a smaller resource footprint than the GUI version and I wouldn't be using the GUI anyway.

Installing and enabling the camera

Install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi. The cable slots into the connector situated between the Ethernet and HDMI ports, with the silver connectors facing the HDMI port. You can also detach/attach the cable to the camera end by sliding the clips below the camera down and inserting the cable. The silver connectors face towards the front of the camera lens. Avoid bending the cable as they can easily break. They cost around $5 to replace on ebay.

To enable the camera once it has been connected, type:
sudo raspi-config
Select Interfacing Options
Select Camera and enable it.
Update your system with sudo apt-get update && sudo apt-get upgrade -y

Recording video

I configured my dash cam to record video in 15 minute sections. The initial recording would kick off as soon as I booted my Pi. I did this using the below scripts and Cron.

Create a shell script named
now=$(date +%d%m%Y-%H%M%S).h264
raspivid -o /mnt/video/$now -t 890000 -w 1920 -h 1080 -fps 30

When the script runs it records at 1920x1080 with 30 FPS. You may want to set this to 720 for smoother playback and smaller files size (-w 1280 -h 720 -fps 60). The -t value of 890000 stands for 890 seconds, slightly less than 15 minutes, it sets the duration of the video's recording.

To automate video recording edit your crontab file, crontab -e, and add the below lines:
@reboot bash /home/pi/
*/15 * * * * bash /home/pi/ >> /mnt/usb/video.log 2>&1
* */1 * * * sudo find /mnt/usb/mov/* -mmin +360 -exec rm -f {} \; >> /mnt/usb/video.log 2>&1

The first line kicks off the recording script when the Pi boots up. The second line initiates the recording at 15 minutes intervals. The third line runs every hour to cleanup old recordings that are older than 360 minutes (6 hours).

I would recommended mounting a USB drive on your Pi to save recorded video there, rather than writing to the SD card that holds your OS. You can see a bunch of info including how to mount USB drives here.

Creating a power switch

To avoid having an unclean shutdown of my Pi each time I switched off my car, I installed a power switch so that I could send a shutdown command to the OS before I turned my car off. I purchased a PC power switch and connected it to GPIO pin 18 which is next to a ground pin. To configure an action on pin 18, create this python script. You can test the script by typing sudo python, then push your power switch, the Pi should now switch off. To get the python script to run at startup edit /etc/rc.local. Add the following line before the #fi at the end of the file:
sudo python /home/pi/Scripts/ &

Other tips

Backup your recorded videos to a NAS or other storage device using rsync:
sudo rsync -avzh /mnt/usb/recorded_pi_mov /mnt/nas/backedup_videos

Backup and restore your OS SD card using Win32 Disk Imager

Show the temperature of your Pi in degrees celsius:
/opt/vc/bin/vcgencmd measure_temp