HC-SR04 Range Finder Unit

This library unit implements a function to return the distance (range) detected by the HC-SR04 device.

Compilation Constants

Five compilation constants should be set in your main program. For  example:

{$DEFINE PING_PORT 'PortA'} // ping port
{$DEFINE ECHO_PORT 'PortA'} // echo port
{$DEFINE PING_PIN 0}        // ping pin
{$DEFINE ECHO_PIN 1}        // echo pin
{$DEFINE UNITS_cm}          // measurement units

For the latter, you may choose from:

UNITS_cm  -- centimeters
UNITS_m   -- meters
UNITS_in  -- inches
UNITS_ft  -- feet

If no unit is specified, then the measurements default to inches.


There's a single command. (The setup and initialization of the port pins are taken care of automatically).

Function readDistance : word;

The function simply returns the distance in the unit of measurement you selected.

Add It to Your Library Collection

You can get the HC-SR04 range finder library unit "SR04.pas" by clicking the following link:

Be sure to read over the source code for additional details on how to use it.

Try It Out

If you'd like to see it in operation, then head over to the exercises to find the range finding exercise. Click here to go to it.

No comments:

Post a Comment