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

Syntax

public class HYSRF05 : IRangeFinder

Remarks

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.

Purchasing

The HY-SRF05 sensor is available in ebay:

Examples

The following application creates a HY-SRF05 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 HYSRF05_Sample
{
    public class Program
    {
        static IApp _app; 
        public static void Main()
        {
            _app = new App();
        }
    }

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

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

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

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

Constructors

View Source

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

HYSRF05(IIODevice, 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(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>