Click here for a free subscription to "HDE" our site newsletter.

Arduino Distance Sensor How to Build

First  2   Last   Next >>

Get part 2 of the Arduino Distance Sensor Project

How to build an Arduino Distance Sensor

Arduino Uno and a 16 by 2 LCD display shield

Contactless distance finders or proximity detectors are used extensively for applications such as robotics, parking sensors, hand driers and motion detectors.

Ultrasonic sensors are an inexpensive solution to the problem but making sense of the data requires a little help from a microcontroller.

When I first hooked up an HC-SR04 ultrasonic echo module to my Arduino Uno board I thought that it would be easy to program the system to deliver a nice solid distance reading. This is what all the simple examples would have you believe after all. In practice things are not quite as straightforward.

The theory of operation is simple. Just provide a short trigger pulse to initiate the 40kHz sound pulse and measure the time for the echo signal to return. Distance is proportional to the echo time. What could go wrong?

Well sometimes the sound echo isn't detected and the pulse on the echo pin stays high for longer than it should. More often than not there are objects in front of the sensor at different distances. This results in the sensor flipping between two or more readings making it difficult to see what is going on.

The Arduino is a powerful and inexpensive computer that you can use to control and analyze signals from the sensor to provide the results that you want.

The purpose of this project is to demonstrate how you can turn a simple ultrasonic sensor into a sophisticated object detector using a clever software algorithm coded in a short Arduino sketch.

Hardware

The great thing about this project is that you only need four inexpensive components:

  1. Arduino Uno board
  2. 16 x 2 Kbd/LCD shield
  3. HC-SR04 Ultrasonic module
  4. Mini Breadboard

Add a few wires and you are good to go.

You may need to do a minimal amount of soldering to attach the connecting wires to the LCD module but that's it. All the action happens in software which is just how I like it.

Wiring between Arduino Uno and LCD/Kbd shield

Connection Diagram

Notice that the pins on the LCD/KBD module are mapped differently to the Arduino.

LCD/Kbd shield layout diagram
HC-SR04 ultrasonic sensor breadboard wiring

You can solder your HC-SR04 onto a small copper strip but I used a small mini solderless breadboard. I may make this more permanent at some point in the future with a small piece of copper strip board.

At some point I will be thinking about putting this whole project into a nice convenient box but right now I haven't finished adding features to it.

I want to add more display modes and ultimately provide some motion detection software so that it can be used to switch on lights, alert you to people at the door or even trigger a water gun to stop cats pooing in the yard.

I shall also be adding support for the keypad on the LCD/Keypad shield. This will allow you to change between display modes, set parameters like range and sensitivity and even change the whole operating mode of the device.

As you can see. I don't have time to put it into an enclosure yet.

LCD/Kbd shild wiring

You can either solder the connecting wires directly onto the connections on the LCD board or you can solder some connector strips onto the board like I have.

I should have used some Arduino stackable headers for this job but I didn't have any to hand. What I did have was some dual inline IC sockets so I got out my craft knife and sliced one row off the connector. It works just as well.

The LCD/Keypad shield stacks nicely onto the Arduino and I used 4 connecting hook up wires to connect between the headers on the shield to the mini breadboard with the ultrasonic sensor on it. It's all a bit flimsy but it's manageable and it's not difficult to get a working system together. It will have to do until I can get the case sorted out.

if anyone is interested in finding a suitable enclosure and would like to write up the assembly details I would be happy to consider publishing them here for everyone to see.


First  2   Last   Next >>


Now subscribe to our newsletter and don't miss a thing
 



Comments (5)

Topic: Arduino Distance Sensor
Full StarFull StarFull StarFull StarFull Star 5/5 (4)
Facebookdel.icio.usStumbleUponDiggGoogle+Twitter
Full StarFull StarFull StarFull StarFull Star
Tony (US) says...
One quick question. How does the LCD know what to do? How do you make that connection from the Arduino to the LCD? That's the part I'm having trouble with, I can't figure out what pins from the Arduino that I need to connect to the LCD then the LCD to the Ultrasonic sensor
16th April 2018 4:47am
Full StarFull StarFull StarFull StarFull Star
Pavan kumar (India) says...
HI,
I was wondering if the same code can be used with a normal 16X2 lcd which comes without the keypad shield. What are the changes I need to do in order to make the same project run on a normal 16X2 display?
Thanks
19th August 2017 9:00am
Full StarFull StarFull StarFull StarFull Star
MrMaint (US) says...
Very nice presentation. I think there are a couple of areas where you could simplify your code without changing it's function. Your 50us pulse is going to be high then low, then high, then low every 50us. Your call to your routine is nice, but the routine could be simplified... void trigger_pulse() { static volatile bool state=0; state=1-state;digitalWrite(trigPin,state); } And your routine for weighting the hits... for(i=0,i<RANGES,i++) { if(i!=found_hit) { ... Read More
23rd November 2016 7:29pm
Full StarFull StarFull StarFull StarFull Star
Bob Jones (US) says...
This code was just what I was looking for and then some. I am building some robot classes and needed logic like this to handle my obstacle sensor.

The inclusion of the LCD code got me over another hump as well.

I will be "cleaning up" this example to encapsulate its functionality a bit better and then incorporating it into my robot code. I will post what I have once it's working.
27th January 2016 1:41am
Steve (UK) says...
Hi Bob,

I'm pleased the article has helped. I would be very interested to see how your project turns out.

Steve
31st January 2016 1:36pm

Add Comment

* Required information
(will not be published)
 
Bold Italic Underline Strike Superscript Subscript Code PHP Code Quote Insert line Bullet list Numeric list Link Email Image Video
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form details on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
Click here for a free subscription to "HDE" our site newsletter.