Meadow.Foundation.Sensors.Distance.Hcsr04
Hcsr04 | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
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.Updated += 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
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
public Length? Distance { get; protected set; }
MinimumDistance
Minimum valid distance in cm
View Source
public double MinimumDistance { get; }
MaximumDistance
Maximum valid distance in cm
View Source
public double MaximumDistance { get; }
IsDisposed
Is the object disposed
View Source
public bool IsDisposed { get; }
Fields
triggerPort
Port for trigger Pin
View Source
protected IDigitalOutputPort? triggerPort
echoPort
Port for echo Pin
View Source
protected IDigitalInterruptPort? echoPort
Methods
MeasureDistance()
Sends a trigger signal
View Source
public virtual void MeasureDistance()
ReadSensor()
Reads data from the sensor
View Source
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
public override void StartUpdating(TimeSpan? updateInterval = null)
Parameters
Type | Name |
---|---|
System.Nullable<System.TimeSpan> | updateInterval |
StopUpdating()
Stops sampling the sensor
View Source
public override void StopUpdating()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
View Source
public void Dispose()
Dispose(bool)
Dispose of the object
View Source
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | Is 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