Package: haas-python-hcsr04
HaaS Python介绍HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见HaaS Python官网,创意案例,硬件积木HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, seeHaaS Python official website,Creative Case,Hardware Building Blocks安装方法在HaaS Python官网下载开发板对应的固件，并完成固件烧录Download the firmware corresponding to the development board atHaaS Python websiteand complete the firmware burning在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在github上向我们提交issue，我们的工程师会及时解答Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us ongithub, Our engineers will answer in timeimport upip
upip.install("haas-python-hcsr04")
import haas-python-hcsr04超声波测距传感器一、产品简介HC-SR04超声波传感器使用声纳来确定物体的距离，其工作原理类似与蝙蝠。测量范围从2厘米到450厘米，HC-SR04超声波传感器上配有超声波发射器和接收器两个模块。超声波测距传感器是数字接口类型，其基本工作过程如下:发射器（触发引脚-Trig），发送信号 一段高频声音当信号遇到一个物体时，它会被反射然后发射器（回声引脚-Echo）接收该信号引脚定义VCC：DC-5VTrig：触发(输入)Echo：回声(输出)GND：地二、技术参数电源：+ 5V DC静态电流：<2mA工作电流：15mA有效角度：<15°测距：2厘米 - 450厘米分辨率：0.3厘米测量角度：30度触发输入脉冲宽度：10uS尺寸：45mm x 20mm x 15mm三、软件接口超声波测距传感器HaaS Python驱动：下载地址HCSR04(trigObj,echoObj) - 创建超声波传感器对象函数原型hcsr04Obj = HCSR04(trigObj,echoObj)参数说明参数类型必选参数？说明trigObjGPIO是GPIO配置输出，触发引脚，调用此函数前需确保trigObj对象已经处于open状态echoObjGPIO是GPIO配置输入，测量引脚，调用此函数前需确保echoObj对象已经处于open状态返回值HCSR04对象成功，返回HCSR04对象；HCSR04对象创建失败，抛出Exception示例代码importhcsr04fromdriverimportGPIOprint("Testing HCSR04 ...")echoDev=GPIO()echoDev.open("echo")trigDev=GPIO()trigDev.open("trig")hcsr04Dev=hcsr04.HCSR04(trigDev,echoDev)输出TestingHCSR04...measureDistance() - 获取超声波测量的距离函数功能：获取超声波测量的距离函数原型：HCSR04.measureDistance()参数说明：无返回值：返回值为cm单位的距离值示例：importhcsr04fromdriverimportGPIOimportutimeprint("Testing HCSR04 ...")echoDev=GPIO()echoDev.open("echo")trigDev=GPIO()trigDev.open("trig")hcsr04Dev=hcsr04.HCSR04(trigDev,echoDev)whileTrue:# 无限循环distance=disDev.measureDistance()print(str(distance)+' CM')utime.sleep(1)# 打印完之后休眠1秒输出TestingHCSR04...34CM492CM四、接口案例此使用实例在board.json中定义了名为IR的GPIO类型的对象。在Python脚本中执行一次获取IR人体红外传感器状态值并打印在日志中。案例代码{"name":"board-name","version":"1.0.0","io":{"trig":{"type":"GPIO","port":25,"dir":"output","pull":"pullup"},"echo":{"type":"GPIO","port":26,"dir":"input","pull":"pullup"}}}importhcsr04fromdriverimportGPIOimportutimeprint("Testing HCSR04 ...")echoDev=GPIO()echoDev.open("echo")trigDev=GPIO()trigDev.open("trig")hcsr04Dev=hcsr04.HCSR04(trigDev,echoDev)whileTrue:# 无限循环distance=disDev.measureDistance()print(str(distance)+' CM')utime.sleep(1)# 打印完之后休眠1秒输出TestingHCSR04...34CM492CM五、工作原理超声波工作示意图基本原理采用IO口Trig触发测距，对Trig口输出至少10us的高电平模块自动发送8个40Khz的方波，自动检测是否有信号返回有信号返回，通过IO口Echo输出一个高电平，高电平持续的时间就是超声波从发送到返回的时间测试距离 = （高电平持续的时间/声速340m/s）/2时序原理参考文献及购买链接[1]购买链接