# Speed Measurement using HC-SR04 Ultrasonic Sensor

In this tutorial, we will be learning how to make a simple speed measurement device using the HC-SR04 Ultrasonic sensor and Arduino

## What is an Ultrasonic Sensor (HC-SR04)?

Ultrasonic sensor is a sensor that accurately measure distance using ultrasonic waves. It a non-contact sensor used to accurately measure distance between the sensor and the object ranging from 4 centimeters to 4 meters. It is quite inexpensive and widely available, making it suitable for all kinds of DIY projects.

The ultrasonic sensor calculates the distance by recording the time take for the emitted ultrasonic wave to reflect back from the object.

The sensor first sends an ultrasonic pulse and sees how long it takes for the receiver to receive back the emitted pulse. Since we know the speed of sound, we can use simple speed formulae to calculate the distance that the sound wave might have traveled. Additionally, we will divide the result by 2 since the sound would have travelled twice the distance between sensor and the object – from sensor to object and back. The HC-SR04 consists of four pins, namely

• VCC (5V Power)
• Trig (Trigger)
• GND (Ground)

VCC and GND pins will be used to power the device while Trigger pin needs to be configured as an digital output pin while the echo pin needs to be declared as digital input pin.

The above pins can be connected to any microcontrollers, but for this guide we will be using the Arduino

## Components Required to Build a Speed Measuring Device are:

1. Arduino Uno (any Arduino can be used) 2. Ultrasonic sensor HC- SR04 3. Jumper wires ## Software Requirement

We will be using Arduino IDE to program our Arduino to read values from the ultrasonic sensor.

Alternatively, other popular code editors like PlatfromIO in Visual studio code can also be used but for simplicity we will be sticking with Arduino IDE

## Working Concept of Speed Measuring Device

To calculate the speed, the ultrasonic sensor needs to read the distance of object at least twice between a fixed interval of time, say 1s.

Let’s visualize this with an example,

Object X is moving with speed Y

The ultrasonic sensor is will measure the distance of the object every 1 second.

When the sensor measures the distance of the object the 1st time, the object X is at position A

When the sensor measures the distance of the object the 2nd time, the object X is at position B

Since the object moved |A-B| distance in 1 second, using the speed formulae, we can calculate the speed of the object

speed = distance / time

Here, speed = |A-B| / 1

The given result will be in the units of cm per second or cm/s

# Circuit Diagram The ultrasonic and Arduino connections are shown above as follows

 Arduino Ultrasonic Sensor 5V VCC GND GND Pin 5 TRIG Pin 6 ECHO

## Code

// Declaration of trigger and echo pins

int trigPin = 5;

int echoPin = 6;

// declaration of variables going to be used for the program

int distance1 = 0;

int distance2 = 0;

int measured_speed = 0;

long duration = 0;

int distance = 0;

void setup()

{

// Setting trigger pin as OUTPUT

pinMode(trigPin, OUTPUT);

// Setting echoPin as INPUT

pinMode(echoPin, INPUT);

// Starts the serial communication at baud rate 9600

Serial.begin(9600);

}

void loop()

{

// Measuring distance 1

delay(1000);                  // gives delay of 1 second

// Formula to calculate speed from distance1 and distance2

// We are dividing it by 1, since the time interval between the two distance measurement is 1000 ms or 1 second

measured_speed = abs(distance2 - distance1) / 1.0;

// Displaying the speed value on the serial monitor

Serial.print("Speed in cm/s :");

Serial.println(measured_speed);

}

// Function declaration to measure the distance based on the working principle of ultrasonic sensor

{

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Waits for the amount of time echoPin remains high and records the duration of the same

duration = pulseIn(echoPin, HIGH);

// Calculates the distance based on the speed of sound in ambient air

// and divide it by two since the sound traveled twice - once to the object and then back

distance = duration * 0.034 / 2;

// returning measured distance

return distance;

}