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()
{
    Console.WriteLine($"Hello HC-SR04 sample");

    hCSR04 = new Hcsr04(
        device: Device, 
        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);
    }

    return Task.CompletedTask;
}

private void HCSR04_DistanceUpdated(object sender, IChangeResult<Meadow.Units.Length> e)
{
    Console.WriteLine($"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> SensorBase<Length> > Hcsr04 > SerialWombatBase.Hcsr04
Implements IRangeFinder
Inherited Members SensorBase<Length>.Updated SensorBase<Length>.samplingLock SensorBase<Length>.SamplingTokenSource SensorBase<Length>.Conditions SensorBase<Length>.IsSampling SensorBase<Length>.UpdateInterval SensorBase<Length>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<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 : SensorBase<Length>, IRangeFinder

Constructors

Hcsr04()

Declaration
protected Hcsr04()

Hcsr04(IDigitalInputOutputController, IPin, IPin)

Create a new HCSR04 object with an IO Device

Declaration
public Hcsr04(IDigitalInputOutputController device, IPin triggerPin, IPin echoPin)

Parameters

Type Name Description
IDigitalInputOutputController device
IPin triggerPin
IPin echoPin

Hcsr04(IDigitalOutputPort, IDigitalInputPort)

Create a new HCSR04 object

Declaration
public Hcsr04(IDigitalOutputPort triggerPort, IDigitalInputPort echoPort)

Parameters

Type Name Description
IDigitalOutputPort triggerPort
IDigitalInputPort echoPort

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

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

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

Parameters

Type Name Description
IChangeResult<Length> changeResult

ReadSensor()

Declaration
protected override Task<Length> ReadSensor()

Returns

Type Description
Task<Length>

Overrides

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

Events

DistanceUpdated

Raised when an received a rebound trigger signal

Declaration
public event EventHandler<IChangeResult<Length>> DistanceUpdated

Event Type

Type Description
EventHandler<IChangeResult<Length>>