Characteristic Locus
Inheritance System.Object > HCSR04
Namespace Meadow.Foundation.Sensors.Distance
Assembly HCSR04.dll

Syntax

public class HCSR04 : IRangeFinder

Remarks

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object (like bats). It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package.

Purchasing

The HC-SR04 sensor is available from Sparkfun:

Examples

The following application creates a HC-SR04 object, invokes MeasureDistanceSensor every second and attaches interrupt handlers to the DistanceDetected event that its triggered when the sensor picks up a rebound signal:

using System.Threading;
using Meadow;
using Meadow.Foundation.Sensors.Distance;

namespace HCSR04_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

    public class App : AppBase<F7Micro, App>
    {
        public App ()
        {
            var  _HCSR04 = new HCSR04(Device.Pins.D14, Device.Pins.D13);
            _HCSR04.DistanceDetected += OnDistanceDetected;

            while (true)
            {
                // Send a echo
                _HCSR04.MeasureDistance();
                Thread.Sleep(500);
            }
        }

        // Fired when detecting an obstacle
        private static void OnDistanceDetected(object sender, DistanceEventArgs e) 
        {
            Debug.Print(e.Distance.ToString());
        }
    }
}
Example Circuit

Constructors

View Source

HCSR04(IDigitalOutputPort, IDigitalInputPort)

Create a new HCSR04 object

Declaration
public HCSR04(IDigitalOutputPort triggerPort, IDigitalInputPort echoPort)

Parameters

Type Name Description
IDigitalOutputPort triggerPort
IDigitalInputPort echoPort
View Source

HCSR04(IIODevice, IPin, IPin)

Create a new HCSR04 object with an IO Device

Declaration
public HCSR04(IIODevice device, IPin triggerPin, IPin echoPin)

Parameters

Type Name Description
IIODevice device
IPin triggerPin
IPin echoPin

Fields

View Source

echoPort

Echo Pin.

Declaration
protected IDigitalInputPort echoPort

Field Value

Type Description
IDigitalInputPort
View Source

tickStart

Declaration
protected long tickStart

Field Value

Type Description
System.Int64
View Source

triggerPort

Trigger Pin.

Declaration
protected IDigitalOutputPort triggerPort

Field Value

Type Description
IDigitalOutputPort

Properties

View Source

CurrentDistance

Returns current distance detected in cm.

Declaration
public float CurrentDistance { get; }

Property Value

Type Description
System.Single
View Source

MaximumDistance

Maximum valid distance in cm (CurrentDistance returns -1 if above).

Declaration
public float MaximumDistance { get; }

Property Value

Type Description
System.Single
View Source

MinimumDistance

Minimum valid distance in cm (CurrentDistance returns -1 if below).

Declaration
public float MinimumDistance { get; }

Property Value

Type Description
System.Single

Methods

View Source

MeasureDistance()

Sends a trigger signal

Declaration
public void MeasureDistance()

Events

View Source

DistanceDetected

Raised when an received a rebound trigger signal

Declaration
public event EventHandler<DistanceEventArgs> DistanceDetected

Event Type

Type Description
System.EventHandler<DistanceEventArgs>