{"id":2871,"date":"2023-08-06T17:48:44","date_gmt":"2023-08-06T09:48:44","guid":{"rendered":"https:\/\/104.es\/?p=2871"},"modified":"2023-08-06T17:48:46","modified_gmt":"2023-08-06T09:48:46","slug":"nodemcu-mpu6050","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/06\/nodemcu-mpu6050\/","title":{"rendered":"\u3010NodeMCU\u3011MPU6050\u4e09\u8ef8\u9640\u87ba\u5100\u53ca\u4e09\u8ef8\u52a0\u901f\u8a08\u611f\u6e2c\u6a21\u7d44"},"content":{"rendered":"\n<p>MPU6050\u652f\u63f4I2C\u4ecb\u9762\uff0c\u9023\u63a5\u7684\u65b9\u5f0f\u975e\u5e38\u7c21\u55ae\uff0c\u4e5f\u80fd\u5920\u5f88\u5feb\u5730\u4e0a\u624b\u4f7f\u7528\u3002\u5728NodeMCU\u7684MicroPython\u4e2d\uff0c\u5df2\u7d93\u6709\u71b1\u5fc3\u7684\u9ad8\u624b\u5e6b\u6211\u5011\u5beb\u597d\u4e86\u6a21\u7d44\uff0c\u53ef\u4ee5\u76f4\u63a5\u62ff\u4f86\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/adamjezek98\/MPU6050-ESP8266-MicroPython\">https:\/\/github.com\/adamjezek98\/MPU6050-ESP8266-MicroPython<\/a><\/p>\n\n\n\n<p>\u540c\u5b78\u5011\u53ea\u8981\u628a\u4e0a\u9762\u7684\u6a21\u7d44\u4e0b\u8f09\u4e0b\u4f86\u89e3\u58d3\u7e2e\uff0c\u6703\u770b\u5230\u4e00\u500bmpu6050.py\u7684\u6a94\u6848\uff0c\u628a\u90a3\u500b\u6a94\u6848\u5229\u7528ampy\u4e0a\u50b3\u5230NodeMCU\u7684\u677f\u5b50\u4e0a\uff0c\u540c\u6642\u63a5\u4e0a5V\u7684\u96fb\u6e90\u4ee5\u53caGND\uff0c\u4e26\u628aSLC\u63a5\u5230D1\uff0cSDA\u63a5\u5230NodeMCU\u7684D2\uff0c\u5728\u786c\u9ad4\u4e0a\u7684\u9023\u7dda\u5c31\u7b97\u662f\u5b8c\u6210\u4e86\u3002\u63a5\u7dda\u4e4b\u5f8c\u7684\u5916\u89c0\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-1024x576.png\" alt=\"\" class=\"wp-image-2872\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-1024x576.png 1024w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-300x169.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-768x432.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-1536x864.png 1536w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-2048x1152.png 2048w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50-600x338.png 600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5728\u6a21\u7d44\u4e2d\u5373\u6709\u4f7f\u7528Shell\u4ecb\u9762\u7684\u65b9\u5f0f\u64cd\u4f5c\u7684\u7bc4\u4f8b\u3002\u5982\u679c\u6211\u5011\u6253\u7b97\u91cf\u6e2c\u4e00\u4e0b\u975c\u7f6e\u4e00\u6bb5\u6642\u9593\u4e4b\u5e73\u5747\u6578\u64da\uff0c\u7a0b\u5f0f\u53ef\u4ee5\u7de8\u5beb\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from machine import I2C, Pin\r\nimport mpu6050, time\r\ni2c = I2C(scl=Pin(5), sda=Pin(4))\r\nacc = mpu6050.accel(i2c)\r\ncount = 600\r\ndelta = 0.1\r\nsum_x, sum_y, sum_z = 0, 0, 0\r\nfor i in range(count):\r\n\ttime.sleep(delta)\r\n\td = acc.get_values()\r\n\tsum_x += d&#91;'GyX']\r\n\tsum_y += d&#91;'GyY']\r\n\tsum_z += d&#91;'GyZ']\r\navg_x = float(sum_x) \/ count\r\navg_y = float(sum_y) \/ count\r\navg_z = float(sum_z) \/ count\r\n\r\nwith open(\"avg_values.txt\", \"wt\", encoding='utf-8') as fp:\r\n\tfp.write(\"x:{}, y:{}, z:{}\\n\".format(avg_x, avg_y, avg_z))<\/code><\/pre>\n\n\n\n<p>\u5728\u6309\u4e0bRESET\u6309\u9215\u57f7\u884cmain.py\u7a0b\u5f0f\u4e00\u6bb5\u6642\u9593\u4e4b\u5f8c\uff0c\u5373\u53ef\u5229\u7528ampy\u4f86\u770b\u770b\u5e73\u5747\u503c\u662f\u591a\u5c11\uff08\u5b83\u6703\u5beb\u5728avg_values.txt\u6a94\u6848\u4e2d\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"232\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-51.png\" alt=\"\" class=\"wp-image-2873\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-51.png 846w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-51-300x82.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-51-768x211.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-51-600x165.png 600w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>MPU6050\u652f\u63f4I2C\u4ecb\u9762\uff0c\u9023\u63a5\u7684\u65b9\u5f0f\u975e\u5e38\u7c21\u55ae\uff0c\u4e5f\u80fd\u5920\u5f88\u5feb\u5730\u4e0a\u624b\u4f7f\u7528\u3002\u5728NodeMCU\u7684MicroPython\u4e2d\uff0c\u5df2\u7d93\u6709\u71b1\u5fc3\u7684\u9ad8\u624b\u5e6b\u6211\u5011\u5beb\u597d\u4e86\u6a21\u7d44\uff0c\u53ef\u4ee5\u76f4\u63a5\u62ff\u4f86\u4f7f\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":2872,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,15],"tags":[],"class_list":["post-2871","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodemcu","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/image-50.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2871","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=2871"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2871\/revisions"}],"predecessor-version":[{"id":2874,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2871\/revisions\/2874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2872"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}