张心花怒放的,超声波测距模块

  HC-S帕杰罗04 接纳 IO 触发测距。上面介绍一下其在 Windows 10 IoT Core
环境下的用法。

张心潮澎湃的 Windows 10 IoT 开发笔记:HC-SLAND04 超声波测距模块,iothc-sr04

  HC-SPRADO04 采纳 IO 触发测距。下边介绍一下其在 Windows 10 IoT Core
环境下的用法。

  项目运作在 Raspberry Pi 2/3 上,使用 C# 举办编码。

 

  1. 准备

  HC-SR04 ×1 

  Raspberry Pi 2/3 ×1

  公母头杜邦线 ×4

 

  2. 连线

Vcc – 5V

Gnd – GND

Trig – GPIO 17 – Pin 11

Echo – GPIO 27 – Pin 13

图片 1

  

  3. 代码

  GitHub
https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/HC\_SR04Demo

  你供给在档次中添加3个 C# 代码文件
HCS瑞虎04.cs,将上面包车型客车代码复制粘贴,并且不要遗忘添加引用 Windows IoT
Extensions for the UWP

using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Devices.Gpio;

namespace HC_SR04Demo
{
    class HCSR04
    {
        private int sensorTrig;
        private int sensorEcho;

        private GpioPin pinTrig;
        private GpioPin pinEcho;

        Stopwatch time = new Stopwatch();

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="trig">Trig Pin</param>
        /// <param name="echo">Echo Pin</param>
        public HCSR04(int trig, int echo)
        {
            sensorTrig = trig;
            sensorEcho = echo;
        }

        /// <summary>
        /// Initialize the sensor
        /// </summary>
        public void Initialize()
        {
            var gpio = GpioController.GetDefault();

            pinTrig = gpio.OpenPin(sensorTrig);
            pinEcho = gpio.OpenPin(sensorEcho);

            pinTrig.SetDriveMode(GpioPinDriveMode.Output);
            pinEcho.SetDriveMode(GpioPinDriveMode.Input);

            pinTrig.Write(GpioPinValue.Low);
        }

        /// <summary>
        /// Read data from the sensor
        /// </summary>
        /// <returns>A double type distance data</returns>
        public async Task<double> ReadAsync()
        {
            double result;

            pinTrig.Write(GpioPinValue.High);
            await Task.Delay(10);
            pinTrig.Write(GpioPinValue.Low);

            while (pinEcho.Read() == GpioPinValue.Low)
            {

            }
            time.Restart();
            while (pinEcho.Read() == GpioPinValue.High)
            {

            }
            time.Stop();

            result = (time.Elapsed.TotalSeconds * 34000) / 2;

            return result;
        }

        /// <summary>
        /// Cleanup
        /// </summary>
        public void Dispose()
        {
            pinTrig.Dispose();
            pinEcho.Dispose();
        }
    }
}

 

  4. 哪些利用

  第二步调用构造函数将 HCSSportage04 实例化,请传入 Trig 和 Echo 的连接值

  第壹步调用 Initialize() 初叶化设备

  第一步调用 ReadAsync() 读取数据,重返的是3个 double 类型的值

  当须要关闭设备时,调用 Dispose() 

 

  详见 GitHub

http://www.bkjia.com/C\_jc/1189689.htmlwww.bkjia.comtruehttp://www.bkjia.com/C\_jc/1189689.htmlTechArticle张高兴的 Windows 10 IoT 开发笔记:HC-SMurano04
超声波测距模块,iothc-sr04 HC-S兰德酷路泽04选取IO触发测距。上面介绍一下其在
Windows 10 IoT Core 环境下的用法。…

  项目运维在 Raspberry Pi 2/3 上,使用 C# 进行编码。

 

  1. 准备

  HC-SR04 ×1 

  Raspberry Pi 2/3 ×1

  公母头杜邦线 ×4

 

  2. 连线

Vcc – 5V

Gnd – GND

Trig – GPIO 17 – Pin 11

Echo – GPIO 27 – Pin 13

图片 2

  

  3. 代码

  GitHub
https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/HC_SR04Demo

  你须求在类型中添加一个 C# 代码文件
HCS奥迪Q704.cs,将上边包车型客车代码复制粘贴,并且不要遗忘添加引用 Windows IoT
Extensions for the UWP

using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Devices.Gpio;

namespace HC_SR04Demo
{
    class HCSR04
    {
        private int sensorTrig;
        private int sensorEcho;

        private GpioPin pinTrig;
        private GpioPin pinEcho;

        Stopwatch time = new Stopwatch();

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="trig">Trig Pin</param>
        /// <param name="echo">Echo Pin</param>
        public HCSR04(int trig, int echo)
        {
            sensorTrig = trig;
            sensorEcho = echo;
        }

        /// <summary>
        /// Initialize the sensor
        /// </summary>
        public void Initialize()
        {
            var gpio = GpioController.GetDefault();

            pinTrig = gpio.OpenPin(sensorTrig);
            pinEcho = gpio.OpenPin(sensorEcho);

            pinTrig.SetDriveMode(GpioPinDriveMode.Output);
            pinEcho.SetDriveMode(GpioPinDriveMode.Input);

            pinTrig.Write(GpioPinValue.Low);
        }

        /// <summary>
        /// Read data from the sensor
        /// </summary>
        /// <returns>A double type distance data</returns>
        public async Task<double> ReadAsync()
        {
            double result;

            pinTrig.Write(GpioPinValue.High);
            await Task.Delay(10);
            pinTrig.Write(GpioPinValue.Low);

            while (pinEcho.Read() == GpioPinValue.Low)
            {

            }
            time.Restart();
            while (pinEcho.Read() == GpioPinValue.High)
            {

            }
            time.Stop();

            result = (time.Elapsed.TotalSeconds * 34000) / 2;

            return result;
        }

        /// <summary>
        /// Cleanup
        /// </summary>
        public void Dispose()
        {
            pinTrig.Dispose();
            pinEcho.Dispose();
        }
    }
}

 

  4. 哪些利用

  第3步调用构造函数将 HCS奥迪Q304 实例化,请传入 Trig 和 Echo 的连接值

  第三步调用 Initialize() 开端化设备

  第3步调用 ReadAsync() 读取数据,重回的是八个 double 类型的值

  当需求关闭设备时,调用 Dispose() 

 

  详见 GitHub