Integrating ultrasonic sensors with Raspberry pi using c#

0 votes

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

Oct 3, 2018 in IoT (Internet of Things) by Matt
• 2,260 points
278 views

1 answer to this question.

0 votes

DateTime is the problem You should use the stopwatch class and it will work.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;
        }

    }
}

answered Oct 3, 2018 by anonymous2
• 4,260 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Refactoring multi thread program on raspberry pi with sensors

Before re-engineering your application, you should narrow ...READ MORE

answered Sep 11, 2018 in IoT (Internet of Things) by Upasana
• 8,530 points
168 views
0 votes
1 answer

Setting time on Raspberry Pi using just PowerShell

The Enter-PSSession is for interactive sessions, so ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
70 views
0 votes
1 answer

Optimal screen resolution for Raspberry Pi with 7" TFT LCD

Hello there! I'm using a 7" TFT ...READ MORE

answered Aug 14, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
88 views
0 votes
1 answer

Sending more than 8 byte through UART on Raspberry Pi using Android Things!

So, you can send 234212441325454543595674859764 in ASCII, ...READ MORE

answered Aug 24, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
129 views
0 votes
1 answer

USB Mobile Broadband Modem on Windows 10 IoT

I was looking for a solution too, ...READ MORE

answered Jul 9, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
420 views
0 votes
1 answer

Autostart published Application on Windows 10 IoT

It can be done by making changes ...READ MORE

answered Jul 12, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
157 views
0 votes
1 answer

Using Sleep()/Delay() in C# on Windows IoT

You'll be glad to know that C# ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
301 views
0 votes
1 answer

Configuring Raspberry Pi with Windows 10 IoT App using Rest API

You should use HttpClient instead of WebClient. Try ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
90 views
0 votes
1 answer