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

Arduino Distance Sensor Part 2

First  2   Last   Next >>

How to build an Arduino Distance Sensor Part 2

Click to get the full project details by subscribing free to HDE Magazine and never miss a project.

Arduino Uno and a 16 by 2 LCD display shield

In part 1 of the Arduino Distance Sensor Project we wired up the three components of the distance sensor and loaded a simple Arduino sketch to produce a display on the LCD.

This time I'm going to show you how you can add more display modes and switch between them using one of the keys on the keypad.

If you haven't yet built the hardware then follow the instructions in part 1 and load the new sketch that you will find later in this article.


Once you have downloaded and run the new sketch you can switch between the three available display modes by clicking the SELECT key on the keypad.

That's all there is to it and once you understand how the code works you should be able to add modes of your own.

Distance sensor mode 0 display Mode 0: The histogram style display from the original sketch on the top line with the distance in centimetres displayed on the bottom.

Distance sensor mode 1 display Mode 1: This is the original display mode from part 1 of the project.

Distance sensor mode 2 display Mode 1: A single horizontal bar to graphically represent distance to the object with the value on the bottom line.


This is where the magic happens...

You might find it helpful to print the sketch from here to help you follow along with this description.

The code sketch contains a lot of comments that I hope will be of use to you when figuring out how it works.

In an effort to keep this explanation as simple as possible I'm only going to describe the parts of the code that have changed since the last revision described in part 1.

The Arduino Distance Sensor project is upgraded with motion detection capability in the latest edition of Home DIY Electronics Magazine so register and claim your copy now.

Starting at the top of the sketch the first things that has been added are some definitions of constants that are used to signal which button has been pressed. These are constants that begin with 'btn' like 'btnRIGHT', 'btnUP' etc.

These constants are each assigned a value and it means that we can use a meaningful name in the code instead of the number itself.

Now that we have more than one display mode we need a variable to keep track of the mode. The variable 'display_mode' is declared and initialized to 0.

Special characters

Last time we defined the special mini bargraph characters as arrays of bytes. There was a character with just the bottom row lit with pixels and seven more characters each with one more row populated than the previous character.

The new software defines six more characters that are used to form the tip of the horizontal bargraph. This time progressive columns of pixels are populated.

The LCD module only allows up to eight special characters to be loaded at any one time. This means that we can't use both sets of characters at the same time. We can swap them in and out on the fly though.

Provided we don't try to display both sets of characters on the display simultaneously we can load them up as we need them.

First  2   Last   Next >>

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

Comments (1)

Topic: Arduino Distance Sensor Part 2
Full StarFull StarFull StarEmpty StarEmpty Star 3/5 (1)
Full StarFull StarFull StarEmpty StarEmpty Star
minoo ekrani (Iran) says...
hi, i just want to measure the distance no echos can u plz get me a code?
9th November 2014 12:03pm

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
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.