{"id":857,"date":"2020-09-08T23:33:11","date_gmt":"2020-09-08T15:33:11","guid":{"rendered":"https:\/\/www.iotch.xyz\/?p=857"},"modified":"2020-09-08T23:33:11","modified_gmt":"2020-09-08T15:33:11","slug":"%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e4%bd%bf%e7%94%a8hc-sr04%e8%b6%85%e5%a3%b0%e6%b3%a2%e6%b5%8b%e8%b7%9d%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=857","title":{"rendered":"\u6811\u8393\u6d3e\u4e0a\u4f7f\u7528HC-SR04\u8d85\u58f0\u6ce2\u6d4b\u8ddd\u6a21\u5757"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237929.jpg\" alt=\"\" class=\"wp-image-5320\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237817.png\" alt=\"\" class=\"wp-image-5319\"\/><\/figure><\/div>\n\n\n\n<p><br>\u8d85\u58f0\u6ce2\u6d4b\u8ddd\u7684\u539f\u7406\u5f88\u7b80\u5355\uff0c\u9ad8\u4e2d\u7269\u7406\u9898\u4e0d\u662f\u505a\u8fc7\u5f88\u591a\u6b21\u4e86\u4e48 \ud83d\ude00<br><br>\u8d85\u58f0\u6ce2\u6d4b\u8ddd\u539f\u7406<br>\u53ef\u4ee5\u770b\u5230\uff0c\u77e5\u9053\u65f6\u95f4\u95f4\u9694\u5c31\u80fd\u5f97\u5230\u8ddd\u79bb L\u3002<br>\u90a3\u4e48 HC-SR04 \u662f\u600e\u4e48\u6d4b\u8ddd\u7684\u5462\uff1f\u53c8\u600e\u4e48\u4f7f\u7528\u6811\u8393\u6d3e\u63a7\u5236\u5b83\uff1f<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">HC-SR04 \u4ecb\u7ecd<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237929.jpg\" alt=\"\" class=\"wp-image-5320\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237251.png\" alt=\"\" class=\"wp-image-5321\"\/><\/figure><\/div>\n\n\n\n<p>HC-SR04 \u6a21\u5757\u53ef\u4ee5\u6d4b\u91cf 3cm \u2013 4m \u7684\u8ddd\u79bb\uff0c\u7cbe\u786e\u5ea6\u53ef\u4ee5\u8fbe\u5230 3mm\u3002\u8fd9\u4e2a\u6a21\u5757\u5305\u62ec \u8d85\u58f0\u6ce2\u53d1\u5c04\u5668\u3001\u8d85\u58f0\u6ce2\u63a5\u6536\u5668\u548c\u63a7\u5236\u7535\u8def\u4e09\u90e8\u5206\u3002\u6709 4 \u4e2a\u5f15\u811a\u3002<br><br>HC-SR04 \u5b9e\u7269\u56fe<br><br>HC-SR04\u53c2\u6570\u8868<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u63a5\u7ebf\u65b9\u5f0f<\/h4>\n\n\n\n<p>4 \u4e2a\u5f15\u811a\u7531 2 \u4e2a\u7535\u6e90\u5f15\u811a(Vcc \u3001GND)\u548c 2 \u4e2a\u63a7\u5236\u5f15\u811a(Trig\u3001Echo)\u7ec4\u6210\u3002<br>Vcc \u548c Gnd \u63a5 5v DC \u7535\u6e90\uff0c\u4f46\u4e0d\u63a8\u8350\u7528\u72ec\u7acb\u7535\u6e90\u7ed9\u5b83\u4f9b\u7535\uff0c\u5e94\u4f7f\u7528\u6811\u8393\u6d3e\u6216\u5355\u7247\u673a\u7684 GPIO \u53e3\u8f93\u51fa 5v \u548c Gnd \u7ed9\u5b83\u4f9b\u7535\u3002\u4e0d\u7136\u4f1a\u5f71\u54cd\u8fd9\u4e2a\u6a21\u5757\u7684\u8fd0\u884c\u3002<br>Trig \u5f15\u811a\u7528\u6765\u63a5\u6536\u6765\u81ea\u6811\u8393\u6d3e\u7684\u63a7\u5236\u4fe1\u53f7\u3002\u63a5\u4efb\u610f GPIO \u53e3\u3002<br>Echo \u5f15\u811a\u7528\u6765\u53d1\u9001\u6d4b\u8ddd\u7ed3\u679c\u7ed9\u6811\u8393\u6d3e\u3002\u63a5\u4efb\u610f GPIO \u53e3\u3002<\/p>\n\n\n\n<p>(\u6ce8\u610f Echo \u8fd4\u56de\u7684\u662f 5v\u4fe1\u53f7\uff0c\u800c\u6811\u8393\u6d3e\u7684 GPIO \u63a5\u6536\u8d85\u8fc7 3.3v \u7684\u4fe1\u53f7\u53ef\u80fd\u4f1a\u88ab\u70e7\u6bc1\uff0c\u56e0\u6b64\u9700\u8981\u52a0\u4e00\u4e2a\u5206\u538b\u7535\u8def)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">HC-SR04 \u7684\u6d4b\u8ddd\u8fc7\u7a0b<\/h4>\n\n\n\n<p>1. \u6811\u8393\u6d3e\u5411 Trig \u811a\u53d1\u9001\u4e00\u4e2a\u6301\u7eed 10us \u7684\u8109\u51b2\u4fe1\u53f7\u3002<br>2. HC-SR04 \u63a5\u6536\u5230\u6811\u8393\u6d3e\u53d1\u9001\u7684\u8109\u51b2\u4fe1\u53f7\uff0c\u5f00\u59cb\u53d1\u9001\u8d85\u58f0\u6ce2 (start sending ultrasoun\uff09\uff0c\u5e76\u628a Echo\u7f6e\u4e3a\u9ad8\u7535\u5e73\u3002 &nbsp; &nbsp;\u7136\u540e\u51c6\u5907\u63a5\u6536\u8fd4\u56de\u7684\u8d85\u58f0\u6ce2\u3002<br>3. \u5f53 HC-SR04 \u63a5\u6536\u5230\u8fd4\u56de\u7684\u8d85\u58f0\u6ce2 (receive returned ultrasound) \u65f6\uff0c\u628a Echo \u7f6e\u4e3a\u4f4e\u7535\u5e73\u3002<\/p>\n\n\n\n<p>\u4ece\u4e0a\u8ff0\u8fc7\u7a0b\u53ef\u4ee5\u770b\u51fa\uff0c Echo \u9ad8\u7535\u5e73\u6301\u7eed\u7684\u65f6\u95f4\u5c31\u662f\u8d85\u58f0\u6ce2\u4ece\u53d1\u5c04\u5230\u8fd4\u56de\u6240\u7ecf\u8fc7\u7684\u65f6\u95f4\u95f4\u9694 ~<\/p>\n\n\n\n<p>\u8bf7\u5bf9\u7167\u4e0b\u56fe\u3002<br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237453.png\" alt=\"\" srcset=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237453.png 945w, https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237453-768x616.png 768w\" width=\"945\" height=\"758\"><br>\u7535\u8def\u56fe\u4e0e python \u7a0b\u5e8f<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237388.png\" alt=\"\" class=\"wp-image-5323\"\/><\/figure><\/div>\n\n\n\n<p><br>\u7535\u8def\u56fe<br>\u63a5\u7ebf\u8ddf\u524d\u6587\u6240\u8bf4\u7684\u4e00\u6837\u3002 GPIO 2 \u811a\u63a5 Trig \uff0c GPIO 3 \u811a\u63a5 Echo \u3002\u6811\u8393\u6d3e\u7684 +5v \u548c Gnd \u4e0e HC-SR04 \u7684 Vcc \u548c Gnd \u76f8\u8fde\u3002\u8fd8\u6709\u4e00\u4e2a\u5206\u538b\u7535\u8def\uff0c\u4e00\u7aef\u63a5 Echo \uff0c\u53e6\u4e00\u7aef\u63a5 Gnd\u3002<\/p>\n\n\n\n<p>1k \u548c 2k \u7535\u963b\u7ec4\u6210\u4e86\u4e00\u4e2a\u5206\u538b\u7535\u8def\uff0c\u4f7f GPIO 3 \u811a\u7684\u7535\u538b\u964d\u5230\u4e86 3.3v \u5de6\u53f3\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">python \u7a0b\u5e8f<\/h4>\n\n\n\n<p>\u521d\u59cb\u5316\u76f8\u5173\u5f15\u811a:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237456.png\" alt=\"\" class=\"wp-image-5324\"\/><\/figure><\/div>\n\n\n\n<p>2 \u811a\u8fde Trig \uff0c\u8bbe\u4e3a\u8f93\u51fa\u6a21\u5f0f\uff1b 3 \u811a\u8fde Echo\uff0c\u8bbe\u4e3a\u8f93\u5165\u6a21\u5f0f\u3002<br>\u7136\u540e\u5411 Trig \u5f15\u811a\u8f93\u5165 10us \u7684\u8109\u51b2\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237689.png\" alt=\"\" class=\"wp-image-5325\"\/><\/figure><\/div>\n\n\n\n<p>time.sleep() &nbsp;\u63a5\u6536\u7684\u53c2\u6570\u5355\u4f4d\u4e3a s \uff0c\u4e8e\u662f\u628a10 us \u8f6c\u6362\u4e3a 0.00001 s \u3002<br>\u63a5\u6536\u5230\u8fd9\u4e2a\u8109\u51b2\u540e\uff0cHC-SR04 \u53d1\u5c04\u51fa\u8d85\u58f0\u6ce2\uff0c\u540c\u65f6\u628a Echo \u7f6e\u4e3a\u9ad8\u7535\u5e73\u3002\u5728\u53d1\u5c04\u4e4b\u524d\uff0cEcho \u4e00\u76f4\u4e3a\u4f4e\u7535\u5e73\u3002<br>\u636e\u6b64\u7f16\u5199\u7a0b\u5e8f\uff0c\u8bb0\u5f55\u8d85\u58f0\u6ce2\u53d1\u5c04\u65f6\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237265.png\" alt=\"\" class=\"wp-image-5326\"\/><\/figure><\/div>\n\n\n\n<p>\u7136\u540e\u8bb0\u5f55\u8d85\u58f0\u6ce2\u8fd4\u56de\u65f6\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237109.png\" alt=\"\" class=\"wp-image-5327\"\/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6837\u5c31\u83b7\u5f97\u4e86\u6211\u4eec\u9700\u8981\u7684\u6570\u636e pulse_start \u548c pulse_end \uff0c\u53ef\u4ee5\u7b97\u51fa\u8ddd\u79bb\u4e86~<br>\u6d4b\u5f97\u8ddd\u79bb(\u5355\u4f4d:m) &nbsp;= &nbsp;(pulse_end \u2013 pulse_start) * \u58f0\u6ce2\u901f\u5ea6 \/ 2<br>\u58f0\u6ce2\u901f\u5ea6\u53d6 343m\/s \u3002<\/p>\n\n\n\n<p>\u7136\u540e\u518d\u628a\u6d4b\u5f97\u7684\u8ddd\u79bb\u8f6c\u6362\u4e3a cm\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/shumeipai.nxez.com\/wp-content\/uploads\/2018\/12\/20181222192237480.png\" alt=\"\" class=\"wp-image-5328\"\/><\/figure><\/div>\n\n\n\n<p>\u6d4b\u5f97\u8ddd\u79bb(\u5355\u4f4d:cm) = (pulse_end \u2013 pulse_start) * \u58f0\u6ce2\u901f\u5ea6 \/ 2 * 100<br>= &nbsp;(pulse_end \u2013 pulse_start) * 17150<br><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5bfc\u5165 GPIO\u5e93\nimport RPi.GPIO as GPIO\nimport time\n  \n#\u8bbe\u7f6e GPIO \u6a21\u5f0f\u4e3a BCM\nGPIO.setmode(GPIO.BCM)\n  \n#\u5b9a\u4e49 GPIO \u5f15\u811a\nGPIO_TRIGGER = 23\nGPIO_ECHO = 24\n  \n#\u8bbe\u7f6e GPIO \u7684\u5de5\u4f5c\u65b9\u5f0f (IN \/ OUT)\nGPIO.setup(GPIO_TRIGGER, GPIO.OUT)\nGPIO.setup(GPIO_ECHO, GPIO.IN)\n  \ndef distance():\n    # \u53d1\u9001\u9ad8\u7535\u5e73\u4fe1\u53f7\u5230 Trig \u5f15\u811a\n    GPIO.output(GPIO_TRIGGER, True)\n  \n    # \u6301\u7eed 10 us \n    time.sleep(0.00001)\n    GPIO.output(GPIO_TRIGGER, False)\n  \n    start_time = time.time()\n    stop_time = time.time()\n  \n    # \u8bb0\u5f55\u53d1\u9001\u8d85\u58f0\u6ce2\u7684\u65f6\u523b1\n    while GPIO.input(GPIO_ECHO) == 0:\n        start_time = time.time()\n  \n    # \u8bb0\u5f55\u63a5\u6536\u5230\u8fd4\u56de\u8d85\u58f0\u6ce2\u7684\u65f6\u523b2\n    while GPIO.input(GPIO_ECHO) == 1:\n        stop_time = time.time()\n  \n    # \u8ba1\u7b97\u8d85\u58f0\u6ce2\u7684\u5f80\u8fd4\u65f6\u95f4 = \u65f6\u523b2 - \u65f6\u523b1\n    time_elapsed = stop_time - start_time\n    # \u58f0\u6ce2\u7684\u901f\u5ea6\u4e3a 343m\/s\uff0c \u8f6c\u5316\u4e3a 34300cm\/s\u3002\n    distance = (time_elapsed * 34300) \/ 2\n  \n    return distance\n  \nif __name__ == '__main__':\n    try:\n        while True:\n            dist = distance()\n            print(\"Measured Distance = {:.2f} cm\".format(dist))\n            time.sleep(1)\n  \n        # Reset by pressing CTRL + C\n    except KeyboardInterrupt:\n        print(\"Measurement stopped by User\")\n        GPIO.cleanup()<\/code><\/pre>\n\n\n\n<p>\u4f5c\u8005\uff1ajiang<br>\u94fe\u63a5\uff1ajianshu.com\/p\/293415ae3e9c<strong>\u6587\u7ae0\u6807\u9898\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2019\/01\/02\/hc-sr04-ultrasonic-ranging-module-on-raspberry-pi.html\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e\u4e0a\u4f7f\u7528HC-SR04\u8d85\u58f0\u6ce2\u6d4b\u8ddd\u6a21\u5757<\/a> - <a href=\"https:\/\/shumeipai.nxez.com\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e\u5b9e\u9a8c\u5ba4<\/a><strong>\u56fa\u5b9a\u94fe\u63a5\uff1a<\/strong><a href=\"https:\/\/shumeipai.nxez.com\/2019\/01\/02\/hc-sr04-ultrasonic-ranging-module-on-raspberry-pi.html\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/shumeipai.nxez.com\/2019\/01\/02\/hc-sr04-ultrasonic-ranging-module-on-raspberry-pi.html<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"857\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u8d85\u58f0\u6ce2\u6d4b\u8ddd\u7684\u539f\u7406\u5f88\u7b80\u5355\uff0c\u9ad8\u4e2d\u7269\u7406\u9898\u4e0d\u662f\u505a\u8fc7\u5f88\u591a\u6b21\u4e86\u4e48 \ud83d\ude00 \u8d85\u58f0\u6ce2\u6d4b\u8ddd\u539f\u7406\u53ef\u4ee5\u770b\u5230\uff0c\u77e5\u9053\u65f6\u95f4\u95f4\u9694\u5c31\u80fd\u5f97\u5230\u8ddd\u79bb L\u3002\u90a3\u4e48 HC-SR0 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-857","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=857"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/857\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}