Skip to main content

This is hcsr04 driver for HaaS Python. HaaS Python is forked from micropython.

Project description

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, see HaaS Python official website, Creative Case, Hardware Building Blocks

安装方法

Download the firmware corresponding to the development board at HaaS Python website and 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 on github, Our engineers will answer in time

import upip
upip.install("haas-python-hcsr04")
import haas-python-hcsr04

超声波测距传感器

一、产品简介

   HC-SR04超声波传感器使用声纳来确定物体的距离,其工作原理类似与蝙蝠。测量范围从2厘米到450厘米,HC-SR04超声波传感器上配有超声波发射器和接收器两个模块。
   超声波测距传感器是数字接口类型,其基本工作过程如下:

  • 发射器(触发引脚-Trig),发送信号 一段高频声音
  • 当信号遇到一个物体时,它会被反射
  • 然后发射器(回声引脚-Echo)接收该信号

引脚定义

  • VCC:DC-5V
  • Trig:触发(输入)
  • 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)

  • 参数说明
参数 类型 必选参数? 说明
trigObj GPIO GPIO配置输出,触发引脚,调用此函数前需确保trigObj对象已经处于open状态
echoObj GPIO GPIO配置输入,测量引脚,调用此函数前需确保echoObj对象已经处于open状态
  • 返回值

HCSR04对象成功,返回HCSR04对象;HCSR04对象创建失败,抛出Exception

  • 示例代码
import hcsr04
from driver import GPIO

print("Testing HCSR04 ...")
echoDev = GPIO()
echoDev.open("echo")

trigDev = GPIO()
trigDev.open("trig")

hcsr04Dev = hcsr04.HCSR04(trigDev,echoDev)
  • 输出
Testing HCSR04 ...

measureDistance() - 获取超声波测量的距离


  • 函数功能:

获取超声波测量的距离

  • 函数原型:

HCSR04.measureDistance()

  • 参数说明:

  • 返回值:

返回值为cm单位的距离值

  • 示例:
import hcsr04
from driver import GPIO
import utime

print("Testing HCSR04 ...")
echoDev = GPIO()
echoDev.open("echo")

trigDev = GPIO()
trigDev.open("trig")

hcsr04Dev = hcsr04.HCSR04(trigDev,echoDev)
while True:             # 无限循环
    distance = disDev.measureDistance()
    print(str(distance)+' CM')
    utime.sleep(1)      # 打印完之后休眠1秒
  • 输出
Testing HCSR04 ...
34 CM
492 CM

四、接口案例

此使用实例在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"
    }
  }
}
import hcsr04
from driver import GPIO
import utime

print("Testing HCSR04 ...")
echoDev = GPIO()
echoDev.open("echo")

trigDev = GPIO()
trigDev.open("trig")

hcsr04Dev = hcsr04.HCSR04(trigDev,echoDev)
while True:             # 无限循环
    distance = disDev.measureDistance()
    print(str(distance)+' CM')
    utime.sleep(1)      # 打印完之后休眠1秒
  • 输出
Testing HCSR04 ...
34 CM
492 CM

五、工作原理

超声波工作示意图


基本原理

  • 采用IO口Trig触发测距,对Trig口输出至少10us的高电平
  • 模块自动发送8个40Khz的方波,自动检测是否有信号返回
  • 有信号返回,通过IO口Echo输出一个高电平,高电平持续的时间就是超声波从发送到返回的时间
  • 测试距离 = (高电平持续的时间/声速340m/s)/2

时序原理

参考文献及购买链接

[1] 购买链接

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

haas-python-hcsr04-0.0.6.tar.gz (8.1 kB view details)

Uploaded Source

File details

Details for the file haas-python-hcsr04-0.0.6.tar.gz.

File metadata

  • Download URL: haas-python-hcsr04-0.0.6.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.5

File hashes

Hashes for haas-python-hcsr04-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7deddf4ee5710e82136ed7c80908061bd73414fc7470ec32d289cc94162a4237
MD5 c274702f4939b30c23dcd9a6ef79cbc1
BLAKE2b-256 7739594d6352018846dcd133c8d3795c8d79b31197b471a49b5243bd0e5dcaa3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page