Trying to build a device that senses cars passing by

Hello all! I'm purchasing my first Arduino with it, and the first thing that I'm trying to do is get it to recognize vehicles passing by within a 8-12 foot range and then ring a bell when one passes by. I'm not sure how to start this project, or what type of sensors I should use. Does anyone have any advice? Thank you so much!

Ultrasound sensors might be the easiest, this has a range of 4m, this of 6.

Thanks a lot! Loud noises shouldn't interfere with the signal, right?

Loud noises shouldn't interfere with the signal, right?

Not if "ultra" means what I think it means, no.... :wink:

There's loads of code for those things by the way: vendor sites like adafruit and sparkfun probably have, and there's some in the Playground, iirc.

A thing you need to think about: the Arduino will loop faster than the car gets out of the beam, so the code needs to set a flag when it sees the beam respond so that next time through loop() it doesn't think it's a new vehicle. Then clear the flag when the beam is clear, ready for the next vehicle.

edit.... although that said, just setting the bell pin high while the vehicle is there may be enough for the alert, and you're not counting the vehicles are you? Then you needn't worry about the foregoing.

I am currently in the process of creating a very similar thing, only for RC cars and mainly as a laptimer/counter.

I have written some code already, but as the parts have not arrived yet, no guarantee for it to work :slight_smile: Additonal Note: I am also kind of new to the whole Arduino thing, so don’t yell at me for bad coding.

// ---------------------------------------------------------------------------
// See:
// External libraries used: Sainsmart LCDKeys ( // NewPing (UltraSonic):
// ---------------------------------------------------------------------------

#include <NewPing.h>
#include <LiquidCrystal.h>
#include <sainsmartkeypad.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

//Global Vars
boolean raceStarted       = false;
unsigned int lapStart     = 0;
unsigned int lastLap      = 0;
unsigned int bestLapTime  = 0;
unsigned int elapsedTime  = 0;
unsigned int initialDist  = 0;
int minWait               = 2000; // Wait in ms before turning on Sonar again

//LCD Sepcific
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
SainsmartKeypad keypad(0);
int value = 0;
uint8_t key;

//Ultrasonic Sensor
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  //WHY NOT Serial.begin(9600); 
  Serial.println("Laptimer Ready");
  initialDist = ( / US_ROUNDTRIP_CM);
  Serial.print("Initial Distance: ");

void loop() {
  //Sainsmart Library Setup // ONLY FOR TESTING CURRENTLY
  key = keypad.getKey_fastscroll();
  if(key != SAMPLE_WAIT) // Do not refresh screen every loop
       case UP_KEY:
       case DOWN_KEY:
     lcd.print("        ");
  delay(30); //29ms should be the shortest delay between pings.
  unsigned int distance = ( / US_ROUNDTRIP_CM);

  //First contact with Sensor
  if (distance > 0 && distance != initialDist && raceStarted == false){
    Serial.println("Race Started!");

    Serial.print("Detected ");

    //Start the Race
    raceStarted = true;
    //Start the Timer
    lapStart = millis();
   //Check for elapsed time
   elapsedTime = millis() - lapStart;
  //Nth contact with the sensor (after minWait)
  if (distance > 0 && distance != initialDist && raceStarted == true && elapsedTime > minWait){
    //Stop the current Laptime
    lastLap = elapsedTime;
    //Start the new Lap
    lapStart = millis();
    //Convert to readable Time and Print
    showTime(lastLap, 1);
    //Check if new best Lap
    if(bestLapTime == 0){
      showTime(lastLap, 2);
    //-->Nth run
    if(lastLap < bestLapTime){
      showTime(lastLap, 2);

void showTime(unsigned long t_milli, unsigned int lcdLine){
   char buffer[20];
   int days, hours, mins, secs;
   int fractime;
   unsigned long inttime;

   inttime  = t_milli / 1000;
   fractime = t_milli % 1000;

   days     = inttime / (24*3600);
   inttime  = inttime % (24*3600);

   hours    = inttime / 3600;
   inttime  = inttime % 3600;

   mins     = inttime / 60;
   inttime  = inttime % 60;

   secs = inttime;

   sprintf(buffer, "%02d:%02d.%03d", mins, secs, fractime);
   //Determine which Line to print the Result
   if (lcdLine == 1){
     Serial.println(buffer); //LCD Line 1
   if (lcdLine == 2){
     Serial.println(buffer); //LCD Line 2

I use the SainSmart LCD/Key Shield

PM/Reply if you need some more help


A magnetometer (electronic compass) will sense the steel in automobile bodies several meters away.

Thank you so much for the help, guys! I bought 2 of these:

Does anyone know where I can find the information I need on what to buy to hook these babies up to the Arduino Uno and code them? Thank you!

Did you dig in the Playground as I suggested?...... there you will find this.