{"id":654,"date":"2022-12-02T13:51:03","date_gmt":"2022-12-02T05:51:03","guid":{"rendered":"http:\/\/wordpress.loc\/?p=654"},"modified":"2022-12-02T13:52:09","modified_gmt":"2022-12-02T05:52:09","slug":"esp32_ch1-24","status":"publish","type":"post","link":"https:\/\/www.nmking.io\/index.php\/2022\/12\/02\/654\/","title":{"rendered":"Arduino ESP32 PWM\u8f38\u51fa L9110 140C04\u99ac\u9054\u8f49\u901f\u63a7\u5236\u8207\u985e\u6bd4\u8f38\u51fa"},"content":{"rendered":"\n<figure class=\"wp-block-pullquote\"><blockquote><p>ESP32\u4ec0\u9ebc\u90fd\u597d\uff0c\u4f46\u662fPWM\u662f\u4e00\u500b\u554f\u984c\uff0c\u50b3\u7d71UNO\u53ef\u4ee5\u7528analogWrite\u7c21\u55ae\u7684\u5be6\u73fe\u985e\u6bd4\u8f38\u51fa\uff0c\u50cf\u662f\u8b93LED\u6162\u6162\u4eae\u8d77\uff0c\u6216\u8005\u767c\u51fa\u4e0d\u540c\u983b\u7387\u7684\u8702\u9cf4\u5668\u7b49\uff0c\u4f46\u662fESP32\u6c92\u6709analogWrite\u7684\u529f\u80fd\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-rgb-led\">\u5982\u4f55\u4f7f\u7528RGB LED\uff1f<\/a><ul class=\"ht_toc_child_list\"><li class=\"\"><a href=\"#htoc-1-ledchannel\">1.\u8a2d\u5b9a\u983b\u9053LEDchannel\u5c6c\u6027<\/a><\/li><li class=\"\"><a href=\"#htoc-2-pin\">2.\u9644\u52a0\u5230PIN\u8173<\/a><\/li><li class=\"\"><a href=\"#htoc-3\">3.\u6c7a\u5b9a\u8f38\u51fa\u5927\u5c0f<\/a><\/li><\/ul><\/li><li class=\"\"><a href=\"#htoc-l9110-fan-motor\">\u4f7f\u7528L9110 Fan Motor<\/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-rgb-led\">\u5982\u4f55\u4f7f\u7528RGB LED\uff1f<\/h2>\n\n\n\n<p>ESP32\u4ec0\u9ebc\u90fd\u597d\uff0c\u4f46\u662fPWM\u662f\u4e00\u500b\u554f\u984c\uff0c\u50b3\u7d71UNO\u53ef\u4ee5\u7528analogWrite\u7c21\u55ae\u7684\u5be6\u73fe\u985e\u6bd4\u8f38\u51fa\uff0c\u50cf\u662f\u8b93LED\u6162\u6162\u4eae\u8d77\uff0c\u6216\u8005\u767c\u51fa\u4e0d\u540c\u983b\u7387\u7684\u8702\u9cf4\u5668\u7b49\uff0c\u4f46\u662fESP32\u6c92\u6709analogWrite\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u4e00\u958b\u59cb\u6211\u5e7e\u4e4e\u6c92\u7528\u5230PWM\u6240\u4ee5\u4e5f\u6c92\u5dee\uff0c\u5f8c\u4f86\u6709\u5b78\u751f\u554f\u5982\u4f55\u7528RGB LED\u624d\u767c\u73fe\u6211\u4e0d\u6703\uff0cgoogle\u4e00\u4e0b\u624d\u767c\u73fe\u539f\u4f86\u662f\u7528LEDchannel\u53c3\u95b1\uff1a\u300c <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/<\/a> \u300d\uff0c\u4f9d\u64da\u898f\u683c\u66f8ESP32\u5408\u8a08\u670916\u500bLEDchannel\uff0c\u53ea\u662f\u6211\u6c92\u7528\u904e\u9019\u9ebc\u591a\uff0c\u66fe\u7d93\u6e2c\u8a66\u4e00\u9846RGB LED\u7528\u4e86\u4e09\u500bLEDchannel\u6c92\u554f\u984c\uff0c\u5c31\u6c92\u6e2c\u8a66\u66f4\u591a\u7684\u6642\u5019\u6703\u4e0d\u6703\u904b\u4f5c\u3002<\/p>\n\n\n\n<p>\u7a0b\u5f0f\u7684\u90e8\u4efd\u4e3b\u8981\u5206\u6210\u4e09\u500b\uff1a1.\u8a2d\u5b9a\u983b\u9053LEDchannel\u30012.\u9644\u52a0\u5230PIN\u8173\u30013.\u6c7a\u5b9a\u8f38\u51fa\u5927\u5c0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"htoc-1-ledchannel\">1.\u8a2d\u5b9a\u983b\u9053LEDchannel\u5c6c\u6027<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcSetup(LEDChannel, freq, resolution);\n\/\/LEDChannel\u8a2d\u5b9a\u70ba0-15(\u81ea\u5df1\u731c\u7684)\uff0c\u4e0d\u540c\u8f38\u51fa\u8981\u8a2d\u5b9a\u5230\u4e0d\u540c\u983b\u9053\uff0c\u4f8b\u5982RGB LED\u5c31\u8981\u958b\u4e09\u500b\u983b\u9053\u5206\u5225\u7ba1\u7406R\u3001G\u3001B\n\/\/freq\u8f38\u51fa\u983b\u7387\uff0c\u5efa\u8b70\u503c5000 Hz\n\/\/resolution\u4ee3\u8868\u8f38\u51fa\u89e3\u6790\u5ea6\uff0c\u4f8b\u59828\u4ee3\u88680-255\uff0c10\u4ee3\u88680-1023<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"htoc-2-pin\">2.\u9644\u52a0\u5230PIN\u8173<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcAttachPin(ledPin, LEDChannel);\n\/\/ledPin\u4ee3\u8868\u8173\u4f4d\uff0c\u770b\u4f60\u628a\u8a2d\u5099\u63a5\u5728\u54ea\u500b\u8173\u4f4d\u4e0a\u9762\n\/\/LEDchannel\u4ee3\u8868\u6b65\u9a5f1\u6240\u5ba3\u544a\u7684LEDchannel\uff0c\u4e5f\u5c31\u662f\u8aaa\u628a\u8a2d\u5b9a\u597d\u7684LEDchannel\u5c6c\u6027\u9644\u52a0\u5230\u67d0\u500b\u8173\u4f4d\u4e0a<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"htoc-3\">3.\u6c7a\u5b9a\u8f38\u51fa\u5927\u5c0f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcWrite(LEDChannel, dutyCycle);\n\/\/\u5c07LEDchannel\u8f38\u51fadutyCycle\u7684\u503c\u3002<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u7bc4\u4f8b\u7a0b\u5f0f\u5c07\u4f7f\u63a5\u5728Pin16\u7684LED\u9010\u6f38\u4eae\u8d77\u4e26\u7184\u6ec5\uff0c\u672c\u7bc4\u4f8b\u8907\u88fd\u65bc <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ the number of the LED pin\nconst int ledPin = 16;  \/\/ 16 corresponds to GPIO16\n\n\/\/ setting PWM properties\nconst int freq = 5000;\nconst int ledChannel = 0;\nconst int resolution = 8;\n \nvoid setup(){\n  \/\/\u5ba3\u544aledChannel\u7279\u6027\n  ledcSetup(ledChannel, freq, resolution);\n  \n  \/\/\u5c07\u4e0a\u8ff0ledChannel\u9644\u52a0\u5230ledPin\u8173\u4f4d\u4e0a\n  ledcAttachPin(ledPin, ledChannel);\n}\n \nvoid loop(){\n  \/\/ \u9010\u6f38\u589e\u52a0\u4eae\u5ea6\n  for(int dutyCycle = 0; dutyCycle &lt;= 255; dutyCycle++){   \n    \/\/ changing the LED brightness with PWM\n    ledcWrite(ledChannel, dutyCycle);\n    delay(15);\n  }\n\n  \/\/ \u9010\u6f38\u6e1b\u4f4e\u4eae\u5ea6\n  for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){\n    \/\/ changing the LED brightness with PWM\n    ledcWrite(ledChannel, dutyCycle); \n    delay(15);\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"htoc-l9110-fan-motor\">\u4f7f\u7528L9110 Fan Motor<\/h2>\n\n\n\n<p>\u53e6\u5916\u4e00\u500b\u6848\u4f8b\u5c07\u4f7f\u7528L9110 Fan Motor\uff0c\u9019\u500b\u96d6\u7136\u6bd4DC\u5c0f\u99ac\u9054\u8cb4\u4e00\u9ede\uff0c\u4f46\u662f\u53ef\u4ee5\u7528PWM\u63a7\u5236\u8f49\u901f\uff0c\u4e5f\u4e0d\u6703\u50cfDC\u99ac\u9054\u4e00\u8d77\u52d5\u5403\u6389\u96fb\u6d41\uff0c\u5c0e\u81f4\u7576\u6a5f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/pic.pimg.tw\/youyouyou\/1561483134-1874412385_n.png?w=1290&#038;ssl=1\" alt=\"\"\/><figcaption class=\"wp-element-caption\">L9110 Fan Motor \u7522\u54c1\u5716<\/figcaption><\/figure>\n\n\n\n<p>\u63a5\u7dda\u7684\u90e8\u4efd\u5982\u4e0b\u5716\uff0c\u5c07INB\u3001INA\u5206\u5225\u63a5\u5728GPIO16\u53caGPIO4\u4e0a\uff0cINB\u8207INA\u4ee3\u8868\u6b63\u53cd\u8f49\uff0c\u5176\u8f38\u51fa\u8207\u7d50\u679c\u5982\u4e0b\u8868<\/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\/1561483634-2799575691_n.png?resize=841%2C576&#038;ssl=1\" alt=\"\" width=\"841\" height=\"576\"\/><figcaption class=\"wp-element-caption\">L9110 Fan Motor \u9023\u63a5\u5716\u793a<\/figcaption><\/figure>\n\n\n\n<p>\u6578\u4f4d\u8f38\u51fa\u6642\u7d50\u679c<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">INA<\/th><th class=\"has-text-align-center\" data-align=\"center\">INB<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u7d50\u679c<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6b63\u8f49<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u53cd\u8f49<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u505c\u6b62<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">HIGH<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u505c\u6b62<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u985e\u6bd4\u8f38\u51fa\u8981\u6ce8\u610f\u7684\u5730\u65b9\u5247\u662f\uff0c\u7531\u65bc\u96fb\u58d3\u592a\u4f4e\u901a\u5e38\u7121\u6cd5\u555f\u52d5\uff0c\u6240\u4ee5\u5148\u628aoutput\u52a0\u5230255\uff0c\u518d\u5f9e80\u9010\u6f38\u4e0a\u5347\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">INA<\/th><th class=\"has-text-align-center\" data-align=\"center\">INB<\/th><th class=\"has-text-align-center\" data-align=\"center\">\u7d50\u679c<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">80\uff5e255<\/td><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6162\u6162\u589e\u52a0\u6b63\u8f49<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">LOW<\/td><td class=\"has-text-align-center\" data-align=\"center\">80\uff5e255<\/td><td class=\"has-text-align-center\" data-align=\"center\">\u6162\u6162\u589e\u52a0\u53cd\u8f49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>int INA = 16; \nint INB = 4;\n\nvoid setup() \n{ \n  Serial.begin(9600);\n  pinMode(INA,OUTPUT); \n  pinMode(INB,OUTPUT); \n  \/\/digital output test\n  Serial.println(\"INA=LOW,INB=HIGH\");\n  digitalWrite(INA,LOW);\n  digitalWrite(INB,HIGH); \n  delay(3000); \n  Serial.println(\"INA=HIGH,INB=LOW\");\n  digitalWrite(INA,HIGH);\n  digitalWrite(INB,LOW); \n  delay(3000); \n  Serial.println(\"INA=LOW,INB=LOW\");\n  digitalWrite(INA,LOW);\n  digitalWrite(INB,LOW); \n  delay(3000); \n  Serial.println(\"INA=HIGH,INB=HIGH\");\n  digitalWrite(INA,HIGH);\n  digitalWrite(INB,HIGH); \n  delay(3000); \n  \/\/analog output(PWM) test  \n  ledcSetup(0, 5000, 8);\n  ledcSetup(1, 5000, 8);\n  ledcAttachPin(INA, 0);\n  ledcAttachPin(INB, 1);  \n  \/\/prepare forward\n  Serial.println(\"INA=i++,INB=LOW\");\n  ledcWrite(1, 0);\n  ledcWrite(0, 255);\/\/\u907f\u514d\u96fb\u6d41\u592a\u4f4e\u7121\u6cd5\u555f\u52d5\n  delay(300);\n  for (int i=80;i&lt;=255;i++)\n  {\n    ledcWrite(0, i);\n    ledcWrite(1, 0);\n    Serial.println(i);\n    delay(100);\n  }\n  ledcWrite(0, 0);\n  ledcWrite(1, 0);\n  delay(3000); \n  \/\/prepare reverse\n  Serial.println(\"INA=LOW,INB=i++\");\n  ledcWrite(0, 0);\n  ledcWrite(1, 255);\/\/\u907f\u514d\u96fb\u6d41\u592a\u4f4e\u7121\u6cd5\u555f\u52d5\n  delay(300);\n  for (int i=80;i&lt;=255;i++)\n  {\n    ledcWrite(1, i);\n    ledcWrite(0, 0);\n    Serial.println(i);\n    delay(100);\n  }\n  ledcWrite(0, 0);\n  ledcWrite(1, 0);\n    delay(3000); \n}\n \nvoid loop() \n{ \n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ESP32\u4ec0\u9ebc\u90fd\u597d\uff0c\u4f46\u662fPWM\u662f\u4e00\u500b\u554f\u984c\uff0c\u50b3\u7d71UNO\u53ef\u4ee5\u7528analogWrite\u7c21\u55ae\u7684\u5be6\u73fe\u985e\u6bd4\u8f38\u51fa\uff0c\u50cf\u662f\u8b93LE [&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-654","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\/654","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=654"}],"version-history":[{"count":1,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/654\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/posts\/654\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/media?parent=654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/categories?post=654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nmking.io\/index.php\/wp-json\/wp\/v2\/tags?post=654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}