Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

Overview:

Ultrasonic sensors are great tools to measure distance and detect objects without any actual contact with the physical world. It is used in several applications, like in measuring liquid level, checking proximity and even more popularly in automobiles to assist in self-parking or anti-collision systems.

The basic principle of ultrasonic distance measurement is based on ECHO. When sound waves are transmitted in the environment then waves return back to the origin as ECHO after striking on the obstacle. So we only need to calculate the traveling time of both sounds means outgoing time and returning time to origin after striking on the obstacle. As the speed of the sound is known to us, after some calculation we can calculate the distance. We are going to use this same technique for this Arduino distance measurement project.

Components Used

  1. Arduino Uno
  2. Ultrasonic sensor Module
  3. 16x2 LCd
  4. Bread board
  5. Connecting wires

Ultrasonic Sensor Module

There are many types of Arduino distance sensors, but in this project we have used the HC-SR04 to measure distance in range of 2cm-400cm with an accuracy of 3mm. The sensor module consists of an ultrasonic transmitter, receiver and control circuit. The working principle of ultrasonic sensor is as follows:

  1. High level signal is sent for 10us using Trigger.
  2. The module sends eight 40 KHz signals automatically, and then detects whether pulse is received or not.
  3. If the signal is received, then it is through high level. The time of high duration is the time gap between sending and receiving the signal.

Distance= (Time x Speed of Sound in Air (340 m/s))/2

First of all we need to trigger the ultrasonic sensor module to transmit signal by using arduino and then wait for receive ECHO. Arduino reads the time between triggering and Received ECHO. We know that speed of sound is around 340m/s. so we can calculate distance by using given formula:

Distance= (travel time/2) * speed of sound

Where speed of sound around 340m per second.

A 16x2 LCD is used for displaying distance.

Arduino Ultrasonic Sensor Code for Distance Measurement

#include <Adafruit_LiquidCrystal.h> //librarry function for lcd

Adafruit_LiquidCrystal lcd_1(0);
void setup()
{
lcd_1.begin(16, 2);
pinMode(2, INPUT);//declaring pin 2 as input
delay(500);
}

void loop()
{
float inches = 0;
float cm = 0;
float total_cm=0;
float total_in=0;
cm = 0.01723 * readUltrasonicDistance(2);//formula for cm
inches = (cm / 2.54);//formula for inches

lcd_1.setCursor(1,0);
lcd_1.print("cm");
lcd_1.setCursor(0,1);
for(int i=0;i<5;++i)//for loop for filtering function
{
total_cm=cm+total_cm;
total_in=total_in+inches;
}
total_cm=total_cm/5;
total_in=total_in/5;
lcd_1.print(total_cm);
lcd_1.setCursor(8,0);
lcd_1.print("inches");
lcd_1.setCursor(8,1);
lcd_1.print(total_in);
}
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.