PiDashCam part two – Hardware, Software Overview and Pseudo Code

Part 2 of the series on my development of a Raspberry Pi based dashcam. See this post for part 1
Photo of PiDashCam breadboard

Although I’ve only just posted part 1 I’ve actually made quite a lot of progress towards achieving the objectives (as outlined in part 1.) In this post, I’ll look at the hardware I’ve chosen and show the Software Overview and Pseudo Code for pidashcam daemon

Breadboard Hardware

The picture at the top shows the basic hardware sitting on a breadboard for development.

  • Raspberry Pi 3 with the UPS Pico HAT sitting on top (including 450mAh battery)
  • Breadboard with Adafruit Ultimate GPS (including external antenna because I’m indoors) and the two buttons.
    I haven’t got the LEDs on yet.

The one addition is the HDMI Camera Extender I purchased from Tindie. This will allow me to have the camera mounted up near the top of the screen but have the main unit down on the dashboard somewhere: maybe down behind the gear change on my Golf Cabriolet.

PiDashCam Code Overview

The code comprises an installed system daemon with three threads:
1. Main thread
3. Camera thread
4. GPS thread

There are interrupt routines to handle the two buttons and there are several Events to manage signalling between the threads.

Pseudo Code

Subject to change – I’ll come back and revise this as development progresses

Button A interrupt handler (flush video)
  Wait for ExtraTime seconds
  set the flushBuffer event

Button B interrupt handler (stop recording)
  If recording
    Wait for ExtraTime seconds
    set the flushBuffer event
    wait for 
    reset the recording event
    Set the recording event

Camera thread
   Do until shutDown
      If recording
         Initialise Camera
         While recording
            start recording into buffer
            If flushBuffer
               flush buffer to new file
                  If shutdown is set
                     reset recording event
               update annotation with current date-time, position and speed
            wait for 0.2 seconds
      wait for 1 second

GPS Thread
    Initialise connection to gpsd
    Do until shutDown
        update current GPS info

Power failure
  wait for ExtraTime seconds
  set flushBuffer
  set shutdown

Main thread
  Kick off Camera thread
  Kick off GPS thread
  reset the flush video flag
  reset the shutdown flag
  set the record flag
  while not shutdown
      wait for 1 second
  while LAN is connected and there are videos in the sync folder
      wait for 1 second
  Kill threads
  initiate system shutdown

PiDashCam – Raspberry Pi based dashcam – Part One

This is the first in a series of posts about developing a Raspberry Pi based dashboard camera (dashcam) for my own personal use. I’m also using the project to improve my Python coding skills and re-use a whole load of software engineering patterns I used to deploy when I was a software engineer yonks ago.


Like many regular drivers, I see a lot of strange behaviour when I’m out on the roads and I often comment to myself that it would be interesting to have something on video. The lady who zoomed past me on the M25 busily touching up her eye makeup; the van driver who had his morning paper spread out across the steering wheel, the lunatic in the BMW who cut across three lanes on the M1 to avoid missing his exit and precipitated a multi-car shunt: the list goes on.

I have been meaning to buy a dashcam that would allow me to capture these delights on video.
However, rather than just shell out for a commercial dashcam, I decided to make one for myself and to my own specification.


These are the requirements I used to drive the system design

  • Single forward facing camera with option for second rearward facing camera
  • Built using Raspberry Pi and programmed in Python
  • Should continue recording even if power has failed (e.g. in an accident)
  • Record HD MPEG format videos continuously but avoid running out of disk space
  • Simple UI to record a notable event and pause/resume recording
  • Video overlaid with Date, Time, Position, Speed and Direction
  • Automatic transfer of recorded video to home Mac when in range of home Wi-Fi
  • to avoid need to physically access pidashcam to extract videos
  • to save space on pidashcam

System Design

After considering the above, I came up with the following system design

Raspberry Pi Model 3 in the Car

  • Connects automatically to Home WI-Fi network
  • PiModules UPS Pico to provide the battery backup, file-safe shutdown and RTC
  • Forward facing Pi Camera
  • Option for USB connected WebCam for rear facing camera
  • Adafruit Ultimate GPS breakout board to source the positional data
  • Two momentary push buttons for external inputs
  • Two LEDs to indicate system status
  • Custom system daemon written in Python to implement the PiDashCam features
  • Resilio Sync1 to synchronise the local video filesystem with my Mac whenever I am connected to home Wi-Fi

iMac in Home

  • Resilio Sync
  • Hazel to:
  • move new videos to another folder (and thus save space on PiDashCam)
  • convert the videos from h264 format to mpeg
  • fire off a Pushover alert to tell me that there are videos to watch

PiDashCam Use Cases

External power is applied to PiDashCam – e.g. ignition switched on

  • Start to record into 5 min circular buffer with positional data overlaid on video stream
  • Switch on LED 1 to indicate that recording is in progress
  • LED 2 remains off

Button A is pressed – flush the current buffer to disk

  • Save content of circular buffer plus another 5 mins of video to disk
  • Flash LED 1 2Hz 50:50 duty cycle whilst this is going on
  • Resume recording into circular buffer

Button B is pressed – flush and pause

  • if PiDashCam is recording
  • simulate Button A being pressed but then pause recording
  • extinguish LED 1
  • otherwise
  • resume recording

UPS signals it has gone on to battery – e.g. ignition switched off

  • Simulate Button B being pressed
  • If PiDashCam is connected to home Wi-Fi
  • Wait for the Videos folder to become empty
  • Flash LED 2 2Hz 50:50 duty cycle whilst this is going on
  • (Note: if UPS PIco battery runs down, it will shutdown PiDashCam safely)
  • shutdown
  • Otherwise
  • shutdown

More to follow

  1. Resilio Sync is a private P2P networking product from the people who brought us BitTorrent. I used the free Sync Home product.