Remarks

Hcsr04
Status Status badge: in-progress
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Distance.Hcsr04

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.

Code Example

Hcsr04 hCSR04;

public override Task Initialize()
{
    Resolver.Log.Info($"Hello HC-SR04 sample");

    hCSR04 = new Hcsr04(
        triggerPin: Device.Pins.D05,
        echoPin: Device.Pins.D06);
    hCSR04.DistanceUpdated += HCSR04_DistanceUpdated;

    return Task.CompletedTask;
}

public override Task Run()
{
    while (true)
    {
        // Sends a trigger signal
        hCSR04.MeasureDistance();
        Thread.Sleep(2000);
    }
}

private void HCSR04_DistanceUpdated(object sender, IChangeResult<Meadow.Units.Length> e)
{
    Resolver.Log.Info($"Distance (cm): {e.New.Centimeters}");
}

Sample project(s) available on GitHub

Purchasing

The HC-SR04 sensor is available from Sparkfun:

Sample projects available on GitHub

Wiring Example

Characteristic Locus
Inheritance System.Object ObservableBase<Length> SamplingSensorBase<Length> > Hcsr04 > SerialWombatBase.Hcsr04
Implements ISamplingSensor<Length> IRangeFinder
Inherited Members SamplingSensorBase<Length>.samplingLock SamplingSensorBase<Length>.Updated SamplingSensorBase<Length>.SamplingTokenSource SamplingSensorBase<Length>.Conditions SamplingSensorBase<Length>.IsSampling SamplingSensorBase<Length>.UpdateInterval SamplingSensorBase<Length>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<Length>.Read() ObservableBase<Length>.observers ObservableBase<Length>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Length>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Length>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Distance
Assembly Hcsr04.dll

Syntax

public class Hcsr04 : SamplingSensorBase<Length>, ISamplingSensor<Length>, IRangeFinder

Constructors

Hcsr04()

Create a new HCSR04 object

Declaration
protected Hcsr04()

Hcsr04(IDigitalOutputPort, IDigitalInputPort)

Create a new HCSR04 object

Declaration
public Hcsr04(IDigitalOutputPort triggerPort, IDigitalInputPort echoPort)

Parameters

Type Name Description
IDigitalOutputPort triggerPort

The port for the trigger pin

IDigitalInputPort echoPort

The port for the echo pin

Hcsr04(IPin, IPin)

Create a new HCSR04 object with an IO Device

Declaration
public Hcsr04(IPin triggerPin, IPin echoPin)

Parameters

Type Name Description
IPin triggerPin

The trigger pin

IPin echoPin

The echo pin

Fields

echoPort

Port for echo Pin

Declaration
protected IDigitalInputPort echoPort

Field Value

Type Description
IDigitalInputPort

triggerPort

Port for trigger Pin

Declaration
protected IDigitalOutputPort triggerPort

Field Value

Type Description
IDigitalOutputPort

Properties

Distance

Returns current distance

Declaration
public Length? Distance { get; protected set; }

Property Value

Type Description
System.Nullable<Length>

MaximumDistance

Maximum valid distance in cm

Declaration
public double MaximumDistance { get; }

Property Value

Type Description
System.Double

MinimumDistance

Minimum valid distance in cm

Declaration
public double MinimumDistance { get; }

Property Value

Type Description
System.Double

Methods

MeasureDistance()

Sends a trigger signal

Declaration
public virtual void MeasureDistance()

RaiseEventsAndNotify(IChangeResult<Length>)

Raise events for subcribers and notify of value changes

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)

Parameters

Type Name Description
IChangeResult<Length> changeResult

The updated sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<Length> ReadSensor()

Returns

Type Description
Task<Length>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<Length>.ReadSensor()

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor

Declaration
public override void StartUpdating(TimeSpan? updateInterval = null)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

StopUpdating()

Stops sampling the sensor

Declaration
public override void StopUpdating()

Overrides

Meadow.Foundation.SamplingSensorBase<Length>.StopUpdating()

Events

DistanceUpdated

Raised when an received a rebound trigger signal

Declaration
public event EventHandler<IChangeResult<Length>> DistanceUpdated

Event Type

Type Description
EventHandler<IChangeResult<Length>>