Thursday, 14 January 2016

The Near-miss-o-meter, Part 1

It all started with this tweet from @beztweets:
with a link to this great post by Dr. Ian Walker. I'd seen a few of these previously (whenever a '3 foot rule' is introduced somewhere) but the suggested addition of a GPS seemed to make sense, possibly for the Near Miss Project run annually by Dr. Rachael Aldred and Emily Brooke.

Coincidentally, I was about to receive my Raspberry Pi Zero on the front of the MagPi magazine. It seemed like a perfect match: a few days off for Christmas, a £4.00 computer, a £10.49 USB GPS (you can get them cheaper if you're will ing to wait for shipping from China) and a £1.61 ultrasonic sonar (HC-SR04) from that auction site.

The Aim

To create and document a compact design that can be built by anyone using easily available components without requiring a huge amount of technical knowledge. Although I played a little with electronics as a teenager, my knowledge is pretty much limited to knowing the difference between a capacitor and a resistor. I do like a challenge.

This is the first of several posts that will briefly document the journey and finish up with a more detailed 'howto' of a reference design.

Prototype 1: Pi, USB GPS, Sonar

After a mad run up to Christmas, I had half a day free, so started playing. Hooking up the Pi to a breadboard and wiring in the sonar is well documented on the web (e.g. here). Major lesson learnt here was how to drop 5 volts (from the HC-SR04) down to 3.3 volts (also known as 3V3, the maximum voltage allowed by the input pins on the Pi) using two resistors (or any three resistors of the same value using two in series).

With the USB GPS plugged into a powered hub, we were away!

Not quite. The Pi works very well with the sonar as long as that is all it is doing. The HC-SR04 works by setting the ECHO pin 'high' for the length of time it takes for the ultrasound 'ping' to bounce to and from its target, but if you start to add any logic into the measuring process, the Pi wanders off to do other things and forgets to listen to the ECHO pin for a few nano seconds. This results in frequent over-long distance measurements.

Thankfully, I'd done a bit of reading, so it was quickly on to:

Prototype 2: Pi, USB GPS, Digispark+Sonar

A Digispark? No, I hadn't heard of them either. They are a tiny, USB powered Arduino. About the size of a postage stamp and costing all of £2.99 on eBay. Unlike the Pi, this is a realtime microprocessor (with 8kb of memory!).

Shamefully, I'd not yet played with an Arduino before so there was a small learning curve to get up to speed. This excellent post, meant the Digispark was quickly wired to the sonar and plugged into the Pi.

Digispark + HC-SR04
Success! Consistent distance readings now being recorded with lon/lat, time, heading and speed from the GPS. Here we have a complete (hardware) solution for less than £20 (plus a USB booster battery to power the Pi on the go).

There was one minor issue: all the data was being recorded onto the Pi's SD card. This is fine for me (we have been 100% Microsoft free since 1997...) but a Windows PC cannot read the Linux Ext4 file system on the card without installing additional software. It is theoretically possible to dual partition the SD card, but Windows still struggles if it doesn't recognise the first partition. I still think this approach has mileage (particularly overlaying data on video etc) but given my my goal of this solution to be easily built by anyone, this was getting a little too techie. Besides, I was now learning about Arduino - perhaps I should follow Ian Walkers lead...

As you may have noticed from the picture above, I also discovered a new tool whilst trying to document my wiring: Fritzing - the open source world never ceases to amaze me!

Coming up in Part 2: The Arduino(s)


  1. fascinating. I'd like to have one of these, so I'm looking forward to your future reports. I wonder if it could feed a digital screen over the rear wheel - giving drivers feedback on passing distance? my compliments

  2. It could feed distance to a screen (there are a few example Arduino sketches that do this) but you would likely have to lose some other functionality (e.g. GPS) because of the limited resources of the Arduino. Also, you could not display the measurement until after the driver was along side.