Ultrasonic distance meter

William Heuser

Overview

For my project I created a microcontroller device that is able to measure the distance to an object. It uses ultrasonic sound waves and the echo of these to measure the distance. The ultrasonic module has a range from 2cm to 400cm and an accuracy in the range ±2%. To do a measurement simply point the ultrasonic ranging module towards an object and press the green button. A word of caution: the angle of the object has to be parallel to the microcontroller board and cannot absorb sound as this will not let the echo return.

Front view Top view

Hardware

For this project I used a Microcontroller from Silicon Labs in the series Zero Gecko with an ARM Cortex-M0+ core processor. For generating the ultrasonic signal and receiving it again I used the Ultrasonic Ranging Module HC-SR04 from Adafruit Industries LLC. This module had a transmitter connected at port PC1 to send out an ultrasonic burst, and a receiver connected at port PB7 to detect the echo of the ultrasonic burst. This module had to use 5V to be powered and as the batteries only provide 3V, a DC/DC converter is used to boost it up. An LCD display of EA-DOGS series from Display Visions was used to display the results of the measurements. It can display 4 rows of 10 characters.

Schematic Assembly

Software

For this project when the power from the batteries is turned on from the slider switch on the right side of the microcontroller the hardware such as the LCD, RTC, GPIO and so forth are configured and at last the message "Ready!" is displayed to the screen. Then an infinite while loop is reached, this lets the Microcontroller go right back into a state of waiting for each interrupt has been handled. This practice greatly reduces power consumption. At the end of each iteration the interrupt flags are set to 0 again, thus it will wait until a new interrupt is received before waking up. The interrupts for this device are sent by either the return of the echo or the push of the button.

When the button is pressed a buttonFlag variable is raised which, in turn, wakes up the controller sends out a 10 μSec ultrasonic burst and afterwards lets the controller go back to sleep.

When the ranging module receives the returned echo it raises echoFlag that wakes up the controller, captures the length of the positive age of the ultrasonic ranging module which shows us the time it has taken for the ultrasonic signal to return from hitting an object. The formula v=d/t is used to compute the distance to the object as the velocity of sound and the return time is known. The result is then sent to a circular buffer, which is first-in-first-out. The buffer is then dumped to the screen, with the newest result first and the oldest result last.

Final Thoughts

While doing this project I learned how difficult it can be to use microcontrollers and the C language if you have no prior knowledge of this. But simply by coming to class and trying to get the smaller components that make up the entire program to work the project can be done. One thing I wish I had done was thinking my entire program through before starting and settling on a way to do things. Had I done this I would have been able to add some cooler features to my microcontroller. For example a way to calibrate the speed of sound in the present conditions at startup would have been a nice feature to have. Overall I am happy with my project and the code I have written as I think my ultrasonic distance meter is a cool little gadget.

Downloads


Last modified:Mon, Jan 23, 2023.

00139