Project tutorial
Arduino Car Counter

Arduino Car Counter © LGPL

A low-power Arduino car counter that works by logging each time a car drives over a rubber tube across the road.

  • 1,001 views
  • 0 comments
  • 3 respects

Components and supplies

11113 01
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
Pimoroni RV 3028 RTC Breakout
×1
13743 01
SparkFun Level Shifting microSD Breakout
×1
NXP MPX5100DP Pressure Sensor
×1
A 4118 2
TaydaElectronics DC POWER JACK 2.1MM BARREL-TYPE PCB MOUNT
×1
37ac8591 40
Flash Memory Card, MicroSD Card
×1
SparkFun Barrel jack power switch
×1
4mm double headed bulkhead connector
×1
clear plastic tube, 4mm internal diamter
×1
4mm internal diameter rubber fuel hose
This is what the cars are going to drive over. Usually sold on ebay as something like “Cotton Braided Rubber Fuel Hose for Unleaded Petrol / Diesel, Oil Line Pipe”. Get as much as you need to go all the way across your road.
×1
4mm hose T-connector
this is to seal the end of the hose
×1
Pelicase 1120
Any waterproof case of this size works well
×1
31ac4603 40
Hook Up Wire Kit, 22 AWG
×1

Necessary tools and machines

About this project

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

We’re not using interrupts so we can ignore this - 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:

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:

Coding

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.

Housing

Drill 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!

Testing and calibrating

With the first piece of code uploaded, seal the tube 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.

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 consumption

I 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.

Code

Car counter pressure testArduino
This code allows you to see that the pressure sensor is working. It's just the main code with some bits commented out.
/* ---------------------------------------------------------------
    I am trying to record a reading when the reading from an air
    pressure sensor jumps suddenly - ie when a car drives over a tube
    attached to the sensor. The Pressure Sensor is a MPX5500DP 
    I am using the movingAvg library to simplify the maths a bit.
   ---------------------------------------------------------------*/

#include <movingAvg.h> // https://github.com/JChristensen/movingAvg
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <RV-3028-C7.h>                 // this is a low-power clock

const uint8_t airSensor(A0);  // connect pressure sensor from A0 pin to ground
movingAvg airAvg(20);        // define the moving average object
//
//unsigned long new_time=0;     // set some variables for preventing reading multiple spikes
unsigned long old_time=0;     // 

RV3028 rtc;                  // get the clock going

String timestamp;           //

     
const int chipSelect = 8;      // SD card pin
int ledPin = 5;                // the pin the LED is connected to

File logFile; // the logging file



/* --------------------------------------------
/*  setup the average, pullup the air sensor, 
 *   begin the serial monitor and show an initial 
 *   reading so we knnow it is working
   --------------------------------------------*/
void setup()
{
  Wire.begin();
     Serial.begin(9600);                 // begin serial monitor
     if (rtc.begin() == false)
    {
     Serial.println("Something went wrong, check wiring");
     while (1);
    }
    else
      Serial.println("RTC online!");
    
  delay(1000);
   pinMode(airSensor, INPUT_PULLUP);   // air sensor

   airAvg.begin();                     //averages
 
   pinMode(chipSelect, OUTPUT); 
   digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library
   delay(100);
   
  // initialize the SD card
  Serial.print("Initializing SD card...");

  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(8, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("Card initialized.");
  Serial.print("Logging to: ");
  Serial.println("TRAFFIC.CSV");
  logFile = SD.open("TRAFFIC.CSV", FILE_WRITE);
  logFile.println("");
  logFile.println("NEW SESSION");
  logFile.close();

  Serial.println("Setup complete");
  Serial.println("initial reading");
  int pc = analogRead(airSensor); // read the sensor
  Serial.println(pc);
}

/* --------------------------------------------
/*  Each loop should comapare the reading against 
 *   the moving average, and if it is greater than 
 *   the specific amount, print this to the monitor
   --------------------------------------------*/
void loop()
{
   rtc.updateTime();                       // get the time
   int pc = analogRead(airSensor);        // read the sensor
   int avg = airAvg.reading(pc);          // calculate the moving average
//   int avgPlus2 = avg + 2;               // to simplify conditional below
//   unsigned long new_time = millis();    // this is to make sure peaks are spaced, in case a single count causes a double spike
//   
  delay(10);   // For some reason, the If statement that follows doesn't work without a delay here?????
//    
//   if ((pc > avgPlus2) && ((new_time - old_time) > 500))  // if the reading is greater than the average, print it
   {

    // write data to serial
    Serial.print(rtc.stringDate());
    Serial.print(" ");
    Serial.print(rtc.stringTime());
    Serial.print(", ");
    Serial.print(pc);
    Serial.print(", ");
    Serial.println(avg);
    
    logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it
    logFile.print(rtc.stringDate()); 
    logFile.print(" ");
    logFile.print(rtc.stringTime());
    logFile.print(", ");
    logFile.print(pc);
    logFile.print(", ");
    logFile.println(avg);
    logFile.close();


//    old_time = new_time;  // spacing spikes

   }
////   else
//   {
//      delay(1);    // this is needed for some reason to make the IF statement work
//    
//
//   }
}

 
Car Counter Full CodeArduino
This code counts each time the pressure sensor is triggered
/* ---------------------------------------------------------------
    I am trying to record a reading when the reading from an air
    pressure sensor jumps suddenly - ie when a car drives over a tube
    attached to the sensor. The Pressure Sensor is a MPX5500DP 
    I am using the movingAvg library to simplify the maths a bit.
    Sarah Dalrymple - Aetos999
   ---------------------------------------------------------------*/

#include <movingAvg.h>  // https://github.com/JChristensen/movingAvg
#include <SD.h>
#include <SPI.h> 
#include <Wire.h>
#include <RV-3028-C7.h>        // https://github.com/constiko/RV-3028_C7-Arduino_Library

const uint8_t airSensor(A0);  // connect pressure sensor from A0 pin to ground

// This is the moving average - change the figure in brackets to what you want

movingAvg airAvg(20);        

unsigned long new_time=0;     // set some variables for preventing reading multiple spikes
unsigned long old_time=0;     // 

RV3028 rtc;                  // get the clock going

String timestamp;           //
     
const int chipSelect = 8;      // SD card pin
int ledPin = 5;                // the pin the LED is connected to

File logFile; // the logging file



/* --------------------------------------------
/*  setup the average, pullup the air sensor, 
 *   begin the serial monitor and show an initial 
 *   reading so we knnow it is working
   --------------------------------------------*/
void setup()
{
  Wire.begin();
     Serial.begin(9600);                 // begin serial monitor
     if (rtc.begin() == false)
    {
     Serial.println("Something went wrong, check wiring");
     while (1);
    }
    else
      Serial.println("RTC online!");
    
  delay(1000);
   pinMode(airSensor, INPUT_PULLUP);   // air sensor

   airAvg.begin();                     //averages
 
   pinMode(chipSelect, OUTPUT); 
   digitalWrite(chipSelect, HIGH); //ALWAYS pullup the ChipSelect pin with the SD library
   delay(100);
   
  // initialize the SD card
  Serial.print("Initializing SD card...");

  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(8, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("Card initialized.");
  Serial.print("Logging to: ");
  Serial.println("TRAFFIC.CSV");
  logFile = SD.open("TRAFFIC.CSV", FILE_WRITE);
  logFile.println("");
  logFile.println("NEW SESSION");
  logFile.close();

  Serial.println("Setup complete");
  Serial.println("initial reading");
  int pc = analogRead(airSensor); // read the sensor
  Serial.println(pc);
}

/* --------------------------------------------
/*  Each loop should comapare the reading against 
 *   the moving average, and if it is greater than 
 *   the specific amount, print this to the monitor
   --------------------------------------------*/
void loop()
{
   rtc.updateTime();                       // get the time
   int pc = analogRead(airSensor);        // read the sensor
   int avg = airAvg.reading(pc);          // calculate the moving average
   int avgPlus = avg + 5;               // to simplify conditional below
   unsigned long new_time = millis();    // this is to make sure peaks are spaced, in case a single count causes a double spike
   
   delay(1);   // For some reason, the If statement that follows doesn't work without a delay here?? I think this is a bug in my system

 // if the reading is greater than the average & however many ms has passed since last time, print it. 
 // This is the ms value between peaks - change it to help calibrate your counter
   if ((pc > avgPlus) && ((new_time - old_time) > 400))  
   {

    // write data to serial
    Serial.print(rtc.stringDate());
    Serial.print(" ");
    Serial.print(rtc.stringTime());
    Serial.print(", ");
    Serial.print(pc);
    Serial.print(", ");
    Serial.println(avg);
    
    logFile = SD.open("TRAFFIC.CSV", FILE_WRITE); // open TRAFFIC.CSV file on SD Card and write to it
    Serial.println("log");
    logFile.print(rtc.stringDate()); 
    logFile.print(" ");
    logFile.print(rtc.stringTime());
    logFile.print(", ");
    logFile.print(pc);
    logFile.print(", ");
    logFile.println(avg);
    logFile.close();
    Serial.println("done.");

    old_time = new_time;  // spacing spikes

   }
   else
   {
      delay(1);    // this is needed for some reason to make the IF statement work
    

   }
}

 

Schematics

Arduino Car Counter
This is a rough approximation of the circuit; the RTC and the pressure sensor aren't the models you use in my project but they have the same connections. (I'm rubbish at fritzing, half of the parts aren't in the library and nothing lines up with the holes. )
Carcounter bb hs8t84uvqv

Comments

Similar projects you might like

Turn your RC Car to Bluetooth RC car

Project tutorial by Prajjwal Nag

  • 20,675 views
  • 2 comments
  • 25 respects

RC Car to BT Car Controlled with Blynk

Project tutorial by Team Daxes Hacks

  • 7,670 views
  • 2 comments
  • 7 respects

Car Counter using Arduino + Processing + PHP

Project tutorial by Team UNO

  • 12,541 views
  • 4 comments
  • 9 respects

Biometric Car Entry - True Keyless Car

Project showcase by Rajeev Velikkal

  • 9,917 views
  • 10 comments
  • 46 respects

Metal Detector Using Frequency Counter and OLED Display

Project tutorial by Andrius Purr

  • 3,955 views
  • 2 comments
  • 23 respects

Hot Wheels Car Photogate

Project tutorial by nfarrier

  • 3,734 views
  • 2 comments
  • 13 respects
Add projectSign up / Login