Speed Measurement Using HCSR-04 Ultrasonic Sensor

Somebody pls help with arduino uno code on how to calculate speed of moving object using HCSR04 Ultrasonic Sensor.It can be a single sensor or multiple sensors.....Pls help.I need the code

Hi,

If you take two distance measurements a short time apart, you just have:

distance1 - distance2 = distance speed = distance / time

You get to pick the units...

Remember, however, that common U/S sensors have very limited range, so you may only get in a couple of measurements if the target is moving fast.

The code is tested and working fine. It also includes LED blinking code.
Do make sure to insert it in the correct pin.

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance1=0;
int distance2=0;
double speed=0;
int distance=0;

void setup() 
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode( 7 , OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() 
{

//calculating speed
   distance1 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
   delay(1000);//giving a time gap of 1 sec
   
   distance2 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
   //formula change in distance divided by change in time
   speed = (distance2 - distance1)/1.0; //as the time gap is 1 sec we divide it by 1.
   
//Displaying speed
  Serial.print("Speed in cm/s  :  "); 
  Serial.println(speed);

   
// LED indicator
if (distance >0 && distance <5) 
{
    digitalWrite( 7 , HIGH);
    delay(50);                  // waits for a second
}

if (distance > 5 && distance <10 ) 
{
    digitalWrite( 7 , HIGH);
    delay(50);                  // waits for a second
    digitalWrite( 7 , LOW);    // sets the LED off
    delay(50);                  // waits for a second
}

if (distance >10  && distance < 20) 
{
    digitalWrite( 7 , HIGH);
    delay(210);                  // waits for a second
    digitalWrite( 7 , LOW);    // sets the LED off
    delay(210);                  // waits for a second
}

if (distance >20  && distance < 35) 
{
    digitalWrite( 7 , HIGH);
    delay(610);                  // waits for a second
    digitalWrite( 7 , LOW);    // sets the LED off
    delay(610);                  // waits for a second
}


}

float ultrasonicRead ()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

//calculating distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance in cm : ");
Serial.println(distance);

return distance;

}

speedF.ino (2.27 KB)

1 Like

Civilised_Falcon:
The code is tested and working fine.

...but won't get you any style marks.

Civilised_Falcon:
The code is tested and working fine. It also includes LED blinking code.
Do make sure to insert it in the correct pin.

// defines pins numbers

const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance1=0;
int distance2=0;
double speed=0;
int distance=0;

void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode( 7 , OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop()
{

//calculating speed
  distance1 = ultrasonicRead(); //calls ultrasoninicRead() function below
 
  delay(1000);//giving a time gap of 1 sec
 
  distance2 = ultrasonicRead(); //calls ultrasoninicRead() function below
 
  //formula change in distance divided by change in time
  speed = (distance2 - distance1)/1.0; //as the time gap is 1 sec we divide it by 1.
 
//Displaying speed
 Serial.print("Speed in cm/s  :  ");
 Serial.println(speed);

// LED indicator
if (distance >0 && distance <5)
{
   digitalWrite( 7 , HIGH);
   delay(50);                  // waits for a second
}

if (distance > 5 && distance <10 )
{
   digitalWrite( 7 , HIGH);
   delay(50);                  // waits for a second
   digitalWrite( 7 , LOW);    // sets the LED off
   delay(50);                  // waits for a second
}

if (distance >10  && distance < 20)
{
   digitalWrite( 7 , HIGH);
   delay(210);                  // waits for a second
   digitalWrite( 7 , LOW);    // sets the LED off
   delay(210);                  // waits for a second
}

if (distance >20  && distance < 35)
{
   digitalWrite( 7 , HIGH);
   delay(610);                  // waits for a second
   digitalWrite( 7 , LOW);    // sets the LED off
   delay(610);                  // waits for a second
}

}

float ultrasonicRead ()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

//calculating distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance in cm : ");
Serial.println(distance);

return distance;

}

Hello, i found your code is useful!

May i know how is the hardware setup?

1 or 2 ultrasonic sensors to measure 2 distance points?

The sensor installed on side way or towards the moving vehicle?

1 Like

Hello there!
In this code they have used a single ultrasonic sensor, if you want to add extra sensor just add numbers to each pin referring to the different sensors.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.