Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
ABINAYA MAM
THEORY
ULTRASONIC SENSOR
An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves.An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object’s proximity.
Ultrasonic sensors work by sending out a sound wave at a frequency above the range of human hearing. The transducer of the sensor acts as a microphone to receive and send the ultrasonic sound. Our ultrasonic sensors, like many others, use a single transducer to send a pulse and to receive the echo. The sensor determines the distance to a target by measuring time lapses between the sending and receiving of the ultrasonic pulse.
The working principle of this module is simple. It sends an ultrasonic pulse out at 40kHz which travels through the air and if there is an obstacle or object, it will bounce back to the sensor. By calculating the travel time and the speed of sound, the distance can be calculated.
Ultrasonic sensors are a great solution for the detection of clear objects. For liquid level measurement, applications that use infrared sensors, for instance, struggle with this particular use case because of target translucence.
For presence detection, ultrasonic sensors detect objects regardless of the color, surface, or material (unless the material is very soft like wool, as it would absorb sound.)
To detect transparent and other items where optical technologies may fail, ultrasonic sensors are a reliable choice.
PING))) Ultrasonic Distance Sensor Overview
Our PING)))™ ultrasonic sensor provides an easy method of distance measurement. This sensor is perfect for any number of applications that require you to perform measurements between moving or stationary objects.
Interfacing to a microcontroller is a snap. A single I/O pin is used to trigger an ultrasonic burst (well above human hearing) and then “listen” for the echo return pulse. The sensor measures the time required for the echo return, and returns this value to the microcontroller as a variable-width pulse via the same.
ADVANTAGES OF ULTRASONIC SENSOR
This sensor is not affected due to atmospheric dust, rain, snow etc.
It can work in any adverse conditions.
It has higher sensing distance (in centimeters and inches) compare to inductive/capacitive proximity sensor types.
It provides good readings in sensing large sized objects with hard surfaces.It has sensing capability to sense all the material types.It has sensing capability to sense all the material types.
APPLICATION OF ULTRASONIC SENSOR
<!-- [if !supportLists]-->· <!--[endif]-->Anti-Collision Detection
<!-- [if !supportLists]-->· <!--[endif]-->People Detection
<!-- [if !supportLists]-->· <!--[endif]-->Contouring or Profiling
<!-- [if !supportLists]-->· <!--[endif]-->Presence Detection
<!-- [if !supportLists]-->· <!--[endif]-->Box Sorting using a Multi-Transducer System
<!-- [if !supportLists]-->· <!--[endif]-->Easy Control of Trash Collection Vehicles
<!-- [if !supportLists]-->· <!--[endif]-->Pallet Detection with Forklifts
<!-- [if !supportLists]-->· <!--[endif]-->Bottle Counting on Drink Filling Machines
PING))) Ultrasonic Distance Sensor Overview
Our PING)))™ ultrasonic sensor provides an easy method of distance measurement. This sensor is perfect for any number of applications that require you to perform measurements between moving or stationary objects.
Interfacing to a microcontroller is a snap. A single I/O pin is used to trigger an ultrasonic burst (well above human hearing) and then “listen” for the echo return pulse. The sensor measures the time required for the echo return, and returns this value to the microcontroller as a variable-width pulse via the same.
ADVANTAGES OF ULTRASONIC SENSOR
This sensor is not affected due to atmospheric dust, rain, snow etc.
It can work in any adverse conditions.
It has higher sensing distance (in centimeters and inches) compare to inductive/capacitive proximity sensor types.
It provides good readings in sensing large sized objects with hard surfaces.It has sensing capability to sense all the material types.It has sensing capability to sense all the material types.
APPLICATION OF ULTRASONIC SENSOR
<!-- [if !supportLists]-->· <!--[endif]-->Anti-Collision Detection
<!-- [if !supportLists]-->· <!--[endif]-->People Detection
<!-- [if !supportLists]-->· <!--[endif]-->Contouring or Profiling
<!-- [if !supportLists]-->· <!--[endif]-->Presence Detection
<!-- [if !supportLists]-->· <!--[endif]-->Box Sorting using a Multi-Transducer System
<!-- [if !supportLists]-->· <!--[endif]-->Easy Control of Trash Collection Vehicles
<!-- [if !supportLists]-->· <!--[endif]-->Pallet Detection with Forklifts
<!-- [if !supportLists]-->· <!--[endif]-->Bottle Counting on Drink Filling Machines
COMPONENTS REQUIRED
Arduino board
ultrasonic sensor
connecting wires
PROGRAM
#define WINDOW_SIZE 5
int inches = 0;
int cm = 0;
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int READINGS[WINDOW_SIZE];
int AVERAGED = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
SUM = SUM - READINGS[INDEX]; // Remove the oldest entry from the sum
VALUE = inches; // Read the next sensor value
READINGS[INDEX] = VALUE; // Add the newest reading to the window
SUM = SUM + VALUE; // Add the newest reading to the sum
INDEX = (INDEX+1) % WINDOW_SIZE; // Increment the index, and wrap to 0 if it exceeds the window size
AVERAGED = SUM / WINDOW_SIZE;
// Divide the sum of the window by the window size for the result
Serial.println("The distance data is");
Serial.println(inches);
Serial.println("The smoothened distance data is");
Serial.println(AVERAGED*100);
delay(100); // Wait for 100 millisecond(s)
}