I'm trying to read the distance from an ultrasonic sensor (HC-SR04) but the only values I get is 0 and 265.xx.
I am using an Raspberry Pi 2 with Windows 10 IoT Core installed.
I've written the code in C#.
This is the ultrasonic sensor class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;
namespace RaspberryPi
{
class UcSensor
{
GpioController gpio = GpioController.GetDefault();
GpioPin TriggerPin;
GpioPin EchoPin;
//Contructor
public UcSensor(int TriggerPin, int EchoPin)
{
//Setting up gpio pin's
this.TriggerPin = gpio.OpenPin(TriggerPin);
this.EchoPin = gpio.OpenPin(EchoPin);
this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);
this.TriggerPin.Write(GpioPinValue.Low);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
//Send pulse
this.TriggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.TriggerPin.Write(GpioPinValue.Low);
//Recieve pusle
while (this.EchoPin.Read() == GpioPinValue.Low)
{
}
DateTime start = DateTime.Now;
while (this.EchoPin.Read() == GpioPinValue.High)
{
}
DateTime stop = DateTime.Now;
//Calculating distance
double timeBetween = (stop - start).TotalSeconds;
double distance = timeBetween * 17000;
return distance;
}
}
}
I've also written a script in python to read the values from the ultrasonic sensor and then it works but in c# I can't get it working.
At the bottom you can find the debug log:
'BACKGROUNDTASKHOST.EXE' (CoreCLR: DefaultDomain): Loaded 'C:\Program Files\WindowsApps\Microsoft.NET.CoreRuntime.1.0_1.0.22816.1_arm__8wekyb3d8bbwe\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\RaspiCar.winmd'. Symbols loaded. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\WinMetadata\Windows.winmd'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.InteropServices.WindowsRuntime.dll'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Threading.dll'. Module was built without symbols. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): Loaded 'C:\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RaspiCarVS.Debug_ARM.chris\System.Runtime.WindowsRuntime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Distance: 265.7457 Distance: 0 Distance: 0 Distance: 0 The program '[2508] BACKGROUNDTASKHOST.EXE' has exited with code 0 (0x0).
Thanks for the reactions. DateTime was the problem i've now used the stopwatch class and now it works. Thanks a lot!
The working class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Windows.Devices.Gpio;
namespace RaspberryPi
{
class UcSensor
{
GpioController gpio = GpioController.GetDefault();
GpioPin TriggerPin;
GpioPin EchoPin;
public UcSensor(int TriggerPin, int EchoPin)
{
this.TriggerPin = gpio.OpenPin(TriggerPin);
this.EchoPin = gpio.OpenPin(EchoPin);
this.TriggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.EchoPin.SetDriveMode(GpioPinDriveMode.Input);
this.TriggerPin.Write(GpioPinValue.Low);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
Stopwatch pulseLength = new Stopwatch();
//Send pulse
this.TriggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.TriggerPin.Write(GpioPinValue.Low);
//Recieve pusle
while (this.EchoPin.Read() == GpioPinValue.Low)
{
}
pulseLength.Start();
while (this.EchoPin.Read() == GpioPinValue.High)
{
}
pulseLength.Stop();
//Calculating distance
TimeSpan timeBetween = pulseLength.Elapsed;
Debug.WriteLine(timeBetween.ToString());
double distance = timeBetween.TotalSeconds * 17000;
return distance;
}
}
}
There is a better solution as the currently proposed answer will occasionally lock while getting the distance. The improved version of the code, which times out after 100 milliseconds (hardcoded). You can return a null or 0.0. Use double? if you want to return null.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Gpio;
namespace MTP.IoT.Devices.Sensors
{
public class HCSR04
{
private GpioPin triggerPin { get; set; }
private GpioPin echoPin { get; set; }
private Stopwatch timeWatcher;
public HCSR04(int triggerPin, int echoPin)
{
GpioController controller = GpioController.GetDefault();
timeWatcher = new Stopwatch();
//initialize trigger pin.
this.triggerPin = controller.OpenPin(triggerPin);
this.triggerPin.SetDriveMode(GpioPinDriveMode.Output);
this.triggerPin.Write(GpioPinValue.Low);
//initialize echo pin.
this.echoPin = controller.OpenPin(echoPin);
this.echoPin.SetDriveMode(GpioPinDriveMode.Input);
}
public double GetDistance()
{
ManualResetEvent mre = new ManualResetEvent(false);
mre.WaitOne(500);
timeWatcher.Reset();
//Send pulse
this.triggerPin.Write(GpioPinValue.High);
mre.WaitOne(TimeSpan.FromMilliseconds(0.01));
this.triggerPin.Write(GpioPinValue.Low);
return this.PulseIn(echoPin, GpioPinValue.High);
}
private double PulseIn(GpioPin echoPin, GpioPinValue value)
{
var t = Task.Run(() =>
{
//Recieve pusle
while (this.echoPin.Read() != value)
{
}
timeWatcher.Start();
while (this.echoPin.Read() == value)
{
}
timeWatcher.Stop();
//Calculating distance
double distance = timeWatcher.Elapsed.TotalSeconds * 17000;
return distance;
});
bool didComplete = t.Wait(TimeSpan.FromMilliseconds(100));
if(didComplete)
{
return t.Result;
}
else
{
return 0.0;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With