HC-SR04 Ultrasonic Sensor Tutorial with evive | Arduino IDE

ultrasonic sensor working
Description
Learn how to use the HC-SR04 Ultrasonic Sensor with Arduino to measure the distance between the sensor and an object. See the pin description, circuit diagram, and Arduino code for calculating the distance.

Introduction

An ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measures distance by sending out a sound wave at a specific frequency and listening to that sound wave bounce back.

Working

Ultrasonic sound vibrates at a frequency above the range of human hearing. Transducers are microphones used to receive and send ultrasonic sounds. HC-SR04 and other ultrasonic sensor modules use a single transducer to send a pulse and to receive the echo. The sensor determines the distance to the target by measuring the time lapse between sending and receiving of the ultrasonic pulses.

Distance Calculation

It is known that sound travels through air at a speed of 344 meters/second approximately, you can take the time taken by the sound wave to return and multiply it by 344 meters to get the total round-trip distance of the sound wave, round-trip time means total distance traveled by the wave i.e two times the distance between HC-SR04 ultrasonic sensor module and object from which ultrasonic wave reflected. To find the distance to the object simply divide round-trip time by 22, you can calculate the distance to the object by using a simple formula which is given below.

Pin Description

The HC-RS04 Ultrasonic sensor module has 4 pins, two pins for the power supply and one pin for sending out ultrasonic sound waves, and one pin for receiving ultrasonic sound waves.

  • VCC
  • GND
  • TRIG (for sending ultrasonic sound waves)
  • ECHO (for receiving ultrasonic sound waves)

Circuit Diagram

Place the HC-RS04 ultrasonic sensor module on the evive board such that the connecting wires or components of the evive board do not block the path of the ultrasonic sensor

  • Connect VCC of HC-SR04 module to VCC of evive board
  • Connect GND of HC-SR04 module to GND of evive board
  • Connect TRIG pin of HC-SR04 module to pin number 9 of the evive board
  • Connect ECHO pin of HC-SR04 module to pin number 10 of the evive board

Arduino Code

/*
* evive
* this code demonstrate interfacing of HC-SR0 Ultrasonic sensor module with evive
*/

const int trigPin = 9;   // connect Trig pin on pin number 9 on evive
const int echoPin = 10;  //  connect Echo pin on pin number 10 on evive

long duration;          //  declaring variable for storing duration 
int distance;           //  and distance between Ultrasonics sensor and object 

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

void loop() {
  
 // 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 the distance
distance= duration*0.034/2;

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

Expected Result

Conclusion

In this lesson, we learned about the HC-SR04 Ultrasonic Sensor and its working. We went through the pin description and circuit diagram of the module and wrote the Arduino code to calculate the distance between the Ultrasonic Sensor and the object. After uploading the code to the board, the distance between the HC-SR04 Ultrasonic Sensor and the object should be displayed on the Serial Monitor.

Table of Contents