Search

# Portable Distance Sensor

Updated: May 12, 2021

I have made a portable distance sensor with an ultrasonic sensor. By pushing a button you are able to take a measurement and display that reading on an LCD. I am using a bread board power supply connected to a 9V battery to power the circuit; you can also use a battery pack to power it.

The Ultrasonic Sensor works by emitting a sound pulse frequency of 35 - 55kHz through the Trig pin and receiving the sound pulse from the Echo pin.

Using simple Physics you can determine how far the sound pulse has travelled. By rearranging the formula Vf = d/t you will get d = Vf * t; and substituting the speed of sound in place of Vf and the time it took for the sound pulse to be received in place of t.

The echo pin on the ultrasonic sensor will give you a reading in microseconds. To change the units you will be using dimensional analysis. By using dimensional analysis you will be able to convert your readings to whatever units; I converted my readings to Cm/s. Remember that there are 1000000 microsecond in a seconds and the speed of sound is 343m/s. After you have converted your units substitute your values in to the formula

d = Vf * t. After you have calculated the distance remember to divide that distance by two; Because what you have calculated is the distance it took for the sound pulse to be emitted and received and not the distance between your object and the sensor.

One problem I ran into was that whenever I made

a measurement I got a different result every time. To counteract that problem I took the average of 100 readings.

## Components Needed

1. 1X Arduino NANO

2. 1X LCD (16 X 2)

3. 1X Potentiometer (10 K)

4. 1X HC-HR04 Ultrasonic Sensor

5. 1X Push Button

6. 1X Bread Board (5.5Cm x 17Cm)

7. Bread Board Power Supply with 9V Battery or Battery pack

8. Jumper Wires

I have provided the code below:

```#include <LiquidCrystal.h>
int rs =7;
int en = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int trig_pin = 2;
int echo_pin = 3;

int ping_travel_time;
float ping_travel_distance;
float distance_to_target;

int dt = 5000;

int button = A0;
int old_button_state = 1;
int new_button_state;

int j;
int num_measurements = 100;
float average;

void setup() {
// put your setup code here, to run once:
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);

pinMode(button, INPUT);
digitalWrite(button, HIGH);

lcd.begin(16, 2);

Serial.begin(9600);
}

void loop(){
// put your main code here, to run repeatedly:

lcd.setCursor(0, 0);
lcd.print("Push the button");

while(old_button_state == 1 and new_button_state == 0){
float total = 0;

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Measuring...");

for(j = 1; j <= num_measurements; j += 1){
digitalWrite(trig_pin, LOW);
delayMicroseconds(10);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);

ping_travel_time = pulseIn(echo_pin, HIGH);
delay(25);

ping_travel_distance = (((ping_travel_time/1000000.)*343.)*100);
distance_to_target = (ping_travel_distance/2);

total += distance_to_target;
}

average = (total/num_measurements);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance");
lcd.setCursor(0, 1);
lcd.print("= ");
lcd.print(average);
lcd.print("Cm");
delay(dt);
lcd.clear();

}

old_button_state = new_button_state;
}

```  