Intro
Wouldn’t it be cool to build an Arduino Speed Measuring Device simply and learn from it?
We’ve seen in previous posts how we can use the ultrasonic sensor transducer HC-SR04 to measure distances using the ultrasonic waves by sending ultrasonic wave then receiving it using this sensor and by knowing the speed of sound we can determine the distance the wave has traveled and here is on Tinkercad simulation software we are using the same sensor with Arduino UNO to measure the speed of objects this time.
Here is how you can start building your own Arduino Speed Measuring Circuit using Ultrasonic wave and the HC-SR04 Transducer.
Hardware Components
Arduino UNO
HC-SR04 Ultrasound Transducer
Circuit Connection
Software Code
// 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);
}
Simulation
Here is the Tinkercad Model
So if you didn’t yet subscribe please subscribe to our channel to see our latest videos.