While talking about electronics at work, my colleague metnioned a car counter he’d bought for a large sum of money that never really worked. I had the idea that I could build one myself, from Arduino, and thought I’d give it a try.
Internet searching found the following projects that I used as inspiration:
Arudino people counting, using PIR (passive infra-red)
Car counting using a rubber tube – anonymous project on makercave. This alsoincludes a really useful PDF from Tomorrowlab
Car counting using a rubber tube – Tomorrowlab
Kris Temmermen’s car counter on Hackaday
All of these sites were incredibly useful, both for ideas, inspiration and of course code help.
The plan was to use a pressure sensor to record when there is a change in pressure inside a sealed rubber tube, such as when a car drives over it. Simple! I wanted to make it as low-power as possible, so it would run for a good while before needing batteries changed. It’s to be deployed in a rural location, counting cars coming in and out of a car park.
There was then weeks AND WEEKS of experimenting, testing and trialling, but here’s what I ended up with. This assumes you have some experience of using Arduinos, soldering, and coding.
Connecting it up:The Arduino Pro Mini has offset A4 (SDA) and A5 (SCL) pins. This is a pain when using a breadboard because they don’t line up with any holes. The trick is to solder them to A2 and A3 respectively, turning those pins into SDA and SCL. It’s a bit fiddly! Here’s what it looks like with my mediocre soldering skills:
Breadboard everything first, so you can see how things connect up. Later, when you’re happy everything works, you can arrange them on a prototype board for soldering.
Arduino Pro Mini has two power pins: VCC and RAW. RAW is power in, which gets converted by the Arduino to 5V. The closer to 5V your power in is, the more efficient the Arduino will run. Take the power from the Arduino VCC pin for everything else.
Here’s how the RV3028 RTC connects to the arduino:
Arduino - RV3028 RTC
A5 (SCL) - SCL
A4 (SDA) - SDA
Gnd - Gnd
VCC - 2-5V
I've a new line of code that allows the arduino to go to sleep when the car park is closed, so we need to connect the RTC interrupt to the arduino.
2 - INT
Let’s connect up the Sparkmatch Shifting microSD cardbreakout:
Arduino - MicroSD breakout
GND - GND
9 - CD
13 - DO
12 - SCK
11 - DI
8 - CS
VCC - VCC
Finally the Pressure sensor. This has 6 pins and two air ports. But we’re only going to use three pins, and attach the hose to one ofthe ports. Which ones?
Look at the data sheet for the MPX5100DP:
That's easy. The pin with a bite taken out of it is pin 1. Here’s how to wire it to the arduino:.
Arduino - MPX5100DP Pin
A0 - 1 (Vout)
GND - 2 (GND)
VCC - 3 (Vs)
The pressure sensor works by measuring the pressure in one of the ports, and comparing it to the pressure in the other port. The difference is returned as a voltage between 0 and 5. This can then be converted to Kpa or mb but we don’t really need to know that, so we’ll just use the raw values the pressure sensor returns to us.
(When I first set this up, I used the MPX5500. This measures up to 500kPa, which means it wasn’t sensitive enough to recognise a lot of cars. I switched to the MPX5100, which measures up to 100kPa, and the sensitivity was vastly improved. The company, NXP have a forum on their website which was really helpful in figuring this out).
Here’s how it looks on a breadboard. The red and black wires are going to the battery, then I'm using green and yellow for +ve and -ve from the arduino.
Without the components hiding wires:
To code this, I first learned how to use an Arduino Uno with an RTC. Then I learned how to use the microSD card reader, and tried a basic logging program – logging temperature to SD. Next, I worked out how to take readings from the pressure logger. Then I learned about the Arduino Pro Mini, and finally put them all together – so it’s not imposible for a relative beginner.
The first code to upload (I am assuming you’ve set the time on your RTC, and checked the RTC and the microSD reader are all functioning) is a simple one. It will return whatever the pressure sensor is reading every 10ms, display this on the serial monitor and write it to a csv file on themicroSD card. There’s also some code in there to keep track of a running average of the previous 10 readings.
[Car counter pressure test code]
Set this up, and if it runs, blow into the tubes on the pressure sensor, make sure you see which one is measuring pressure, and how much it’s changing by. Have a look at the csv file too, then delete it and we’ll try the car counting code.
This next code waits until the reading from the pressure sensor is a certain amount above the average of the past twenty readings. This means it measures the data spikes. It also waits 400ms between readings, so for example, a car taking a while to cross the tube doesn’t register as many. (I think this could be improved with a debounce or similar instead however).
[Full car counter code]
Try this code and run it. Blow into the tube and see if it registers as a hit.
if you’re happy with everything and it all seems to work, you might want to now solder everything to the prototyping board. I use female header sockets rather than solder the RTC, microSD and the Arduino directly on to the board, so that they’re easily replaced when something goes wrong.
HousingDrill a hole in the plastic housing case for the bulkhead connector, and seal it in place. To fix my breadboard I glued a bit of wood inside the case, very hi-tec. I snipped small bits of the black tube and used them as spacers under the prototyping board! Use a short length of the small transparent tubing to connect the correct pressure nozzle to the bulkhead connector. Connect up the switch and the battery, the black rubber tube to the outside and it is ready to deploy!
Fix your rubber tube to the road (I just use rawl plugs and saddle clips), and seal the other end. You can do this by using the T-connector at the end, and then fit a short piece of tube from onearm of the T to the other in a D-shape. Or just put some sealant in there and a screw, or however you like.
With the first piece of code uploaded, take it outside, switch it on, and drive over the tube several times. Switch it off, and have a look at the.csv file on the microSD card to see what is happening. Every 10ms, a row will have been recorded, showing the date, pressure recorded and average pressure from the past twenty readings. By adding ina column simply counting the rows, and then dividing it by 100, I was able to make this graph showing the spikes by my car and my truck (and me) going overthe tube, with the number of seconds on the X axis:
Looking at this, the easiest would surely be to record a car whenever the reading is equal or greater than 60. However, we’re in the middle of a cold winter and I’m not sure how the pressure readings will change in hot, sunny weather – will the pressure in the tube increase greatly? So instead, I’m going to take a reading whenever the pressure is more than 7 above the average of the last ten readings.
You can also see a peak for each wheel axle. I looked at the data, and the average time between each axle is 0.345s (min 0.22s, max 0.56s) so I think a delay of 400ms (0.40s) should work OK to stop a single axle being recorded more than once. This is only from a sample of 7 vehicle triggers though, so I might change this.
As for each vehicle causing two spikes (one for each axle) –the easiest way to deal with this is simply to divide the final number by two when you get the data off the SD card.
Once you’ve decided where you are going to measure cars, the black tube can be held in place with saddle-clips fixed to the road with screws and rawlplugs. Try to position them so they won’t cause a puncture. For slow traffic (which I’m using, going in and out of a car park on a narrow track) this should be enough, but faster traffic may stretch and move the tube.
Set everything up, and try it out, see what data you get back. I am able to use a motion-activated camera and leave it in place for a few days, then compare the number of cars photographed vs the numbers recorded, but be wary of this (particularly in Europe) as you will need to consider data protection laws if you can’t put the camera where the reg plate isn’t recorded.
Instead you might need to sit and count cars for a few hours! Keep adjusting the triggers, both the moving average and the trigger value, until it accurately counts the cars.
Power consumptionI don’t have a way of measuring low power, but I calculated the power consumption from the components as using 193mA when triggered, and 11.3mA when at rest. I had it running for 147 hours off 1900mAh batteries (12mA average), but there weren’t many cars to trigger it. There are loads of ways to further reduce power consumption of an Arduino pro mini (see https://www.gammon.com.au/power) but this is low enough for me for now.
Comments