Home / Home made Projects / Arduino Projects / Simulation of HC SR04 Ultrasonic range finder using Arduino

Simulation of HC SR04 Ultrasonic range finder using Arduino

In this article we will learn how to simulate HC SR04 ultrasonic range finder using Arduino. Article covers basic concept and working of ultrasonic range finder. Also the schematic and code required for simulation of HC SR04 ultrasonic range finder using Arduino are also presented. In the end, the working is proved from simulation results.

Working of the ultrasonic sensor:

Ultrasonic range finder works in the similar fashion as the sonar used by ships and submarines. The sensor sends the sound wave and it is reflected by the object it hits. By using the speed of the sound, the distance to the sound-reflected object can be found by measuring the time it takes to travel to and from the object back to the receiver.

The working of HC SR04 ultrasonic range sensor can be seen from the following figureworking of HC SR04The ultrasonic sensors operate beyond the audible human frequency which is 40KHz. The input of ultrasonic sensor is triggered by the 10us pulse and then it measures the time of the reflected sound wave at the echo pin by using the built-in “microseconds” function. Timing diagram HC SR04It can be seen from above figure that after giving 10us pulse, HC_SR04 produces ultrasonic waves than waits for its echo. When the echo is received, the propagation delay time between sending and receiving is noted. It is the time for traveling both towards the obstacle and coming back to sensor. So actual distance will be calculated by taking half of this time.

Calculations HC SR04 ultrasonic range 

The calculation for Ultrasonic range finder is based upon the basic physics formula

Where S= Distance

V= Velocity

T= time

If any two quantities are known, the third one can be calculated using the given formula. So to find the distance, the speed and time must be known.

Ultrasonic sensor utilizes the sound waves to measure the distance of the object. The speed of sound in air is constant which is 343m/s in dry air at 20 ºC.

Converting value from (m/s) to smaller units (cm/microseconds)

0.0343 is a fractional value. For ease in programing, we convert it into whole numbers by taking its reciprocal

It means that in 29.15 us, sound travel distance of 1cm. Similarly 291.5us will correspond to 10cm distance.

The speed of sound in air changes with temperature and humidity. Therefore, in order to accurately calculate distance, we need to consider the local ambient temperature and humidity. The formula for the speed of sound in air that factors in temperature and humidity is:

c = 331.4 + (0.606 x T) + (0.0124 x H)

c = Speed of sound in meters per second (m/s)

331.4 = Speed of sound (in m/s) at 0 °C and 0% humidity

T = Temperature in °C

H = % Humidity (relative humidity)

For example, at 20 °C and 50% humidity, sound travels at a speed of:

c = 331.4 + (0.606 x 20) + (0.0124 x 50)

c =  344.02 m/s.

Above calculations will be used in HC SR04 ultrasonic range finder using Arduino programming.

Schematic

The main components used in this project are

  • HC-SR04 Ultransonic sensor
  • Arduino UNO R-3
  • Supply 5V.
  • LCD (16×2)
  • Resistors

The connection diagram is shown in the following figureschematic of HC SR04 ultrasonic range finder using Arduino

LCD is interfaced with Arduino to show the calculated range.  Also To display results on PC, virtual terminal is added as well.

The Simulation employs the variable resistor to simulate HC-SR04. A library is developed to simulate HC-SR04 ultrasonic range sensor which is available free of cost at

http://www.theengineeringprojects.com/2015/02/ultrasonic-sensor-library-proteus.html

The ultrasonic range sensor library can be downloaded for free from above link. To mimic the obstacle and its response in sensor, the library requires user to connect variable resistor to virtual test pin of the sensor. High voltage means more distance as a result, sensor will receive echo with more delay. Less voltage, smaller delay and less distance.

Simulation

The proposed circuit is simulated in ISIS. The simulation model of ultrasonic range finder is downloaded from above mentioned link.

HC SR14 ranger finder simulataion

Initially a very low voltage is set at variable resistor. After triggering, the sensor receives the response.  The trigger as well as prorogation delay is shown by the oscilloscope.The delay is 2 squares which is 2ms (according to scale selected in the scope).

This is the time taken by sound in travelling both direction i-e towards obstacle and back to sensor after reflection. Time for one side will be 1ms. Speed of sound as already mentioned is 0.0343cm/us. So distance will be 1000x 0.0343 = 34.3 cm which is shown by the LCD and virtual terminal.

HC SR04 ultrasonic ranger finder result 1

Similarly the voltage is now increased which means extended distance. The results are shown.

Result 2 HC SR04 ultrasonic range finder

The results are displayed on LCD screen and virtual terminal. Similary calculation can bedone as above to verify the results. They are left for u!

Code HC SR04 ultrasonic range finder using Arduino

#include <LiquidCrystal.h> // Libraries
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor
const int debug = 0; // If we want to debug put =1
LiquidCrystal lcd(12,11,5,4,3,2); //pin of Arduino
Servo myservo; // SETUP
void setup(){
  Serial.begin(9600);
  pinMode(6, INPUT);   // button
  pinMode(7,INPUT);
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  lcd.begin(16,2); // 16 characters 2 rows
  lcd.print(“Help2educate”);
  delay(2000);
  lcd.setCursor(0,0);
  lcd.print(“UltrasonicHCSR04”);
  lcd.setCursor(0,1);
  lcd.print(”  Range Finder   “);
  delay(2000);
}
void loop(){
  button = 0; // As default
  lcd.clear(); // Clean Screen
  lcd.setCursor(0,0); // beginning
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH); //Reads a pulse (either HIGH or LOW) on a //pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts //timing, then waits for the pin to go LOW and stops timing. Returns the length of //the pulse in microseconds or 0 if no complete pulse was received within the //timeout.
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print(“in, “);
Serial.print(cm);
Serial.print(“cm”);
Serial.println();
lcd.setCursor(0,0);
lcd.print(“Inches:”);
lcd.print(inches);
lcd.setCursor(0,1);
lcd.print(“Centimeters:”);
lcd.print(cm);
  }
 delay(1000);
}
//My function print lcd
void print_status(char* text){
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(“Status”);
  lcd.setCursor(0,1);
  lcd.print(text);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

If you have any question related to HC SR04 ultrasonic range finder using Arduino, feel free to ask in commentsif you

About Harris Fayyaz

Muhammed Harris Fayyaz, electrical engineer from NUST, EME, has 2.5 yrs industrial experience. He does research work in control systems, micro controllers and their applications.

Check Also

Arduino based power meter using Emon Library

This article assist the user to construct home made Arduino based power meter. The connection …

One comment

  1. Zaidatul Akmal bt Mohammad Khairuddin

    Thank you so much for these explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *

*