Skip to main content

Meadow.Foundation.Sensors.Distance.Hcsr04

Hcsr04
StatusStatus badge: in-progress
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet 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

Wiring Example

Class Hcsr04

HCSR04 Distance Sensor - driver not complete

Assembly: Hcsr04.dll
View Source
Declaration
public class Hcsr04 : SamplingSensorBase<Length>, IObservable<IChangeResult<Length>>, IRangeFinder, ISamplingSensor<Length>, ISensor<Length>, ISensor, ISamplingSensor, IDisposable

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Derived:
Meadow.Foundation.ICs.IOExpanders.SerialWombatBase.Hcsr04

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.Length>>, Meadow.Peripherals.Sensors.Distance.IRangeFinder, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Length>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Length>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, System.IDisposable

Properties

Distance

Returns current distance

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

MinimumDistance

Minimum valid distance in cm

View Source
Declaration
public double MinimumDistance { get; }

MaximumDistance

Maximum valid distance in cm

View Source
Declaration
public double MaximumDistance { get; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Fields

triggerPort

Port for trigger Pin

View Source
Declaration
protected IDigitalOutputPort? triggerPort

echoPort

Port for echo Pin

View Source
Declaration
protected IDigitalInterruptPort? echoPort

Methods

MeasureDistance()

Sends a trigger signal

View Source
Declaration
public virtual void MeasureDistance()

ReadSensor()

Reads data from the sensor

View Source
Declaration
protected override Task<Length> ReadSensor()
Returns

System.Threading.Tasks.Task<Meadow.Units.Length>: The latest sensor reading### StartUpdating(TimeSpan?) Starts continuously sampling the sensor

View Source
Declaration
public override void StartUpdating(TimeSpan? updateInterval = null)
Parameters
TypeName
System.Nullable<System.TimeSpan>updateInterval

StopUpdating()

Stops sampling the sensor

View Source
Declaration
public override void StopUpdating()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Units.Length>>
  • Meadow.Peripherals.Sensors.Distance.IRangeFinder
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Length>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Length>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • System.IDisposable