{"id":1205,"date":"2022-03-26T09:30:40","date_gmt":"2022-03-26T01:30:40","guid":{"rendered":"https:\/\/104.es\/?p=1205"},"modified":"2022-03-26T09:30:40","modified_gmt":"2022-03-26T01:30:40","slug":"raspberry-pi-hc-sr04","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2022\/03\/26\/raspberry-pi-hc-sr04\/","title":{"rendered":"\u6a39\u8393\u6d3e\u9023\u63a5\u8d85\u97f3\u6ce2\u6e2c\u8ddd\u611f\u6e2c\u5668"},"content":{"rendered":"\n<p>\u5728\u7269\u806f\u7db2\u7684\u7df4\u7fd2\u4e2d\uff0cHC-SRC04\u8d85\u97f3\u6ce2\u6e2c\u8ddd\u611f\u6e2c\u5668\u7684\u4f7f\u7528\u4e5f\u662f\u5fc5\u5b78\u7684\u9805\u76ee\u4e4b\u4e00\uff0c\u900f\u904e\u7c21\u55ae\u7684\u7dda\u8def\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u8f15\u6613\u5730\u88fd\u4f5c\u51fa\u6e2c\u8ddd\u88dd\u7f6e\uff0c\u611f\u6e2c\u7684\u901f\u5ea6\u4e5f\u5f88\u5feb\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u6559\u5927\u5bb6\u5982\u4f55\u5229\u7528\u6a39\u8393\u6d3e\u5b8c\u6210\u6e2c\u8ddd\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u672c\u7bc7\u6587\u7ae0\u4f7f\u7528\u7684\u8d85\u97f3\u6ce2\u6e2c\u8ddd\u611f\u6e2c\u5668\u662f\u4e0b\u9762\u9019\u500b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-\u53f0\u7063\u7269\u806f\u79d1\u6280-taiwaniot-studio wp-block-embed-\u53f0\u7063\u7269\u806f\u79d1\u6280-taiwaniot-studio\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"VFhU39cTkR\"><a href=\"https:\/\/www.taiwaniot.com.tw\/product\/hc-sr04\/\">HC-SR04 \u8d85\u97f3\u6ce2\u6e2c\u8ddd \/ \u907f\u969c\u6a21\u7d44  \u8d85\u8072\u6ce2\u50b3\u611f\u5668<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"HC-SR04 \u8d85\u97f3\u6ce2\u6e2c\u8ddd \/ \u907f\u969c\u6a21\u7d44  \u8d85\u8072\u6ce2\u50b3\u611f\u5668 &#8212; \u53f0\u7063\u7269\u806f\u79d1\u6280 TaiwanIOT Studio\" src=\"https:\/\/www.taiwaniot.com.tw\/product\/hc-sr04\/embed\/#?secret=VFhU39cTkR\" data-secret=\"VFhU39cTkR\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u9019\u500b\u6a21\u7d44\u67094\u500b\u63a5\u8173\uff0c\u5206\u5225\u662fVcc\u9023\u63a55V\u96fb\u6e90\uff0cGND\u9023\u63a5GND\u63a5\u5730\uff0c\u4ee5\u53caTrig\u548cEcho\u5247\u4efb\u9078\u5169\u500bGPIO\u63a5\u8173\u9032\u884c\u9023\u63a5\uff0c\u4f46\u662f\u5728\u7a0b\u5f0f\u4e2d\u8981\u78ba\u5b9a\u9019\u5169\u500b\u63a5\u8173\u5206\u5225\u662f\u54ea\u4e00\u500bGPIO\uff0c\u4ee5\u672c\u7bc7\u6587\u7ae0\u7684\u4f8b\u5b50\uff0c\u6211\u5011\u628aTrig\u63a5\u5230\u5be6\u969b\u9023\u7dda\u7684PIN38\uff08GPIO20\uff09\uff0c\u4e26\u628aEcho\u63a5\u5230\u5be6\u969b\u9023\u7dda\u7684PIN40\uff08GPIO21\uff09\uff0c\u4e26\u53c3\u8003\u9019\u500b<a href=\"https:\/\/sites.google.com\/site\/rasberrypintust\/shu-mei-pai-xiao-ji-qiao\/8-hc-sr04-chao-yin-bo-gan-ce-qi\">\u9023\u7d50\u7db2\u9801<\/a>\u4e2d\u7684\u7a0b\u5f0f\u9032\u884c\u4fee\u6539\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\nimport RPi.GPIO as GPIO\n\nGPIO.setmode(GPIO.BCM)\nTRIG_PIN = 20\nECHO_PIN = 21\n\ndef get_distance(TRIG_PIN, ECHO_PIN):\n    GPIO.setup(TRIG_PIN,GPIO.OUT)  \n    GPIO.setup(ECHO_PIN,GPIO.IN)      \n    GPIO.output(TRIG_PIN, False)\n    time.sleep(0.5)\n    GPIO.output(TRIG_PIN, True)\n    time.sleep(0.00001)\n    GPIO.output(TRIG_PIN, False)\n    start = time.time()\n    while GPIO.input(ECHO_PIN)==0:\n        start = time.time()\n    while GPIO.input(ECHO_PIN)==1:\n        stop = time.time()\n    elapsed = stop-start\n    distance = elapsed * 34000\n    distance = distance \/ 2\n    return round(distance, 2)\n\ntry:\n    while True:\n        print(get_distance(TRIG_PIN, ECHO_PIN))\nexcept:\n    pass\nGPIO.cleanup()<\/code><\/pre>\n\n\n\n<p>\u6b64\u7a0b\u5f0f\u57f7\u884c\u4e4b\u5f8c\u5c31\u53ef\u6703\u4e0d\u65b7\u5730\u50b3\u56de\u5b83\u6240\u6e2c\u51fa\u7684\u8ddd\u96e2\u6578\u503c\uff08\u55ae\u4f4d\u662f\u516c\u5206\uff09\uff0c\u76f4\u5230\u4f60\u6309\u4e0bCtrl+C\u4e2d\u65b7\u7a0b\u5f0f\u7684\u57f7\u884c\u70ba\u6b62\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"500\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2022\/03\/image-21.png?w=633\" alt=\"\" class=\"wp-image-1211\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2022\/03\/image-21.png 633w, https:\/\/104.es\/wp-content\/uploads\/2022\/03\/image-21-300x237.png 300w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7269\u806f\u7db2\u7684\u7df4\u7fd2\u4e2d\uff0cHC-SRC04\u8d85\u97f3\u6ce2\u6e2c\u8ddd\u611f\u6e2c\u5668\u7684\u4f7f\u7528\u4e5f\u662f\u5fc5\u5b78\u7684\u9805\u76ee\u4e4b\u4e00\uff0c\u900f\u904e\u7c21\u55ae\u7684\u7dda\u8def\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u8f15\u6613\u5730\u88fd\u4f5c\u51fa\u6e2c\u8ddd\u88dd\u7f6e\uff0c\u611f\u6e2c\u7684\u901f\u5ea6\u4e5f\u5f88\u5feb\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c31\u4f86\u6559\u5927\u5bb6\u5982\u4f55\u5229\u7528\u6a39\u8393\u6d3e\u5b8c\u6210\u6e2c\u8ddd\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1205","post","type-post","status-publish","format-standard","hentry","category-raspberry-pi"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=1205"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/1205\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=1205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=1205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=1205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}