{"id":20501,"date":"2023-10-06T13:46:17","date_gmt":"2023-10-06T05:46:17","guid":{"rendered":"https:\/\/www.circuspi.com\/?p=20501"},"modified":"2023-10-30T13:32:03","modified_gmt":"2023-10-30T05:32:03","slug":"esp32-pwm","status":"publish","type":"post","link":"https:\/\/www.circuspi.com\/index.php\/2023\/10\/06\/esp32-pwm\/","title":{"rendered":"ESP32 \u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)"},"content":{"rendered":"\n<p><style>\n\/* ###\u5be6\u9a57\u4e0b\u4e00\u5217### *\/\n\na:link {color:#0695e4\n;}    \/* \u8a2d\u5b9a\u5c1a\u672a\u9ede\u95b1\u904e\u7684\u9023\u7d50\u6a23\u5f0f *\/\na:visited {color:#0695e4\n;} \/* \u8a2d\u5b9a\u904e\u53bb\u66fe\u7d93\u95b1\u904e\u7684\u9023\u7d50 *\/\na:hover {color:#0695e4\n;}   \/* \u8a2d\u5b9a\u6ed1\u9f20\u6e38\u6a19\u6307\u5728\u9023\u7d50\u4f4d\u7f6e\u4e0a\u7684\u6a23\u5f0f *\/\na:active {color:#0000BB;}  \/* \u8a2d\u5b9a\u4e0a\u9ede\u64ca\u904e\u7684\u9023\u7d50\u6a23\u5f0f *\/\n<\/style><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"526\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/10\/1000x526_1.jpg\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\n\" class=\"wp-image-20558\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u524d\u8a00<\/h2>\n\n\n\n<p>\u4e0a\u4e00\u7bc7\u8b1b\u5230\u4e86\u6578\u4f4d\u8f38\u51fa\u7684\u6982\u5ff5\uff0c\u8b93 ESP32 \u900f\u904e\u8f38\u51fa\u9ad8\u96fb\u4f4d\u548c\u4f4e\u96fb\u4f4d\u9ede\u4eae\u6216\u662f\u7184\u6ec5 LED \u71c8\u3002\u4f46\u662f\u6578\u4f4d\u8f38\u51fa\u5c31\u53ea\u80fd\u505a\u5230\u6253\u958b\u6216\u95dc\u9589\u55ce\uff1f\u5176\u5be6\u6578\u4f4d\u8f38\u51fa\u9084\u80fd\u505a\u51fa\u66f4\u591a\u8b8a\u5316\uff01\u8b93\u6211\u5011\u4f86\u770b\u770b\u6578\u4f4d\u8f38\u51fa\u7684\u9032\u968e\u7528\u6cd5\u2014PWM\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001PWM \u7c21\u4ecb<\/h2>\n\n\n\n<p>PWM\uff08Pulse-width modulation\uff0c\u8108\u6ce2\u5bec\u5ea6\u8abf\u8b8a\uff09\u662f\u4e00\u7a2e\u7528\u6578\u4f4d\u8a0a\u865f\u6a21\u64ec\u985e\u6bd4\u8a0a\u865f\u7684\u6280\u8853\uff0c\u4e5f\u662f\u76ee\u524d\u5e38\u898b\u7684\u6578\u4f4d\u529f\u7387\u63a7\u5236\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u4f86\u8aaa\uff0c\u6578\u4f4d\u8a0a\u865f\u53ea\u80fd\u8f38\u51fa\u9ad8\u96fb\u4f4d\uff08HIGH\uff09\u548c\u4f4e\u96fb\u4f4d\uff08LOW\uff09\u5169\u7a2e\u72c0\u614b\uff0c\u4e26\u4e14\u7576\u9ad8\u3001\u4f4e\u96fb\u4f4d\u5728\u56fa\u5b9a\u6642\u9593\u5468\u671f\u5167\u6709\u898f\u5f8b\u7684\u8b8a\u5316\u6642\uff0c\u6703\u7522\u751f\u5982\u4e0b\u5716\u822c\u7684\u6ce2\u5f62\uff0c\u50cf\u9019\u6a23\u7684\u6ce2\u5f62\u6211\u5011\u7a31\u4e4b\u70ba\u8108\u6ce2\uff08pulse\uff09\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1755\" height=\"500\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/06-\u9031\u671fA.jpg\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\" class=\"wp-image-20503\"\/><figcaption class=\"wp-element-caption\">\u8108\u6ce2\u8a0a\u865f<\/figcaption><\/figure><\/div>\n\n\n<p>\u800c PWM \u7684\u539f\u7406\u5c31\u662f\u5728\u4e0d\u6539\u8b8a\u6642\u9593\u5468\u671f\u7684\u689d\u4ef6\u4e0b\uff0c\u8abf\u6574\u9ad8\u96fb\u4f4d\u5728\u8108\u6ce2\u4e2d\u7684\u5bec\u5ea6\uff08\u4ea6\u5373\u901a\u96fb\u6642\u9593\u5728\u5468\u671f\u4e2d\u7684\u6bd4\u4f8b\uff09\uff0c\u5c31\u80fd\u8abf\u6574\u6578\u4f4d\u8f38\u51fa\u7684\u529f\u7387\uff0c\u56e0\u6b64\u7a31\u505a\u8108\u6ce2\u5bec\u5ea6\u8abf\u8b8a\u3002\u8209\u51e1 LED\u3001\u76f4\u6d41\u99ac\u9054\u3001RC \u4f3a\u670d\u99ac\u9054\u90fd\u53ef\u4ee5\u7528 PWM \u4f86\u63a7\u5236\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u8a08\u7b97 PWM<\/h3>\n\n\n\n<p>PWM \u7684\u8f38\u51fa\u5be6\u969b\u4e0a\u662f\u4e00\u500b 0~3.3v \u7684\u53ef\u8b8a\u96fb\u58d3\u8a0a\u865f\uff0c\u9019\u500b\u8a0a\u865f\u7531\u983b\u7387\u548c\u5de5\u4f5c\u5468\u671f\uff08duty cycle\uff09\u7d44\u6210\u3002\u6211\u5011\u77e5\u9053\uff0c\u983b\u7387\u662f\u5468\u671f\u7684\u5012\u6578\uff0c\u5468\u671f\u662f\u7531\u9ad8\u614b\u6642\u9593\u52a0\u4e0a\u4f4e\u614b\u6642\u9593\u7d44\u6210\uff1b\u5de5\u4f5c\u5468\u671f\u5247\u662f\u9ad8\u614b\u6642\u9593\u5728\u6574\u500b\u5468\u671f\u4e2d\u7684\u6240\u4f54\u7684\u6bd4\u4f8b\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1755\" height=\"501\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/07-\u9031\u671fB.jpg\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\" class=\"wp-image-20504\"\/><figcaption class=\"wp-element-caption\">\u5468\u671f\u3001\u983b\u7387\u8207\u5de5\u4f5c\u5468\u671f\u7684\u95dc\u4fc2<\/figcaption><\/figure><\/div>\n\n\n<p>\u7528\u516c\u5f0f\u8868\u793a\u7684\u8a71\u5c31\u6703\u662f\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/image-57.png\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\" class=\"wp-image-20505\" width=\"434\" height=\"176\"\/><\/figure><\/div>\n\n\n<p>\u6709\u4e86\u5de5\u4f5c\u5468\u671f\u7684\u6982\u5ff5\u5f8c\uff0c\u5c31\u80fd\u63db\u7b97\u51fa PWM \u8f38\u51fa\u7684\u5e73\u5747\u96fb\u58d3\u3002\u8b93\u6211\u5011\u770b\u4e00\u500b\u4f8b\u5b50\uff08\u8acb\u53c3\u8003\u4e0b\u5716\uff09\uff1a\u5047\u8a2d\u6709\u4e00\u500b\u5468\u671f 10ms\uff0c\u5de5\u4f5c\u5468\u671f 50%\u7684\u8108\u6ce2\u8a0a\u865f\uff0c\u6bcf\u500b\u5468\u671f\u7522\u751f\u7684\u5e73\u5747\u96fb\u58d3\u5c31\u662f 1.65V\u3002\u82e5\u5de5\u4f5c\u5468\u671f\u662f80%\uff0c\u5e73\u5747\u96fb\u58d3\u5c31\u6703\u662f 2.64V\u3002\u5de5\u4f5c\u5468\u671f\u8d8a\u9ad8\uff0c\u5e73\u5747\u8f38\u51fa\u96fb\u58d3\u5c31\u8d8a\u5927\uff0c\u76f8\u5c0d\u7684\u5e73\u5747\u8f38\u51fa\u529f\u7387\u4e5f\u6703\u8d8a\u5927\u3002<\/p>\n\n\n\n<p>\u800c\u7576\u5de5\u4f5c\u5468\u671f\u70ba 0% \u6216 100% \u6642\uff0c\u8f38\u51fa\u7684\u96fb\u58d3\u5c31\u6703\u662f 0V \u6216 3.3V\uff0c\u7b49\u540c\u65bc\u6578\u4f4d\u8f38\u51fa\u7684 LOW \u6216 HIGH\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/05-\u4eae\u5ea6\u8aaa\u660e.jpg\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\" class=\"wp-image-20506\" width=\"480\" height=\"552\"\/><figcaption class=\"wp-element-caption\">\u76f8\u540c\u9031\u671f\u4e2d\uff0c\u6539\u8b8a\u9ad8\u614b\u548c\u4f4e\u614b\u7684\u5360\u6bd4\u5c31\u80fd\u63a7\u5236LED\u767c\u51fa\u4e0d\u540c\u7684\u4eae\u5ea6\u3002<\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5728 ESP32 \u4e0a\u9762\u4f7f\u7528 LED PWM<\/h2>\n\n\n\n<p><strong>\ud83d\udca1 Tips<\/strong><\/p>\n\n\n\n<p>\u6a02\u946b\u70ba\u4e86\u8b93 ESP32 \u4e5f\u80fd\u4f7f\u7528 Arduino IDE \u958b\u767c\uff0c\u53e6\u5916\u958b\u767c\u4e86 Arduino-ESP32 \u9019\u500b\u6838\u5fc3\u51fd\u5f0f\u5eab\uff0c\u8a66\u5716\u505a\u5230\u50cf\u662f\u5728 Arduino \u539f\u751f\u74b0\u5883\u958b\u767c\u7684\u9ad4\u9a57\u3002 \u904e\u53bb Arduino-ESP32 \u53ea\u80fd\u7528<code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"> ledc <\/mark><\/code>\u624d\u80fd\u7522\u751f PWM \u8a0a\u865f\uff0c\u4f46\u5728 2.0.6 \u7248\u672c\u4ee5\u5f8c\uff0cArduino-ESP32 \u90fd\u80fd\u5920\u652f\u63f4 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">analogWrite<\/mark><\/code>\u3002<\/p>\n\n\n\n<p>ESP32 \u7e3d\u5171\u6709 16 \u500b LED PWM \u901a\u9053\uff0c\u6bcf\u4e00\u500b\u901a\u9053\u90fd\u53ef\u4ee5\u7368\u7acb\u63a7\u5236\uff0c\u9664\u4e86 GPIO 34, GPIO 35, GPIO 36, GPIO 39\uff08\u975e\u8f38\u51fa\u63a5\u8173\uff09\u4ee5\u53ca GPIO 6~11\uff0c\u5176\u4ed6 I\/O \u63a5\u8173\u90fd\u80fd\u7522\u751f PWM \u8a0a\u865f\uff0c\u53ef\u4ee5\u4efb\u610f\u9032\u884c\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\uff08\u8a3b\uff1a\u4f7f\u7528\u6642\u8acb\u8df3\u904e GPIO 6~11, GPIO 34, GPIO 35, GPIO 36, GPIO 39 \u9019\u5e7e\u96bb\u63a5\u8173\u3002\u539f\u56e0\u8acb\u898b<a href=\"https:\/\/www.circuspi.com\/index.php\/2023\/08\/30\/esp32-gpio-1\/\">\u7b2c\u4e09\u7bc7<\/a>\u3002\uff09<\/p>\n\n\n\n<p>LEDC \u7684\u914d\u7f6e\u6bd4\u8f03\u8907\u96dc\uff0c\u9700\u8981\u624b\u52d5\u914d\u7f6e\u591a\u500b\u53c3\u6578\uff0c\u4f46\u662f\u7528\u9014\u6bd4\u8f03\u5ee3\u6cdb\uff1banalogWrite \u51fd\u5f0f\u7684\u7528\u6cd5\u5247\u8207 Arduino \u76f8\u540c\uff0c\u4f7f\u7528\u6642\u53ea\u8981\u9078\u64c7 GPIO \u63a5\u8173\u4ee5\u53ca PWM \u6578\u503c\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u5c07\u4ecb\u7d39 LEDC \u4ee5\u53ca analogWrite \u5169\u7a2e API \u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">LEDC API<\/h3>\n\n\n\n<p><strong>\ud83d\udca1 Tips<\/strong><\/p>\n\n\n\n<p>ledc \u662f LED Control \u7684\u7e2e\u5beb\uff0c\u5b83\u4e3b\u8981\u8a2d\u8a08\u7528\u4f86\u63a7\u5236 LED\uff0c\u6240\u4ee5\u624d\u6703\u770b\u5230\u4ee5 ledc \u70ba\u958b\u982d\u7684\u8a9e\u6cd5\u3002<\/p>\n\n\n\n<p>LEDC API \u7684\u8a2d\u5b9a\u6bd4\u8f03\u8907\u96dc\uff0c\u5728\u914d\u7f6e\u6642\u9700\u8981\u5148\u8a2d\u5b9a\u4e09\u500b\u53c3\u6578\uff1a\u901a\u9053\u3001\u983b\u7387\u4ee5\u53ca\u89e3\u6790\u5ea6\uff0c\u63a5\u8457\u6307\u5b9a\u8981\u4f7f\u7528\u7684\u63a5\u8173\uff0c\u6700\u5f8c\u624d\u662f\u8f38\u51fa PWM \u8a0a\u865f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6b65\u9a5f<\/strong><\/h3>\n\n\n\n<p>1. \u5f9e 16 \u500b\u901a\u9053\u4e2d\u9078\u64c7\u4e00\u500b PWM \u901a\u9053\u4f7f\u7528\uff0c\u7de8\u865f\u5f9e 0 \u5230 15<\/p>\n\n\n\n<p>2. \u8a2d\u5b9a PWM \u983b\u7387\uff08LED\u4f7f\u75285kHz\u5373\u53ef\uff09<\/p>\n\n\n\n<p>3. \u8a2d\u5b9a Duty Cycle \u7684\u89e3\u6790\u5ea6 \u7576\u6211\u5011\u628a\u89e3\u6790\u5ea6\u8a2d\u6210 8 \u4f4d\u5143\u6642\uff0c\u4ee3\u8868 PWM \u7684\u8f38\u51fa\u6578\u503c\u7bc4\u570d\u5c31\u6703\u662f 0~255\u3002ESP32 \u7684\u89e3\u6790\u5ea6\u6700\u9ad8\u53ef\u4ee5\u5230 20 \u4f4d\u5143\uff0c\u89e3\u6790\u5ea6\u8d8a\u9ad8\uff0c\u5c31\u4ee3\u8868\u80fd\u63a7\u5236\u8d8a\u5fae\u5c0f\u7684\u8b8a\u5316\u91cf\u3002 <\/p>\n\n\n\n<p>\u901a\u9053\u3001\u983b\u7387\u8207\u89e3\u6790\u5ea6\u90fd\u8a2d\u5b9a\u597d\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u5c07\u9019\u4e9b\u53c3\u6578\u586b\u5165 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledSetup<\/mark><\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcSetup<\/mark>(channel, freq, resolution_bits); \/\/ \u8a2d\u5b9a\u901a\u9053\u3001\u983b\u7387\u8207\u89e3\u6790\u5ea6<\/code><\/pre>\n\n\n\n<p>4. \u6307\u5b9a\u8981\u8f38\u51fa\u7684 GPIO \u63a5\u8173\u8207 PWM \u901a\u9053<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ledcAttechPin(pin, channel); \/\/ \u8a2d\u5b9a\u63a5\u8173\u8207\u901a\u9053<\/code><\/pre>\n\n\n\n<p>5. \u5728\u6307\u5b9a\u7684PWM\u901a\u9053\u8f38\u51fa duty cycle<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWrite<\/mark>(channel, dutycycle); \/\/ \u8f38\u51faPWM\u8a0a\u865f\u9a45\u52d5LED<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">analogWrite API<\/h3>\n\n\n\n<p>\u76f8\u5c0d\u65bc LEDC\uff0canalogWrite() \u7684\u8a2d\u5b9a\u5c31\u7c21\u55ae\u8a31\u591a\u3002\u5b83\u4f7f\u7528\u7684\u65b9\u5f0f\u8207 Arduino \u8a9e\u8a00\u76f8\u540c\uff0c\u53ea\u9700\u8981\u8a2d\u5b9a GPIO \u63a5\u8173\u4ee5\u53ca PWM \u6578\u503c\uff08\u9810\u8a2d\u7bc4\u570d\u5f9e 0~255\uff09\u5c31\u80fd\u5728\u6307\u5b9a\u7684\u63a5\u8173\u8f38\u51fa\u5c0d\u61c9\u7684 PWM\u8a0a\u865f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">analogWrite<\/mark>(pin, value); \/\/ value\u7684\u9810\u8a2d\u7bc4\u570d\u662f0~255<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u60f3\u6539\u8b8a PWM \u8f38\u51fa 0~255 \u4ee5\u5916\u7684\u6578\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">analogWriteResolution<\/mark><\/code> \u4f86\u8abf\u6574\u89e3\u6790\u5ea6\uff0c\u6700\u9ad8\u53ef\u4ee5\u5230 20 \u4f4d\u5143\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>analogWriteResolution(bits); \/\/ \u9078\u64c7PWM\u901a\u9053\u7684\u89e3\u6790\u5ea6<\/code><\/pre>\n\n\n\n<p>\u8981\u4fee\u6539\u983b\u7387\u7684\u8a71\u5247\u662f\u7528 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">analogWriteFrequency<\/mark><\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">analogWriteFrequency<\/mark>(freq);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u7bc4\u4f8b 1: LED \u547c\u5438\u71c8<\/h2>\n\n\n\n<p><strong>\ud83d\udca1 Tips<\/strong><\/p>\n\n\n\n<p>\u6240\u6709\u80fd\u8a2d\u5b9a\u70ba\u8f38\u51fa\u7684\u63a5\u8173\u90fd\u53ef\u4ee5\u7522\u751f PWM \u8a0a\u865f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6750\u6599\u6e05\u55ae<\/h3>\n\n\n\n<ul>\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368030501866\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 \u958b\u767c\u677f<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368070100145\" target=\"_blank\" rel=\"noreferrer noopener\">micro USB \u50b3\u8f38\u7dda <\/a>x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/368090200053\" target=\"_blank\" rel=\"noreferrer noopener\">\u9eb5\u5305\u677f<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products\/3681101000174\" target=\"_blank\" rel=\"noreferrer noopener\">\u675c\u90a6\u7dda\uff08\u516c-\u516c\uff09<\/a> x2<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/products?query=5mm%20LED&amp;http_cookie=c4988d52-392e-47d1-b62d-2002bf800aa8\" target=\"_blank\" rel=\"noreferrer noopener\">5mm LED \u71c8<\/a> x1<\/li>\n\n\n\n<li><a href=\"https:\/\/www.icshop.com.tw\/categories\/%E6%8F%92%E6%9D%BF%E5%BC%8F%E9%9B%BB%E9%98%BBdip\" target=\"_blank\" rel=\"noreferrer noopener\">220\u03a9 ~ 1k\u03a9 \u96fb\u963b<\/a> x1<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9023\u63a5\u793a\u610f\u5716<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/image-58.png\" alt=\"ESP32\u6559\u5b78\u7cfb\u5217(\u4e94)\uff1a\u6578\u4f4d\u8108\u885d\u5bec\u5ea6\u8abf\u8b8a(PWM)\" class=\"wp-image-20508\" width=\"700\" height=\"641\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7a0b\u5f0f\u78bc<\/h3>\n\n\n\n<p>\u4e0b\u9762\u5169\u7a2e\u7248\u672c\u7684\u7a0b\u5f0f\u78bc\u90fd\u6709\u76f8\u540c\u7684\u8f38\u51fa\u7d50\u679c\uff0c\u70ba\u4e86\u65b9\u4fbf\u8b93\u8b80\u8005\u6bd4\u8f03\u5dee\u7570\uff0c\u7a0b\u5f0f\u78bc\u4f7f\u7528\u76f8\u540c\u7684\u7d50\u69cb\uff0c\u4e26\u4e14\u5206\u5225\u4f7f\u7528 LEDC \u548c analogWrite \u7522\u751f PWM \u8a0a\u865f\u3002<\/p>\n\n\n\n<div style=\"overflow: auto; height: auto; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/f509b0a06ee13664844fa084a5331a83.js\"><\/script>\n<\/div>\n\n\n\n<div style=\"overflow: auto; height: auto; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/df83c6c507b360212c15467cfa0a1c07.js\"><\/script>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u8aaa\u660e<\/h3>\n\n\n\n<ul>\n<li>\u5169\u7a2e\u7bc4\u4f8b\u7684\u8f38\u51fa\u7d50\u679c\u90fd\u76f8\u540c\u3002\u5728 LEDC \u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u901a\u9053 0\uff0c5kHz \u7684\u983b\u7387\u53ca 8 \u4f4d\u5143\u7684\u89e3\u6790\u5ea6\u3002\u800c analogWrite \u7684\u9810\u8a2d\u89e3\u6790\u5ea6\u5c31\u662f 8 \u4f4d\u5143(0~255)\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u984d\u5916\u8a2d\u5b9a\u3002<\/li>\n\n\n\n<li>\u6ce8\u610f ledcWrite \u7684\u8f38\u51fa\u7684\u53c3\u6578\u662f\u901a\u9053\uff0canalogWrite \u8f38\u51fa\u7684\u53c3\u6578\u662f\u63a5\u8173\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u60f3\u52a0\u5feb\u547c\u5438\u71c8\u7684\u8b8a\u5316\u901f\u5ea6\uff0c\u53ef\u4ee5\u589e\u52a0 fade_amount \u7684\u6578\u503c\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u89ba\u5f97\u547c\u5438\u71c8\u7684\u6548\u679c\u4e0d\u5920\u660e\u986f\uff0c\u53ef\u4ee5\u8a66\u8457\u52a0\u5927\u96fb\u963b\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f38\u51fa\u7d50\u679c<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"296\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/IMG_1863.gif\" alt=\"\" class=\"wp-image-20509\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u9032\u968e\u529f\u80fd: \u8b93\u8702\u9cf4\u5668\u767c\u51fa\u8072\u97f3<\/h2>\n\n\n\n<p>\u96d6\u7136\u524d\u9762\u63d0\u5230 ledc \u662f\u5c08\u9580\u8a2d\u8a08\u7528\u4f86\u63a7\u5236 LED \u71c8\u7684\u8a9e\u6cd5\uff0c\u4f46\u4e8b\u5be6\u4e0a\u5b83\u9084\u6709\u8b93\u8702\u9cf4\u5668\u767c\u51fa\u8072\u97f3\u7684\u529f\u80fd\u3002\u5728 Arduino-ESP32 \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u7528 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteTone<\/mark><\/code> \u548c <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteNote<\/mark><\/code> \u9019\u5169\u500b\u8a9e\u6cd5\u4f86\u767c\u51fa\u8072\u97f3\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8a9e\u6cd5\u8aaa\u660e<\/h3>\n\n\n\n<p><strong>ledcWriteTone<\/strong><\/p>\n\n\n\n<p>\u9019\u500b\u51fd\u5f0f\u6703\u5728\u63a5\u8173\u4e0a\u7522\u751f\u6307\u5b9a\u983b\u7387\uff0c\u5de5\u4f5c\u9031\u671f\u70ba 50% \u7684\u65b9\u6ce2\uff0c\u5b83\u8207 Arduino Tone() \u76f8\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteTone<\/mark>(channel, freq); \/\/\u9078\u64c7PWM\u901a\u9053\u548c\u983b\u7387<\/code><\/pre>\n\n\n\n<p><strong>ledcWriteNote<\/strong><\/p>\n\n\n\n<p>\u9019\u500b\u51fd\u5f0f\u7528\u4f86\u8a2d\u5b9a\u901a\u9053\u767c\u51fa\u7279\u5b9a\u7684\u97f3\u7b26(note)\u8207\u97f3\u7b26\u7684\u516b\u5ea6(\u53ef\u4ee5\u8a2d\u5b9a0~8)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteNote<\/mark>(channel, note, octave);<\/code><\/pre>\n\n\n\n<p>note \u53c3\u6578\u53ef\u4ee5\u586b\u5165\u7684\u5167\u5bb9\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>NOTE_C<\/td><td>NOTE_Cs<\/td><td>NOTE_D<\/td><td>NOTE_Eb<\/td><td>NOTE_E<\/td><td>NOTE_F<\/td><\/tr><tr><td>NOTE_Fs<\/td><td>NOTE_G<\/td><td>NOTE_Gs<\/td><td>NOTE_A<\/td><td>NOTE_Bb<\/td><td>NOTE_B<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e0b\u8868\u662f\u97f3\u7b26(note)\u8207\u516b\u5ea6(octave)\u7684\u5c0d\u61c9\u983b\u7387\uff0c\u5176\u4e2d\uff0c\u6a19\u793a\u70ba\u9ec3\u8272\u5340\u584a\u7684\u662f\u6a19\u6e96\u97f3\u9ad8 440Hz\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/image-59.png\" alt=\"\" class=\"wp-image-20510\" width=\"960\" height=\"507\"\/><figcaption class=\"wp-element-caption\">\u97f3\u7b26\u8207\u8072\u97f3\u983b\u7387\u7684\u5c0d\u61c9\u95dc\u4fc2\u8868<\/figcaption><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u8a2d\u5b9a\u6d41\u7a0b<\/h3>\n\n\n\n<p>\u8981\u4f7f\u7528 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><code>ledcWriteTone<\/code> <\/mark>\u548c <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><code>ledcWriteNote<\/code> <\/mark>\u8b93\u8702\u9cf4\u5668\u9806\u5229\u767c\u51fa\u8072\u97f3\uff0c\u8a2d\u5b9a\u7684\u65b9\u6cd5\u8207\u7bc4\u4f8b1\u7684 LEDC\u975e\u5e38\u985e\u4f3c\u3002<\/p>\n\n\n\n<ol>\n<li>\u9078\u64c7\u901a\u9053\u8207 GPIO \u63a5\u8173<\/li>\n\n\n\n<li>\u4f7f\u7528 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcAttachPin<\/mark><\/code> \u8a2d\u5b9a\u63a5\u8173\u8207\u901a\u9053<\/li>\n\n\n\n<li>\u9078\u64c7 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteTone<\/mark><\/code> \u6216 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteNote<\/mark><\/code> \u8f38\u51fa\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\ud83d\udca1 Tips<\/strong><\/p>\n\n\n\n<p>\u5c07 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><code>ledcWrite<\/code><\/mark> \u642d\u914d\u4e0a\u9762\u5169\u500b\u8a9e\u6cd5\u4f7f\u7528\uff0c\u8abf\u6574 PWM \u5de5\u4f5c\u5468\u671f\u5c31\u80fd\u6539\u8b8a\u8f38\u51fa\u97f3\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u7bc4\u4f8b 2: \u8b93\u8702\u9cf4\u5668\u7522\u751f\u97f3\u968e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6750\u6599\u6e05\u55ae<\/h3>\n\n\n\n<ul>\n<li>ESP32\u958b\u767c\u677f x1<\/li>\n\n\n\n<li>micro USB \u50b3\u8f38\u7dda x1<\/li>\n\n\n\n<li>\u9eb5\u5305\u677f x1<\/li>\n\n\n\n<li>\u675c\u90a6\u7dda\uff08\u516c-\u516c\uff09 x2<\/li>\n\n\n\n<li>\u7121\u6e90\u8702\u9cf4\u5668<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9023\u63a5\u793a\u610f\u5716<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/image-60.png\" alt=\"\" class=\"wp-image-20511\" width=\"522\" height=\"475\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7a0b\u5f0f\u78bc<\/h3>\n\n\n\n<p>\u96d6\u7136\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u7528\u9663\u5217\u4f86\u5beb\u6703\u66f4\u7c21\u6f54\uff0c\u4f46\u6211\u5011\u9084\u662f\u4e00\u9805\u4e00\u9805\u5217\u51fa\u4f86\uff0c\u8b93\u7a0b\u5f0f\u78bc\u53ef\u4ee5\u66f4\u5bb9\u6613\u7684\u88ab\u95b1\u8b80\u3002<\/p>\n\n\n\n<div style=\"overflow: auto; height: auto; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/896a3bd38e89ff153b2260c27d2f034d.js\"><\/script>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u7bc4\u4f8b3: toneMelody<\/h2>\n\n\n\n<p>\u5728 Arduino \u5167\u5efa\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6709\u4e00\u9805\u540d\u70ba toneMelody \u7684\u7a0b\u5f0f\u7bc4\u4f8b\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.circuspi.com\/wp-content\/uploads\/2023\/09\/image-61.png\" alt=\"\" class=\"wp-image-20512\" width=\"960\" height=\"746\"\/><figcaption class=\"wp-element-caption\">\u5167\u5efa\u7bc4\u4f8b\u7a0b\u5f0f\u7684\u4f4d\u7f6e<\/figcaption><\/figure><\/div>\n\n\n<p>\u4e0b\u9762\u7684\u7a0b\u5f0f\u662f\u7528 <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteNote<\/mark><\/code> \u548c <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ledcWriteTone<\/mark><\/code> \u91cd\u5beb\u904e\u7684\u7248\u672c\uff0c\u4e0d\u904e\u5728\u5be6\u969b\u6e2c\u8a66\u5f8c\uff0c\u7b46\u8005\u767c\u73fe ESP32 \u73fe\u5728\u4e5f\u652f\u63f4 tone() \u51fd\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u57f7\u884c\u5167\u5efa\u7684 toneMelody \u7bc4\u4f8b\uff0c\u4e0b\u9762\u7684\u7a0b\u5f0f\u5c31\u63d0\u4f9b\u7d66\u6709\u8208\u8da3\u7684\u8b80\u8005\u53c3\u8003\u3002<\/p>\n\n\n\n<p>\u9019\u500b\u7a0b\u5f0f\u7684\u63a5\u7dda\u8207\u7bc4\u4f8b 2 \u76f8\u540c\uff0c\u56e0\u6b64\u4e0d\u5fc5\u4fee\u6539\u786c\u9ad4\u90e8\u5206\u3002\u9019\u90e8\u5206\u7684\u7a0b\u5f0f\u8f03\u8907\u96dc\uff0c\u53ef\u4ee5\u53c3\u8003\u5167\u5efa\u7bc4\u4f8b\u4e2d\u7684\u8aaa\u660e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7a0b\u5f0f\u78bc<\/h3>\n\n\n\n<div style=\"overflow: auto; height: auto; background-color: #0000;\"><script src=\"https:\/\/gist.github.com\/iCShop-Sophie\/adb617e42ed893c0aa279f46daea1892.js\"><\/script>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u5c0f\u7d50<\/h2>\n\n\n\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u8aaa\u660e\u4e86\u5982\u4f55\u914d\u7f6e ESP32 \u7684 PWM \u53c3\u6578\uff0c\u4ee5\u53ca\u5728 Arduino IDE \u7522\u751f LED PWM \u8a0a\u865f\uff0c\u9084\u6709\u8b93\u7121\u6e90\u8702\u9cf4\u5668\u64ad\u653e\u97f3\u8abf\u7684\u65b9\u6cd5\u3002\u4e0b\u7bc7\u5c07\u8aaa\u660e\u5982\u4f55\u4f7f\u7528\u5167\u5efa\u7684 ADC \u8b80\u53d6\u985e\u6bd4\u6578\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u8b1b\u5230\u4e86\u6578\u4f4d\u8f38\u51fa\u7684\u6982\u5ff5\uff0c\u8b93 ESP32 \u900f\u904e\u8f38\u51fa\u9ad8\u96fb\u4f4d\u548c\u4f4e\u96fb\u4f4d\u9ede\u4eae\u6216\u662f\u7184\u6ec5 LED \u71c8\u3002\u4f46\u662f\u6578\u4f4d\u8f38\u51fa\u5c31\u53ea\u80fd\u505a\u5230\u6253\u958b\u6216\u95dc\u9589\u55ce\uff1f\u5176\u5be6\u6578\u4f4d\u8f38\u51fa\u9084\u80fd\u505a\u51fa\u66f4\u591a\u8b8a\u5316\uff01\u8b93\u6211\u5011\u4f86\u770b\u770b\u6578\u4f4d\u8f38\u51fa\u7684\u9032\u968e\u7528\u6cd5\u2014PWM\u3002<\/p>\n","protected":false},"author":1,"featured_media":20559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[235,238],"tags":[],"table_tags":[],"_links":{"self":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20501"}],"collection":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/comments?post=20501"}],"version-history":[{"count":18,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20501\/revisions"}],"predecessor-version":[{"id":20737,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/posts\/20501\/revisions\/20737"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media\/20559"}],"wp:attachment":[{"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/media?parent=20501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/categories?post=20501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/tags?post=20501"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.circuspi.com\/index.php\/wp-json\/wp\/v2\/table_tags?post=20501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}