Remarks

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

The HY-SRF05 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.

Sample projects available on GitHub

Code Example

Hysrf05 hYSRF05;

public override Task Initialize()
{
    hYSRF05 = new Hysrf05(
        device: Device, 
        triggerPin: Device.Pins.D05, 
        echoPin: Device.Pins.D06);
    hYSRF05.DistanceUpdated += HYSRF05_DistanceUpdated;

    return Task.CompletedTask;
}

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

    return Task.CompletedTask;
}

private void HYSRF05_DistanceUpdated(object sender, IChangeResult<Meadow.Units.Length> e)
{
    Console.WriteLine($"Distance is {e.New.Centimeters}cm");
}

Sample project(s) available on GitHub

Wiring Example

The HY-SRF05 sensor has 5 pins, but you’ll only connect 4: power, ground, echo and trigger (out isn’t used).

Characteristic Locus
Inheritance System.Object ObservableBase<Length> SensorBase<Length> > Hysrf05
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 Hysrf05.dll

Syntax

public class Hysrf05 : SensorBase<Length>, IRangeFinder

Constructors

Hysrf05(IDigitalInputOutputController, IPin, IPin)

Create a new HYSRF05 object with a IO Device HSSRF05 must be running the default 4/5 pin mode 3 pin mode is not supported on Meadow

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

Parameters

Type Name Description
IDigitalInputOutputController device
IPin triggerPin
IPin echoPin

Hysrf05(IDigitalOutputPort, IDigitalInputPort)

Create a new HYSRF05 object and hook up the interrupt handler HSSRF05 must be running the default 4/5 pin mode 3 pin mode is not supported on Meadow

Declaration
public Hysrf05(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; }

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