Challenge

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

Answer:

The SEN136B5B is an ultrasonic range finder from Seeedstudio. It detects the distance of the closest object in front of the sensor (from 3 cm up to 400 cm). It works by sending out a burst of ultrasound and listening for the echo when it bounces off of an object. It pings the obstacles with ultrasound. The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the same pin using the 'pulseIn()' function. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this time can be converted to distance. The 5V pin of the SEN136B5B is connected to the 5V pin on the board, the GND pin is connected to the GND pin, and the SIG (signal) pin is connected to digital pin 7 on the board.

While coding the microcontroller, in the setup fuction, I initialized the UART serial communication with the builtin UART monitor using 'Serial.begin()'.

In the loop function, first I declared three variables named, 'duration', 'inches' and 'cm' of datatype long integer. First, in order to transmit a pulse towards the object, I declared pin 7 as an output pin and transmitted a pulse by making pin 7 LOW for 2 microseconds followed by making pin 7 HIGH for 5 microseconds. Then I declared the same pin, pin7 as an input pin and read the incoming data from the ultrasonic range finder using the 'pulseIn()' function. The time duration for the echo was stored in the long variable named 'duration'. Then I called two functions named, 'microsecondsToInches()' and 'microsecondsToCentimeters()' respectively to convert the time duration into inches and centimeters. The 'microsecondsToInches()' function divides the duration vallue by 73.746 and then by 2 to get the distance between the ultrasonic transmitter and the object in inches. The 'microsecondsToCentimeters()' function divides the duration value by 29 and then by 2 to get the value of the distance between the ultrasonic transmitter and the object in centimeters.

After converting to inches and centimeters, I smoothened the sensor data using two moving average filters one each for inches and centimeters.

Sensors and microcontrollers allow us to turn real-life phenomena into simple numerical signals that we can learn from. However, the raw output from the sensor may not be sufficient to extract desired information from. Real hardware is susceptible to noise and other interference from the environment.

The moving average filter is a simple technique that makers can use to smooth out their signal, removing noise and making it easier to learn from the sensor output.

A moving average filter is a basic technique that can be used to remove noise (random interference) from a signal. It is a simplified form of a low-pass filter.

Running a signal through this filter will remove higher frequency information from the output. While a traditional low pass filter can be efficiently used to focus on a desired signal frequency, the moving average filter is a more direct approach to simply “smoothing out” a signal.

The idea is simple: the moving average filter takes the average of the last “M” amount of entries in the signal and averages them to produce the output.

The only real parameter that can be controlled in the moving average filter is the window size, which is the amount of previous signal entries that can be averaged together. If the window is too small, the signal may still end up being very noisy. However, if the window is too large, critical information in the signal may be lost. Choosing the right window size is a matter of trial and error.

The code keeps track of the sum of the five most recent readings in a variable called SUM. The five most recent readings themselves are in an array called READINGS, so that the oldest one can be accessed and removed from the sum when a new one arrives.

When a new reading arrives, it is added to the sum and the index (position in the array) is increased by one. The modulus operator (%) is used to wrap the index back to zero if the index exceeds the window size, since the modulus operator returns the integer remainder of one number divided by another.

Notice that as a result of the calculation, the filtered signal lags slightly behind the raw input signal. If the window size is too large, this effect can become noticeable. The filtered signal will lag far behind the raw signal, and too much information will be lost from the signal. Since in this code, I have used the window size of '5', it usually takes 5 readings for the filtered signal to match the real value of the sensor data returned from the object.

After the moving average filter has been implemented, I printed both the raw data value of inches and cms as well as the filtered (smoothened) value of inches and centimeters in the UART monitor.

I will attach a screenshot of the output for reference. The circuit can be tested on Tinkecad by clicking on the following link:

Link: https://www.tinkercad.com/things/3giKymLxATe-ultrasonic-distance-sensor/editel?sharecode=2Zzmrm7Kx6OzqYF5dXJCH3NWzP5ccwAntvSY6ZFUhbU

================================END=================================

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.