Remarks

HC-SR04
Status Not working
Source code GitHub
NuGet package Not published

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:

Sample projects available on GitHub

Wiring Example

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

Syntax

public class Hcsr04 : IRangeFinder

Constructors

Hcsr04(IDigitalOutputPort, IDigitalInputPort)

Create a new HCSR04 object

Declaration
public Hcsr04(IDigitalOutputPort triggerPort, IDigitalInputPort echoPort)

Parameters

Type Name Description
IDigitalOutputPort triggerPort
IDigitalInputPort echoPort

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

echoPort

Echo Pin.

Declaration
protected IDigitalInputPort echoPort

Field Value

Type Description
IDigitalInputPort

tickStart

Declaration
protected long tickStart

Field Value

Type Description
System.Int64

triggerPort

Trigger Pin.

Declaration
protected IDigitalOutputPort triggerPort

Field Value

Type Description
IDigitalOutputPort

Properties

Conditions

Declaration
public DistanceConditions Conditions { get; }

Property Value

Type Description
DistanceConditions

CurrentDistance

Returns current distance detected in cm.

Declaration
public float CurrentDistance { get; }

Property Value

Type Description
System.Single

MaximumDistance

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

Declaration
public float MaximumDistance { get; }

Property Value

Type Description
System.Single

MinimumDistance

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

Declaration
public float MinimumDistance { get; }

Property Value

Type Description
System.Single

Methods

MeasureDistance()

Sends a trigger signal

Declaration
public void MeasureDistance()

Subscribe(IObserver<DistanceConditionChangeResult>)

Declaration
public IDisposable Subscribe(IObserver<DistanceConditionChangeResult> observer)

Parameters

Type Name Description
System.IObserver<DistanceConditionChangeResult> observer

Returns

Type Description
System.IDisposable

Events

DistanceDetected

Raised when an received a rebound trigger signal

Declaration
public event EventHandler<DistanceEventArgs> DistanceDetected

Event Type

Type Description
System.EventHandler<DistanceEventArgs>

Updated

Declaration
public event EventHandler<DistanceConditionChangeResult> Updated

Event Type

Type Description
System.EventHandler<DistanceConditionChangeResult>