{"id":84166,"date":"2019-04-18T14:03:13","date_gmt":"2019-04-18T14:03:13","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=84166"},"modified":"2019-04-20T10:51:51","modified_gmt":"2019-04-20T10:51:51","slug":"esp32-esp8266-pwm-micropython","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-pwm-micropython\/","title":{"rendered":"ESP32\/ESP8266 PWM with MicroPython &#8211; Dim LED"},"content":{"rendered":"\n<p>This tutorial shows how to generate PWM signals with the ESP32 and ESP8266 boards using MicroPython firmware. As an example, we&#8217;ll dim the brightness of an LED by changing the duty cycle over time.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32\/ESP8266 PWM with MicroPython Dim LED\" class=\"wp-image-84169\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need to have MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>For this example, wire an LED to your ESP board. We\u2019ll wire the LED to <span class=\"rnthl rntcblue\">GPIO 5<\/span> in both boards, but you can choose another suitable PWM pin. See the best pins to use in our <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts required<\/h3>\n\n\n\n<p>Here&#8217;s a list of the parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a>&nbsp;or&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a>&nbsp;(read:&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>)<\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\">5mm LED<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">330 Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic \u2013 ESP32<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP32 board:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1065\" height=\"719\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp32_bb.png?resize=1065%2C719&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with MicroPython circuit to fade Dim LED\" class=\"wp-image-84167\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp32_bb.png?w=1065&amp;quality=100&amp;strip=all&amp;ssl=1 1065w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp32_bb.png?resize=300%2C203&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp32_bb.png?resize=768%2C518&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp32_bb.png?resize=1024%2C691&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1065px) 100vw, 1065px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic \u2013 ESP8266<\/h3>\n\n\n\n<p>Follow the next schematic diagram if you\u2019re using an ESP8266 board:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"984\" height=\"537\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp8266_bb.png?resize=984%2C537&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 PWM with MicroPython circuit to fade Dim LED\" class=\"wp-image-84168\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp8266_bb.png?w=984&amp;quality=100&amp;strip=all&amp;ssl=1 984w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp8266_bb.png?resize=300%2C164&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/PWM_esp8266_bb.png?resize=768%2C419&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 984px) 100vw, 984px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Script<\/h2>\n\n\n\n<p>Here\u2019s the script that changes the LED brightness over time by increasing the duty cycle. This script works with the ESP32 and ESP8266.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nfrom machine import Pin, PWM\nfrom time import sleep\n\nfrequency = 5000\nled = PWM(Pin(5), frequency)\n\nwhile True:\n  for duty_cycle in range(0, 1024):\n    led.duty(duty_cycle)\n    sleep(0.005)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/PWM\/pwm_basic.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>To create a PWM pin, import the <span class=\"rnthl rntliteral\">PWM<\/span> class in addition to the <span class=\"rnthl rntliteral\">Pin<\/span> class from the <span class=\"rnthl rntliteral\">machine<\/span> module.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>from machine import Pin, PWM<\/code><\/pre>\n\n\n\n<p>Then, create a <span class=\"rnthl rntliteral\">PWM<\/span> object called <span class=\"rnthl rntliteral\">led<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = PWM(Pin(5), frequency)<\/code><\/pre>\n\n\n\n<p>To create a <span class=\"rnthl rntliteral\">PWM<\/span> object, you need to pass as parameters, the pin it is connected to, the signal frequency and the duty cycle.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Frequency<\/strong>: The frequency can be a value between 0 and 78125. A frequency of 5000 Hz can be used to control the LED brightness.<\/li><li><strong>Duty cycle<\/strong>: The duty cycle can be a value between 0 and 1023. In which 1023 corresponds to 100% duty cycle (full brightness), and 0 corresponds to 0% duty cycle (unlit LED).<\/li><\/ul>\n\n\n\n<p>We\u2019ll just set the duty cycle on the <span class=\"rnthl rntliteral\">while<\/span> loop, so we don\u2019t need to pass the duty cycle parameter. If you don\u2019t set the duty cycle when instantiating the <span class=\"rnthl rntliteral\">PWM<\/span> object, it will be 0 by default.<\/p>\n\n\n\n<p>To set the duty cycle use the <span class=\"rnthl rntliteral\">duty()<\/span> method on the <span class=\"rnthl rntliteral\">PWM<\/span> object and pass the duty cycle as an argument:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.duty(duty_cycle)<\/code><\/pre>\n\n\n\n<p>Inside the <span class=\"rnthl rntliteral\">while<\/span> loop, we create a <span class=\"rnthl rntliteral\">for<\/span> loop that increases the duty cycle by 1 in each loop with an interval of 5 ms between each change.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>for duty_cycle in range(0, 1024):\n    led.duty(duty_cycle)\n    sleep(0.005)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">range()<\/span> function has the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(start, stop, step)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Start<\/strong>: a number that specifies at which position to start. We want to start with 0 duty cycle;<\/li><li><strong>Stop<\/strong>: a number that specifies at which position we want to stop, excluding that value. The maximum duty cycle is 1023, because we are incrementing 1 in each loop, the last value should be 1023+1. So, we\u2019ll use 1024.<\/li><li><strong>Step<\/strong>: an integer number that specifies the incrementation. By default, incrementation is 1.<\/li><\/ul>\n\n\n\n<p>In each for loop, we set the LED\u2019s duty cycle to the current <span class=\"rnthl rntliteral\">duty_cycle<\/span> value:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.duty(duty_cycle)<\/code><\/pre>\n\n\n\n<p>After that, the <span class=\"rnthl rntliteral\">duty_cycle<\/span> variable is incremented by 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Save the code to your ESP board using <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Thonny IDE<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">uPyCraft IDE<\/a>. The LED attached to <span class=\"rnthl rntcblue\">GPIO 5<\/span> should increase brightness over time.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-led-fade.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32\/ESP8266 PWM with MicroPython circuit to fade Dim LED\" class=\"wp-image-84172\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-led-fade.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-led-fade.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you\u2019ve found this MicroPython tutorial useful. If you want to learn more about MicroPython, make sure you take a look at our eBook: <strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a><\/strong>.<\/p>\n\n\n\n<p>If you like MicroPython, you may also like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/\">ESP32\/ESP8266 GPIOs Explained with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-digital-inputs-digital-outputs-micropython\/\">ESP32\/ESP8266 Digital Inputs and Digital Outputs with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-analog-readings-micropython\/\">ESP32\/ESP8266 Analog Readings with MicroPython<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to generate PWM signals with the ESP32 and ESP8266 boards using MicroPython firmware. As an example, we&#8217;ll dim the brightness of an LED by changing the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266 PWM with MicroPython &#8211; Dim LED\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-pwm-micropython\/#more-84166\" aria-label=\"Read more about ESP32\/ESP8266 PWM with MicroPython &#8211; Dim LED\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":84169,"comment_status":"open","ping_status":"closed","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":""},"categories":[281,265,310,309,264],"tags":[],"class_list":["post-84166","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp8266-project","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/esp32-esp8266-pwm.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=84166"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/84166\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84169"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=84166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=84166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=84166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}