Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter :
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. Previously we have also build many ultrasonic sensor projects like water level detecting.ultra radar etc . This is an efficient way to measure small distances precisely. In this project, we have used the HC-SR04 Ultrasonic Sensor with Arduino to determine the distance of an obstacle from the sensor. 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, so let's get started.
Component used :
* Arduino Uno or Pro Mini
* Ultrasonic sensor Module
* 16x2 LCD
* Scale
* Bread board
* 9 volt battery
* Connecting wires
ultra 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:
* High level signal is sent for 10us using Trigger.
*The module sends eight 40 KHz signals automatically, and then detects whether pulse is received or not.
* 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.
ultra sensor circuit diagram explanation:
The circuit diagram for arduino and ultrasonic sensor is shown above to measure the distance. In circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly connected to pin 18(A4) and 19(A5) of arduino. A 16x2 LCD is connected with arduino in 4-bit mode. Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3. And data pin D4-D7 is connected to 4, 5, 6 and 7 of arduino.
Code :
#include
LiquidCrystal lcd_l(12,11,5,4,3,2);//define lcd module pind
int inches=0;//variable
int cm=0;//variable
ling readUltrasonicDistance(int triggerpin,int echopin)//function definition
{
pinMode(triggerpin,OUTPUT);
digitalWrite(triggerpin,Low);
delay(2);//wait fpr 2ms
digitalwrite(triggerpin,HIGH);
delay;//wait for 10ms
digitalWrite(triggerpin,LOW);
pinMode(echopin,INPUT);
return pulseIn(echopin,HIGH);//wait and return the pulseIn function value of echo pin with high
}
void setup()
{
serial.begin(9600);//initializes the serial communication with a baud rate of 9600
lcd_l.begin(16,2);// Set up the number of columns and rows on the LCD.
lcd_l.setCursor(0,0);
lcd_l.print("Measuring Distance");//display on LCD
lcd_l.serCursor(0,1);
lcd_l.print("Ultrasonic sensor");
for(int i=0;i<16;i++)//for loop , Repeating 16 times
{
lcd_l.scrollDisplayLeft();//scrolls the text on the LCD to the left 16 times with a delay of 100 milliseconds between each //scroll
delay(100);//very 100 milliseconds
}
lcd_l.clear();//clear lcd screen
}
void loop()
{
//measure the ping time in cm
cm=0.01723*readUltrasonicDistance(7,7);
//covert to inches by dividihng by 2.54
inches=(cm/2.54);
Serial.print(inches);
Serial.print("inch");
Serial\.print(cm);
Serial.print("cm\n");//print"cm" on serial monitor
delay(100);//delay of 100ms
lcd_l.setcursor(0,0);//set cursor 1st row 1st column
lcd_l.print("Distance");//display "distance" on lcd screen
lcd_l.setCursor(0,1);
lcd_l.print(inches);//display value of inch in lcd screen
lcd_l.print("inch");//display "inch" on lcd screen
lcd_l.print(cm);//display value of cm on lcd screen
lcd_l.print("cm");//display "cm"on lcd screen
delay(1000);//wait for 1000 millisecond
}