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