Challenge

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

Aim:

  • To measure the distance of an object using an ultrasonic sensor and also smoothen the sensor data using a moving average filter
  • To Measure the distance traveled by the wave to the object and gets bounced back from the object to the sensor
  • Smoothening the sensor data by moving average filter technique, the filter has a window of a certain size and it determines how much data the filter can take

Ultrasonic sensor

  • It calculates the target's distance by the emission of ultrasonic sound waves and converts those waves into electrical signals
  • It provides precise non-contact distance measurement within a 3cm to 3 m range
  • The sensor (28015 Ping) consists of a chip 40KHz signal generator, an ultrasonic speaker, and an ultrasonic microphone
  • Ultrasonic sensors work by emitting a sound wave and measuring the time it takes for the wave to bounce back after hitting an object
  • The distance can be calculated based on the speed of sound in the air and the time taken for the wave to return

Connections

  • Connect the VCC pin of the sensor to the 5V output of your Arduino or external power supply. This pin provides power to the sensor
  • GND pin to the GND (ground) on Arduino for common ground reference
  • Connect the SIG (signal) pin to a digital pin on Arduino (D13), which will be used to trigger the sensor and receive the echo signal

Reference design as well as code in Tinkercad are shown below

Here the signal is connected to digital pin 7

int inches = 0;

int cm = 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);

Serial.print(inches);

Serial.print("in, ");

Serial.print(cm);

Serial.println("cm");

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

}

  • Here cm and inch values are declared as integer
  • The triggerPin and echoPin are defined for the trigger and echo pins of the sensor
  • The triggerPin is output while echoPin is initialized as input
  • When the triggerPin is high the echoPin read and return the sound wave travel time in microseconds
  • The baud rate is 9600
  • The value measured is displayed in inches as well as cm
  • For measuring value from the ultrasonic sensor, the value is calculated with 0.01723 to make it in cm
  • The value in cm can be converted to inches by dividng 2.54 as 1 cm=2.54 inches
  • The values obtained are displayed in serial monitor and this repeats in the loop

  • With reference to available code as well as other sources following code is done to measure the distance using an ultrasonic sensor
  • The circuit implemented for the same is shown below
  • For the code below the sample size is defined using # define

// C++ code

//

int inches = 0;

#define SAMPLESIZE 10 // Filter Size Or Window Size

const int US_PIN = 13;

int INDEX = 0;

int VALUE = 0;

int SUM = 0;

int avg[SAMPLESIZE];

int AVERAGE = 0;

int cm = 0;

void setup()

{

//pinMode(US_PIN, INPUT); //ultrasonic sensor as input pin

Serial.begin(9600);

}

void loop()

{

pinMode(US_PIN, OUTPUT);

digitalWrite(US_PIN, LOW);

delayMicroseconds(5);

digitalWrite(US_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(US_PIN, LOW);

//Moving average algorithm

SUM = SUM - avg[INDEX]; // subtract previous values

VALUE = analogRead(US_PIN);// Read from ultrasonic sensor

SUM = SUM+VALUE;

INDEX = (INDEX+1)%SAMPLESIZE;//Increment the index

//wrap it to zero if sample size is exceeded

AVERAGE = SUM / SAMPLESIZE; //Moving average formula

Serial.print(VALUE);

Serial.print(",");

//Serial.print("Smoothed:");

Serial.println(AVERAGE);

delay(500);

}

  • Sum, average, index, and value are declared as integer
  • Pin 13 is the ultrasonic pin and US_pin is initialized using pinMode
  • With a delay of 5 as well as 10 seconds digitalWrite is high and low
  • Average algorithm is implemented for the calculation
  • Sum value is calculated by subtracting from previous values
  • The value is read from the sensor and is added to the sum value
  • The index value is incremented and the average value is calculated by dividing the sum value by average value
  • The value and average value calculated are displayed on the serial monitor

Result and observation

  • The sensor detects objects by emitting a short ultrasonic burst and then listening for the echo at the trigger pulse the sensor emits a short 40Khz burst and this travels through the air, hits an object, and then bounces back to the sensor
  • Measured the distance of an object using an ultrasonic sensor and also smoothened the sensor data using a moving average filter.
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.