{"id":761,"date":"2022-12-17T15:56:36","date_gmt":"2022-12-17T07:56:36","guid":{"rendered":"http:\/\/wordpress.loc\/?p=761"},"modified":"2022-12-17T15:57:02","modified_gmt":"2022-12-17T07:57:02","slug":"esp32_ch1-32","status":"publish","type":"post","link":"https:\/\/www.nmking.io\/index.php\/2022\/12\/17\/761\/","title":{"rendered":"ESP32\u985e\u6bd4\u8f38\u51faSG90"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>ESP32\u7684\u985e\u6bd4\u8f38\u51fa\u8f03\u70ba\u9ebb\u7169\uff0c\u63a1\u7528\u7684\u662fledcWrite\u6307\u4ee4\uff0c\u4e00\u822c\u4f86\u8aaa\u6c92\u7279\u5225\u9700\u8981\u76f4\u63a5\u4f7f\u7528ESP32Servo\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u63a1\u7528ledcWrite\u7684SG90\u6307\u4ee4\u3002<\/p><\/blockquote><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-ht-block-toc is-style-rounded htoc htoc--position-wide toc-list-style-plain\" data-htoc-state=\"expanded\"><span class=\"htoc__title\"><span class=\"ht_toc_title\">Table of Contents<\/span><span class=\"htoc__toggle\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"16\" height=\"16\"><g fill=\"#444\"><path d=\"M15 7H1c-.6 0-1 .4-1 1s.4 1 1 1h14c.6 0 1-.4 1-1s-.4-1-1-1z\"><\/path><path d=\"M15 1H1c-.6 0-1 .4-1 1s.4 1 1 1h14c.6 0 1-.4 1-1s-.4-1-1-1zM15 13H1c-.6 0-1 .4-1 1s.4 1 1 1h14c.6 0 1-.4 1-1s-.4-1-1-1z\"><\/path><\/g><\/svg><\/span><\/span><div class=\"htoc__itemswrap\"><ul class=\"ht_toc_list\"><li class=\"\"><a href=\"#htoc-\">\u5be6\u505a\u8aaa\u660e<\/a><\/li><li class=\"\"><a href=\"#htoc-1\">\u4e00\u3001\u8a2d\u5b9a\u983b\u9053<\/a><\/li><li class=\"\"><a href=\"#htoc-11\">\u4e8c\u3001\u9644\u52a0\u983b\u9053\u5230\u8173\u4f4d<\/a><\/li><li class=\"\"><a href=\"#htoc-111\">\u4e09\u3001\u5beb\u5165\u6578\u503c<\/a><\/li><\/ul><\/div><\/div>\n\n\n\n<p>\u6211\u7684ESP32\u5be6\u505a\u66f8\u7c4d\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.nmking.io\/index.php\/2022\/11\/10\/esp32_ch1-2\/\" target=\"_blank\">\u6211\u51fa\u66f8\u4e86 ESP32 \u7269\u806f\u7db2\u5c08\u984c<\/a><br>\u535a\u5ba2\u4f86\u7db2\u5740\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.books.com.tw\/products\/0010901195\" target=\"_blank\">https:\/\/www.books.com.tw\/products\/0010901195<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"htoc-\">\u5be6\u505a\u8aaa\u660e<\/h2>\n\n\n\n<p>ESP32\u7684\u985e\u6bd4\u8f38\u51fa\u8f03\u70ba\u9ebb\u7169\uff0c\u63a1\u7528\u7684\u662fledcWrite\u6307\u4ee4\uff0c\u4e00\u822c\u4f86\u8aaa\u6c92\u7279\u5225\u9700\u8981\u76f4\u63a5\u4f7f\u7528ESP32Servo\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u63a1\u7528ledcWrite\u7684SG90\u6307\u4ee4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"htoc-1\">\u4e00\u3001\u8a2d\u5b9a\u983b\u9053<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcSetup(7, 50, 8);<\/code><\/pre>\n\n\n\n<p>ESP32\u4e00\u5171\u670916\u500b\u985e\u6bd4\u8f38\u51fa\u983b\u9053\uff080~15\uff09\uff0c\u6211\u5011\u5728\u9019\u88e1\u4f7f\u7528\u983b\u90537\uff0c\u800c50\u5247\u662f50 Hz\u8f38\u51fa\u983b\u7387\u4e5f\u5c31\u662f1\u79d2\/50=20ms\u5bec\u5ea6\uff0c8\u5247\u662f\u89e3\u6790\u5ea6\uff0c\u4ee3\u88682\u76848\u6b21\u65b9=256\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"htoc-11\">\u4e8c\u3001\u9644\u52a0\u983b\u9053\u5230\u8173\u4f4d<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcAttachPin(2, 7); <\/code><\/pre>\n\n\n\n<p>\u5c07\u4e0a\u8ff0\u8a2d\u5b9a\u597d\u7684\u983b\u90537\u9644\u52a0\u5728GPIO 2\u8173\u4f4d\uff0c\u800cGPIO 2\u5247\u662f\u6211\u5011\u9023\u63a5SG90\u7684\u8173\u4f4d\uff0c\u4e5f\u5c31\u662f\u4ee3\u8868\u5f8c\u7e8c\u6211\u5011\u53ea\u9700\u8981\u6307\u5b9a\u983b\u90537\u7684\u8f38\u51fa\u6578\u503c\uff0c\u5c31\u662f\u8f38\u51fa\u5728GPIO 2\u4e0a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"htoc-111\">\u4e09\u3001\u5beb\u5165\u6578\u503c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcWrite(7, 18.56);<\/code><\/pre>\n\n\n\n<p>\u5728\u983b\u90537\u8f38\u51fa18.56\u7684\u8108\u885d\uff0c\u4ee3\u8868\u5c07SG90\u8f49\u523090\u5ea6\u7684\u4f4d\u7f6e\uff0c\u81f3\u65bc18.56\u4ee3\u886890\u5ea6\u662f\u5982\u4f55\u7b97\u51fa\u7684\u5462\uff1f<br>\u9996\u5148\u983b\u9053\u8a2d\u5b9a\u70ba50Hz\uff0c\u4e5f\u5c31\u662f1\u79d2\/50=20ms\u9031\u671f\uff0c20ms\u9031\u671f\u53c8\u88ab\u5206\u70ba2\u76848\u6b21\u65b9=256\u500b\u7b49\u7d1a\uff0c\u56e0\u6b64\u4e00\u500bPWM\u55ae\u4f4d\u662f20\/256\u3002<br><br>\u53e6\u4e00\u65b9\u9762SG90\u7684\u8108\u885d\u70ba500us~2400us\uff0c\u4e5f\u5c31\u662f0.5ms~2.4ms\uff0c\u56e0\u6b64\u63db\u7b97\u70baPWM\u7bc4\u570d\u5c31\u662f0.5\/(20\/256)~2.4\/(20\/256)=6.4~30.72\u4e4b\u9593\uff0c\u800c6.4\u53ca30.72\u5c31\u5206\u5225\u4ee3\u88680\u53ca180\u5ea6\u4e86\uff0c\u56e0\u6b6490\u5ea6\u5c31\u662f(6.4+30.72)\/2=18.56\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u5247\u70baSG90\u5f9e0\uff5e180\u5ea6\u7684\u6e2c\u8a66\u7a0b\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int value=map(angle,0,180,6.4,30.72);<\/code><\/pre>\n\n\n\n<p>map\u662f\u4e00\u500b\u5f88\u65b9\u4fbf\u7684\u53d6\u6bd4\u4f8b\u51fd\u6578\uff0c\u53c3\u6578\u5206\u5225\u662f \u8f49\u63db\u5f8c\u6578\u503c\uff1dmap(\u6578\u503c,\u6578\u503c\u6700\u5c0f\u503c,\u6578\u503c\u6700\u5927\u503c,\u8f49\u63db\u6700\u5c0f\u6642,\u8f49\u63db\u6700\u5927\u503c)<br>\u4f8b\u598290\u5ea6\u7684\u8a08\u7b97\u65b9\u5f0f\uff1amap(90,0,180,6.4,30.72)\u5f97\u523018.56\uff0c\u5c31\u662fledcWrite\u8981\u5beb\u5165\u7684\u6578\u503c\u4e86<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1620620123-3513274074-g_n.png?resize=840%2C160&#038;ssl=1\" alt=\"\" width=\"840\" height=\"160\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>void setup()\n{  \n  Serial.begin(115200);\n  ledcSetup(7, 50, 8);  \n  ledcAttachPin(2, 7);  \n}\nvoid loop()\n{\n  for(int angle = 0; angle &lt;= 180; angle++)\n  {\n    int value=map(angle,0,180,6.4,30.72);\n    ledcWrite(7, value);\n    Serial.println(\"\u89d2\u5ea6=\" + String(angle) + \",value=\" + String(value));\n    delay(70);    \n  }\ndelay(1000);\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1620619189-2048832557-g.png?resize=618%2C801&#038;ssl=1\" alt=\"\" width=\"618\" height=\"801\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>ESP32\u7684\u985e\u6bd4\u8f38\u51fa\u8f03\u70ba\u9ebb\u7169\uff0c\u63a1\u7528\u7684\u662fledcWrite\u6307\u4ee4\uff0c\u4e00\u822c\u4f86\u8aaa\u6c92\u7279\u5225\u9700\u8981\u76f4\u63a5\u4f7f\u7528ESP32Servo\u5373 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-esp32"],"blocksy_meta":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":1,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":762,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/761\/revisions\/762"}],"wp:attachment":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}