Challenge

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

Objectives:

  1. To Measure the distance in terms of inches of an object using Ultrasonic Distance Sensor (28015 Ping).
  2. The sensor data is smoothened by using Moving average filter technique to eliminate the high frequency variations from the sensor data.

Normally an analogue sensor sends a voltage level, usually between 0 and 5V, representing a physical value. This voltage can be subject to measurement noise (electronic interference, electromagnetic interference, measurement accuracy, etc.). In some applications, We will need a fairly stable value to make our calculations or to detect the events we are interested in. A simple method to set up is the sliding average method which allows modifying the value read according to the measurement history.

Ultrasonic Distance Sensor (28015)

The Parallax PING))) ultrasonic distance sensor provides precise, non-contact distance measurements from about 2 cm (0.8 inches) to 3 meters (3.3 yards). It is very easy to connect to microcontrollers such as the BASIC Stamp, Propeller chip, or Arduino, requiring only one I/O pin. The PING))) sensor works by transmitting an ultrasonic (well above human hearing range) burst and providing an output pulse that corresponds to the time required for the burst echo to return to the sensor. By measuring the echo pulse width, the distance to target can easily be calculated.

Features of PING)))28015:

  1. Range: 2 cm to 3 m (0.8 in to 3.3 yd)
  2. Burst indicator LED shows sensor activity
  3. Bidirectional TTL pulse interface on a single I/O pin can communicate with 5 V TTL or 3.3 V CMOS microcontrollers
  4. Input trigger: positive TTL pulse, 2 µs min, 5 µs typ.
  5. Echo pulse: positive TTL pulse, 115 µs minimum to 18.5 ms maximum.
  6. RoHS Compliant.

Practical Considerations for Use Object Positioning The PING))) sensor cannot accurately measure the distance to an object that: a) is more than 3 meters away, b) that has its reflective surface at a shallow angle so that sound will not be reflected back towards the sensor, or c) is too small to reflect enough sound back to the sensor. In addition, if your PING))) sensor is mounted low on your device, you may detect sound reflecting off of the floor.

Communication Protocol:

The PING))) sensor detects objects by emitting a short ultrasonic burst and then "listening" for the echo. Under control of a host microcontroller (trigger pulse), the sensor emits a short 40 kHz (ultrasonic) burst. This burst travels through the air, hits an object and then bounces back to the sensor. The PING))) sensor provides an output pulse to the host that will terminate when the echo is detected, hence the width of this pulse corresponds to the distance to the target.

Host Device

Input Trigger Pulse

tOUT

2 µs (min), 5 µs typical

PING))) Sensor

Echo Holdoff

tHOLDOFF

750 µs

Burst Frequency

tBURST

200 µs @ 40 kHz

Echo Return Pulse Minimum

tIN-MIN

115 µs

Echo Return Pulse Maximum

tIN-MAX

18.5 ms

Delay before next measurement

200 µs

Schematic :

An analogue input is preferably connected to an analogue pin on the microcontroller. There is no additional connection required to operate the sliding average. It is a purely algorithmic solution. For more controlled results, it is possible to put electronic filters on your measurement circuit.

Moving average filter technique

The principle of moving average is to record a certain number of measurements in an array and then average these values at each reading.

There are several shortcomings to this:

The array takes a while to fill in, therefore, a delay in establishing the average. Please take this into account when starting the microcontroller. The variation of the measurement will be slower, so it will be impossible to detect “fast” phenomena. Check carefully the behaviour of what you want to measure. The array takes up more memory space and requires more management time.

The above sample fig shows the different between Actual (Blue) reading and Averaged readings(Red).

The Code:

// Project-2 "Find the Distance of an object"

#include <Adafruit_LiquidCrystal.h> //I2C LCD header file

#include <Wire.h> //I2C communication header file

Adafruit_LiquidCrystal lcd(0); //Initialze LCD

int inches = 0; //Variable initialization for Inches

int cm = 0; // Variable initialization for CM

const int echoPin = 7; //set Signal Pin 7, both trigger and echo are same pin

// Ultrasonic sensor reading function declaration for 28015

long readUltrasonicDistance(int triggerPin, int echoPin)

{

pinMode(triggerPin, OUTPUT); // Enable the trigger as output

digitalWrite(triggerPin, LOW); // To stop the trigger

delayMicroseconds(2); // For clear the trigger

digitalWrite(triggerPin, HIGH); // Start sending wave

delayMicroseconds(10); // For 10 Micro seconds

digitalWrite(triggerPin, LOW); // Stop wave

pinMode(echoPin, INPUT); // Eco pin get back the wave

// Reads the echo pin,and send the sound wave travel time

return pulseIn(echoPin, HIGH); // Eco pin getting high

}

void setup()

{

Serial.begin(9600); // Start serial communication

lcd.begin(16,2); // Start 16x2 LCD

Wire.begin(9); // Start I2C Communication

}

void loop()

{

smooth(); //smooth function calling

cm = 0.01723 * readUltrasonicDistance(7, 7);// measure the ping time

inches = (cm / 2.54); // convert to inches by dividing by 2.54

lcd.setCursor(0,0); // LCD cursor set

lcd.print("The distance is "); //LCD print the string

lcd.setCursor(0,1);

lcd.print(inches); //LCD Print distance value in inches

lcd.print(" INC, ");

lcd.setCursor(8,1);

lcd.print(cm); //LCD Print distance value in cm

lcd.print(" CM, ");

Serial.print(inches); //Serial monitor Print distance value in inches

Serial.print("INC, ");

Serial.print(cm); //Serial monitor Print distance value in cm

Serial.println("CM");

delay(100); // Delay for 100 millisecond(s)

}

long smooth() // Moving average function

{

long average=0;

const int numReadings = 10;//Declare the average range

int readings [numReadings]; //Sample readings stored in a array

int readIndex = 0;

int total = total - readings[readIndex]; // subtract the last reading

readings[readIndex] = analogRead(echoPin); // read the sensor

total = total + readings[readIndex]; // add value to total

readIndex = readIndex + 1; //increase the sampling count

if (readIndex >= numReadings)

{

readIndex = 0;

}

average = total / numReadings; // calculate the average

return average; //return the average

}

The output:

Conclusion:

Distance measure by using ARDUINO with 28015 PING))) Ultrasonic sensor and smoothen the sensor output by using the Moving Average Filter method was done.

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.