Ultrasonic raspberry pickup pi 2 C # .net

I am trying to read the distance from the ultrasonic sensor (HC-SR04), but the only values ​​I get are 0 and 265.xx.

I am using Raspberry Pi 2 with IoT Core Windows 10 installed.

I wrote the code in C #.

This is the class of ultrasonic sensor:

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 also wrote a script in python to read values ​​from an ultrasonic sensor, and then it works, but in C # I can't get it to work.

Below 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'. Missing download symbols. The module is optimized and the debugger option "Only 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'. Missing download symbols. The module is optimized and the debugger option "Only my code" is enabled. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): loaded 'C: \ Users \ DefaultAccount \ AppData \ Local \ DevelopmentFiles \ RaspiCarVS.Debug_ARM.chris \ WinMetadata \ Windows.winmd'. The module was built without characters. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): loaded 'C: \ Users \ DefaultAccount \ AppData \ Local \ DevelopmentFiles \ RaspiCarVS.Debug_ARM.chris \ System.Runtime.InteropServices.WindowsRuntime.dll'. The module was built without characters. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): loaded 'C: \ Users \ DefaultAccount \ AppData \ Local \ DevelopmentFiles \ RaspiCarVS.Debug_ARM.chris \ System.Threading.dll'. The module was built without characters. 'BACKGROUNDTASKHOST.EXE' (CoreCLR: CoreCLR_UAP_Domain): loaded 'C: \ Users \ DefaultAccount \ AppData \ Local \ DevelopmentFiles \ RaspiCarVS.Debug_ARM.chris \ System.Diagnostics.Debug.dll'. Missing download symbols. The module is optimized and the debugger option "Only 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'. Missing download symbols. The module is optimized and the debugger option "Only my code" is enabled. Distance: 265.7457 Distance: 0 Distance: 0 Distance: 0 The program '[2508] BACKGROUNDTASKHOST.EXE' exited with the code 0 (0x0).

+7
c # windows-10 sensor raspberry-pi2 iot
source share
2 answers

Thanks for the response. The DateTime problem was that now I used the stopwatch class and now it works. Many thanks!

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; } } } 
+6
source share

There is a better solution, as the currently proposed answer will be blocked when receiving the distance. An improved version of the code that expires after 100 milliseconds (hard-coded). You can return 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; } } } } 
+1
source share

All Articles