Ahmed Ebeed
Published © CC BY-NC-SA

How to Build Arduino Ultrasound Speed Measurement Device

Wouldn’t it be cool to build an Arduino Speed Measuring Device simply and learn from it?

IntermediateFull instructions provided1 hour1,310
How to Build Arduino Ultrasound Speed Measurement Device

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Tinkercad
Autodesk Tinkercad

Story

Read more

Schematics

Circuit

Code

Arduino Code

Arduino
// Video on Youtube

//  https://www.youtube.com/watch?v=LDfpC_XlJVM

//  https://www.youtube.com/watch?v=kua3zsn4dHA





const int trigPin = 5;
const int echoPin = 6;


long duration;
int FirstDistance=0;
int SecondDistance=0;
double speed=0;
int distance=1;
float Time = 2.0;
float delayedtime = 1000*Time;

void setup()
{
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
Serial.begin(9600); 
}


void loop()
{
CalcSpeed();

}

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

// Sets  trigPin HIGH  for 10 microsec
  
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

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

//calcdistance
  
distance= duration*0.034/2;

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

return distance;

}
void CalcSpeed(){
 
   FirstDistance = CalcDistance(); //get the first distance
   
   delay(delayedtime); //waits 2 seconds depending on the time declared above ,, feel free to change the value dependng on the resolution of your sensor 
   
   SecondDistance = CalcDistance(); //gets the second distance
   
   speed = (FirstDistance - SecondDistance)/Time;  // now calculating the difference 
   
 
// print speed on  serial monitor
  
  Serial.print("the speed (cm/s) is  :  ");
  Serial.println(speed);
 
}

Credits

Ahmed Ebeed

Ahmed Ebeed

17 projects • 3 followers
Love to try new things and make stuff.

Comments