#7 Developing An Ultrasonic Depth Sensor Gauge In Arduino

This post was a result of the skills I obtained from the Arduino Beginner’s Course by Moscow Institute Of Technology on Coursera.


PREMISE

During my tinkering around with the new Arduino kit that I bought online, I found myself mesmerized by the many possibilities it presented and how many cool projects I could work out with it.

Of course, coding in C nor Arduino development were my strong suites so I probed around the internet for a bit before settling on the aforementioned Coursera course. (the parts and projects mentioned in its description matched vehemently with the ones that came with my kit)

Anyways, I did ended up developing several Arduino projects throughout the ensuing months (some small some big) but the one that obviously caught my attention was the object avoiding self driving car. And although I’m no Tesla fanatic, – I did had my interest in the technology.

The most intriguing was the Ultrasonic Distance sensor, Waving my hands in front of it and seeing the plots and numbers change on the screen in real time was extremely exciting.

I wanted to create a sort of an Ultrasonic distance gauge thing that when I point it to some direction of an object, the direct distance will be displayed on the LCD.

Practically speaking, to use the sensor outside (places where I can’t bring my PC) and still be able to gauge the readings. (I.e a portable version).

Develop a portable ultrasonic distance gauge using Arduino

GOAL

PARTS USED

PROJECT FILES

Ultrasonic Distance Gauge.ino The final version of the Arduino code (tested with Arduino Uno)

GLOSSARY

All the important technical terms in the article are highlighted in bold and are explained here.


UDS: (Abbreviation) Ultrasonic Distance Sensor.

MA: (Abbreviation) Mean Aggregate, The total number of readings taken every cycle. In this case it’s 7. – That is, I took the mean of seven observations.

TRIG: Located on the HSCR04 board, when a high voltage (5v in this case) is sent for 10┬Ás. An 8-cycle sonic burst is generated on the Ultrasonic emitter which travels at the speed of sound and is received on the Echo pin.

ECHO: The echo pin calculates the time it took for the 8-cycle sonic burst generated on the ultrasonic emitter to reach the ultrasonic receiver, the recorded time is sent back to the Arduino board for processing.

For a general understanding of how these sensors work, I recommend checking this image.

Serial: is used for communication between the Arduino board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB.

METHODOLOGY

PART ONE: Libraries Used

PART Two: Initialization

After including all the required libraries , I defined all the pins to which stuff

PART Three: Void Setup

PART Four: Void Loop

PART Five: Additional Functions

A. Measuring distance Algorithm.

I learned how to measure the distance from the UDS in Arduino from the aforementioned Coursera course. It has a few tweaks here and there to increase the stability and precision of the readings. Here’s a rough break down of how it works,

  1. Take a distance measure from echo.
  2. Sum these measurements in a mean aggregate (upto 7).
  3. Update each last term with new reading to keep the loop going.
  4. divide the mean aggregate by number of measurements.

–> return the mean as the true result.

Since the sensor takes measurements every 25ms the distance displayed on the screen is slightly in the past. However, taking the mean of an array of inputs was necessary to even out the measurement and rule out outliers.

B. Object Alert

Object alert is rather easy to explain, If the distance recorded by UDS at any interval is smaller than a min_distance threshold value, it’ll flash the “OBJ DETECTED” message on the LCD and flash an alert yellow LED (Y_LED) in synced cycles.

C. Default State

Default state is a display widget I wrote to act as an intermediary to switch to after the object has left the minimum distance range. This way, the model can easily transition between alarm mode and information mode.

PART Five: Test Run

I suppose the results speaks for themselves. Although I can’t connect a motor shield to this configuration (yet) so I can hardly comment on its accuracy while in motion.

As always, The entire code of the project can be find on my Github Page.

What I’ve Learned

It was fun messing around while making this project, especially after it was done. While the range is rather short (upto about 5 meters) I suppose the logic applied here shall scale consistently over more broad range sensors.

What are the practicality of this weird duo? I don’t know really, I liked the object detection I learned while making collision detecting cars and the retro looks of the 16-bit LCD so much that I straight up married the two. – I guess they could help while traversing a really dark room but at that point why not use a normal flashlight?

My original aim of displaying a live feed to a local LCD was so I can gauge how accurately and with how much accuracy my self-driving prototype halted and turned when met with an obstacle. I could do it with a nearby desktop, But that requires the prototype to be connected to the PC. (which was not always feasible for me)

The objective skills I learned were setting up an 8bit 16×2 LCD on Arduino (both physically as well as writing codes to feed the sensor reading directly to it (live). In the future, I would like to expand on this by experimenting with more stringent signal denoiser algorithms, and a bar gauge instead of a numerical percentage. (helps with readability)

About Me!

An aspiring data scientist with a great interest in machine learning and its applications. I post my work here in the hope to improve over time.