Verified schematic diagram of distance meter using Arduino UNO and HC SR04 Ultrasonic sensor

Verified schematic diagram of distance meter using Arduino UNO and HC SR04 Ultrasonic sensor

What is a Distance Meter and what are the applications of a Distance Meter?

An Ultrasonic Sensor based Distance meter enables distance measuring of a target object without making any physical contact. It is efficient and suitable for many applications that require sensitive measurements in places where it is impractical to measure distance physically.

Apart from measuring distance, Ultrasonic Sensor based Distance Meters can also be used for measuring Liquid Level in a water tank or in a fuel tank.

Components Needed For Distance Meter Design

1 x Arduino UNO Board (Nano or Mega Board can also be used)

1 x HC SR04 Ultrasonic Sensor

1 x HDD44780 drive based 16x4 LCD

1 x 10K Ohm Variable Resistance

1 x 1K Ohm Variable Resistance

2 x 470 Ohm Resistor

1 x LED

1 x MOSFET IRF Z44N (Optional)

1 x DC Motor (Optional)

1 x Breadboard / PCB

1 x Jumper wires set

Simulation on Proteus

It is recommended to simulate this project before developing hardware. The main advantage of using Proteus is its diverse functions. You can design or edit project features according to your requirements without the need for soldering/desoldering any wires. The designer can also validate the functioning of code within the simulation.

Proteus is easy to use, and if you are using it for the first time, you can download Proteus from the downloads in the bottom and learn how to use Proteus from VerifiedSchematics YouTube channel.

 

Arduino Code

/*
 *         Distance Meter
 * Code by VerifiedSchematics.com
 * https://www.facebook.com/verifiedschematics/
 */
#include <LiquidCrystal.h>
LiquidCrystal LCD(13, 12, 11, 10, 9, 8);
int trigPin=3;
int echoPin=2;
int AlarmPin=7;
int myCounter=0;
int j=15;
char wvs[] = "     VerifiedSchematics.com";
int servoControlPin=6;
float pingTime;
float Distance_inches;
float Distance_meters;
float speedOfSound=776.5;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(AlarmPin, OUTPUT);
LCD.begin(16,4);
LCD.setCursor(0,1);
LCD.print("                ");
LCD.setCursor(0,0);
LCD.print("                ");
}

void loop() {
  digitalWrite(trigPin, LOW); //Set trigger pin low
  delayMicroseconds(2000); //Let signal settle
  digitalWrite(trigPin, HIGH); //Set trigPin high
  delayMicroseconds(15); //Delay in high state
  digitalWrite(trigPin, LOW); //ping has now been sent
  delayMicroseconds(10); //Delay in high state
  pingTime = pulseIn(echoPin, HIGH);  
  pingTime=pingTime/1000000; 
  pingTime=pingTime/3600;
  Distance_inches= speedOfSound * pingTime;  
  Distance_inches=Distance_inches/2; 
  Distance_inches= Distance_inches*63360;   
  Distance_meters = Distance_inches*0.0254;
  if (Distance_inches <=20)
     {
      digitalWrite(AlarmPin, HIGH); //Set Alarm pin High
     }
   else
     {
      digitalWrite(AlarmPin, LOW); //Set Alarm pin low
     }
  LCD.setCursor(0,0);
  LCD.print(" Distance Meter ");
  LCD.setCursor(1,1);
  LCD.print(Distance_inches);
  LCD.setCursor(7,1);
  LCD.print(" inche(s)");
  LCD.setCursor(1,2);
  LCD.print(Distance_meters);
  LCD.setCursor(7,2);
  LCD.print(" meter(s)");
  for (int z=0; z<27; z++)
     {
      wvs[27] = wvs[0];
      wvs[z] = wvs[z+1];
     }
  LCD.setCursor(0,3);
  LCD.print(wvs);
  delay(100);
  }

How does an HC SR04 Ultrasonic Sensor Based Distance Meter Works?

The primary components of this distance meter are Ultrasonic sensor and Arduino Board. The Ultrasonic Sensor is capable of transmitting and receiving ultrasonic waves.

When a distance is to be measured, the ultrasonic sensor transmits ultrasonic waves, and the microcontroller in Arduino board starts a counter when the ultrasonic waves hit any obstacle (placed in front of the ultrasonic sensor), it reflects to the sensor. When the ultrasonic waves are received at the ultrasonic sensor, the microcontroller stops the counter and measures the total duration of wave travel.

Using the speed of light and total time taken, the microcontroller determines the distance of the obstacle from the ultrasonic sensor. The measured distance is then displayed on an LCD in inches and meters.

In Proteus simulation, the distance is varied using a variable resistance connected with the ultrasonic sensor. As the distance cannot be physically changed in simulation, the value of variable resistance can be changed for varying distance.

To authenticate proper functioning of distance meter, a dc motor and an LED has been connected with the Arduino. According to the code, the DC motor and LED will turn on only if the measured distance is less than 20 inches.

Distance meter code 20inches condition

Distance meter code 20inches condition

Until the distance remains below 20 inches, the motor keeps rotating and the LED glows.

Distance below 20 inches

Distance below 20 inches

As soon as the distance is increased beyond 20 inches, the motor and LED turn off.

Distance above 20 inches

Distance above 20 inches

A similar function is used in autonomous robots that operate independently. When they detect any obstacle in front of them they change their direction (typically the direction of at least one motor is changed).

The distance changes as the variable resistance’s value is varied. The maximum achievable distance in proteus simulation is approximately 444 inches. Practically the value can be different, depending upon the manufacturer.

Maximum range of distance meterMaximum range of distance meter

Maximum range of distance meter

You can edit Arduino code according to your requirements and change the minimum/maximum limit for actuating DC motor and LED. For example, if we wanted to change the limit to 70%, the value in “if condition” shall be changed from “20” to “70.”

Less than 70 inches code condition

Less than 70 inches code condition

As the value is changed to 70, the DC motor and LED will remain on until the distance remains below 70 inches.

Distance below 70 inches

Distance below 70 inches

Using distance meter for security

This distance meter can be used as a security sensor, fix it on the side jamb of your door. Change the condition according to the width of your door in a way. For example, if anyone passes through the door, a small distance reading is encountered, and an alarm is initiated. The overall schematic diagram will remain the same, but the DC motor must be replaced with a buzzer or an Alarm.

Arduino code for using distance meter as an intruder sensor

/*

*         Distance Meter For Door Security

* Code developed by VerifiedSchematics.com

* https://www.facebook.com/verifiedschematics/

*/

 

#include <LiquidCrystal.h>

LiquidCrystal LCD(13, 12, 11, 10, 9, 8);

int trigPin=3;

int echoPin=2;

int AlarmPin=7;

int myCounter=0;

int j=15;

char wvs[] = ”     VerifiedSchematics.com”;

int servoControlPin=6;

float pingTime;

float Distance_inches;

float Distance_meters;

float speedOfSound=776.5;

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(AlarmPin, OUTPUT);

LCD.begin(16,4);

LCD.setCursor(0,1);

LCD.print(”                “);

LCD.setCursor(0,0);

LCD.print(”                “);

}

void loop() {

digitalWrite(trigPin, LOW); //Set trigger pin low

delayMicroseconds(2000); //Let signal settle

digitalWrite(trigPin, HIGH); //Set trigPin high

delayMicroseconds(15); //Delay in high state

digitalWrite(trigPin, LOW); //ping has now been sent

delayMicroseconds(10); //Delay in high state

pingTime = pulseIn(echoPin, HIGH);

pingTime=pingTime/1000000;

pingTime=pingTime/3600;

Distance_inches= speedOfSound * pingTime;

Distance_inches=Distance_inches/2;

Distance_inches= Distance_inches*63360;

Distance_meters = Distance_inches*0.0254;

if (Distance_inches <=30)

{

digitalWrite(AlarmPin, HIGH); //Set Alarm pin High

delay(500);

digitalWrite(AlarmPin, LOW); //Set Alarm pin low

delay(100);

digitalWrite(AlarmPin, HIGH); //Set Alarm pin High

delay(500);

digitalWrite(AlarmPin, LOW); //Set Alarm pin low

delay(100);

digitalWrite(AlarmPin, HIGH); //Set Alarm pin High

delay(500);

digitalWrite(AlarmPin, LOW); //Set Alarm pin low

delay(100);

}

else

{

digitalWrite(AlarmPin, LOW); //Set Alarm pin low

}

LCD.setCursor(0,0);

LCD.print(” Distance Meter “);

LCD.setCursor(1,1);

LCD.print(Distance_inches);

LCD.setCursor(7,1);

LCD.print(” inche(s)”);

LCD.setCursor(1,2);

LCD.print(Distance_meters);

LCD.setCursor(7,2);

LCD.print(” meter(s)”);

for (int z=0; z<27; z++)

{

wvs[27] = wvs[0];

wvs[z] = wvs[z+1];

}

LCD.setCursor(0,3);

LCD.print(wvs);

delay(100);

  }

Downloads

Arduino UNO and Ultrasonic Sensor Library for Proteus Software

Proteus Simulation File

Arduino IDE Simulation FIle

Proteus 7.7 Software

Arduino IDE Software