Building Radar Speed Camera and Traffic Logger

with a Raspberry Pi

I’ve always been interested in connecting things up to computers that they were never designed for. I could reminisce about my first drawing capture arm that used captured the two joint angles with potentiometers and used them to control the pulse width oscillators that fed into the cassette input on a ZX Spectrum. (I’ve just done a google search for this idea and came up blank - I’m sure there used to be commercial products - it looks like this technology has been lost in the digital dark ages.) Over the last couple of decades it’s been hard going as most business focused and mass produced hardware has little hackability. With the advent of the Raspberry Pi and the looming Internet of Things it’s now really easy to have a lot of fun. Partly it’s about control but it’s also about curiosity and being able to see and understand things that are not normally visible either because we don’t have the physical senses or the timescales are too short or long.

In this post I want to describe my latest “experiment” with a doppler radar. I’ve played around with PIR sensors and light sensors in the past and both of these have their limitations as motion sensors. Radars are becoming quite popular for automotive applications from parking sensors all the way to autonomous vehicles so I decided to see what I could get working.

High level system architecture High level system architecture

Hardware - Radar Module

The radar technology I found was a 24Ghz stereo doppler radar module. Cheaper 10Ghz modules are available on ebay and it appears that most automotive radars use 77Ghz. It appears to be the case that higher frequencies give better range. The module I have used has the limitation that it doesn’t measure distance to a target - all it can detect is movements towards or away from the sensor. This is achieved due to the doppler effect which causes the echo from a moving object to be of higher or lower frequency than the original frequency depending upon if it’s moving towards or away respectively. Because the 24Ghz signal is way too high to sample and process directly the sensor module works by subtracting the transmitted signal from the received echo. This causes beating and the result is a much lower frequency which is proportional to the speed of a target. The reason the sensor is called stereo is because it has two outputs. These two outputs don’t detect in different directions but instead are used to detect if a target is moving towards or away from the radar. This is done by detecting the phase of the received echo with one output receiving the real and the other the complex part. A target moving towards the sensor leads to a positive phase shift and away a negative phase shift. The output frequencies are in the range of human hearing with each 1mph roughly 72Hz (or 44Hz per 1kmh)

B+B Sensors RSM2650 24GHz Stereo Radar Sensor Module B+B Sensors RSM2650 24GHz Stereo Radar Sensor Module

Multiple objects moving at different speeds are independently detectable because of their different frequencies. The following spectrogram (Audacity is great) shows a recording of the response pointing at a fairly busy motorway. You can also see how the relative speed and hence frequency varies as the vehicles move away from the sensor - this is known as the cosine effect. You can hear what this sounds like from this audio sample on soundcloud.

Spectrogram of radar output pointing at motorway Spectrogram of radar output pointing at motorway

Hardware - Signal Amplifier

Unfortunately the output from the radar module is really feeble. After experimenting with a number of approaches I settled on using an operational amplifier circuit fed into a the line-in on a sound card. It seems that the choice of operational amplifier is not that critical. I tried three (LM833n, NE5532a and LM358) and few different circuit designs but the signal to noise ratio was pretty much constant. High frequency noise from the radar module and it’s sensitivity to picking up electromagnetic hum 50Hz/60Hz seem to be the main issues. Here is a plot of the noise spectrum when no movement is present:

Noise Spectrum from output of amplifier Noise Spectrum from output of amplifier

This is the circuit I ended up using, obviously times two for the two outputs from the radar:

Amplifier circuit diagram Amplifier circuit diagram

I powered this from a separate regulated 5v supply because the noise on the USB 5v power is horrendous.

Update: Peter has been experimenting with a pre build audio amplifier module based on the LM386 (http://www.ebay.co.uk/itm/141507760671) and has had success eliminating the 50Hz hum)

This is what it looks like assembled with radar module connected:

Assembled radar module and amplifier Amplifier circuit diagram

Hardware - Soundcard

Because the signals are in the audible range use of the sound card solves a lot of problems such as sampling at high rate, avoiding sampling jitter and buffering the samples. Sound cards are also pretty cheap. I got this one from eBay for £10 It looks like overkill but it’s hard to get one with a proper line-in rather than just a microphone.

USB Soundcard with LineIn USB Soundcard with LineIn

Hardware - Raspberry Pi

I used a standard Raspberry Pi module B. The two USB ports are just enough to attach the sound card and a WiFi dongle. I installed it with the latest Raspbian image and set up the WiFi and SSH access.

Hardware - Enclosure

After experimenting with circuit I wanted to mount it outside near the road - the closer the better in terms of signal to noise. For the strength of reflection from vehicles it looks like a range of about 50m is the absolute maximum. Luckily I have power from a nearby circuit for a pond pump. After problems with hum pickup and also noise from the WiFi signal I placed the sensitive radar module and amplifier separate from the Raspberry Pi and power supplies. I found these great DriBoxes work really well. Here is what it looks like installed near the road.

Radar installed in garden Radar installed in garden

Software

On the Raspberry Pi I have a single Python program which performs all the processing to
capture the signal from the sound card through to emitting events when vehicles are detected.

The code has dependencies on python-alsaaudio, python-numpy and optionally python-httplib2 for posting events to a HTTP rest web service.

Here is a block diagram of the processing performed. The code is available on this GitHub repository.

Software block diagram Software block diagram

Results

It’s early days in terms of analysing the data. My initial data has been simply captured in a csv file and then analysed with a spreadsheet. I’ve also started send the cumulative flow numbers into polestar. On there you can see a live feed.

Here is the speed distribution of the first 24 hours of captured data:

Distribution of speeds over 24 hours Distribution of speeds over 24 hours

Here is a scatter graph of traffic speed vs time for the first 24 hours of data:

Scatter graph of speeds over 24 hours Distribution of speeds over 24 hours

Here is a time series bar chart for the first 4 days of traffic flow:

Time series bar chart with 4 days of traffic flow Time series bar chart with 4 days of traffic flow