{"id":75663,"date":"2024-06-11T11:05:00","date_gmt":"2024-06-11T11:05:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=75663"},"modified":"2024-08-01T20:17:39","modified_gmt":"2024-08-01T20:17:39","slug":"esp32-pwm-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/","title":{"rendered":"ESP32 PWM with Arduino IDE (Analog Output)"},"content":{"rendered":"\n<p>Learn to generate PWM signals with the ESP32 using Arduino IDE. We&#8217;ll explain two different methods: using <span class=\"rnthl rntliteral\">analogWrite<\/span> and using the <span class=\"rnthl rntliteral\">LEDC API<\/span>. As an example, we&#8217;ll build a simple circuit to fade an LED.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE Analog Output LED\" class=\"wp-image-158727\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 11 June 2024<\/em><\/p>\n\n\n\n<p>Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>This tutorial is compatible with ESP32 board add-on version 3.X or above &#8211; learn more in our <a href=\"https:\/\/randomnerdtutorials.com\/esp32-migrating-version-2-to-3-arduino\/\">migration guide<\/a>.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Table of Contents<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#esp32-pwm-controller\" title=\"\">ESP32 PWM Controller<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#analogwrite\" title=\"\">analogWrite<\/a><\/li>\n\n\n\n<li><a href=\"#ledc-functions\" title=\"\">LEDC Functions<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#dim-led-esp32\" title=\"\">Dimming an LED with the ESP32<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-pwm-code-analogwrite\" title=\"\">ESP32 PWM Example using analogWrite &#8211; Code<\/a><\/li>\n\n\n\n<li><a href=\"#esp32-pwm-ledc-example-code\" title=\"\">ESP32 PWM Example using the LEDC API &#8211; Code<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need these parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DOIT DEVKIT V1 Board<\/a>&nbsp;&#8211; read<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\"> best ESP32 development boards<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noreferrer noopener\">3x 5mm LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">3x 330 Ohm resistor<\/a> (or similar values)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n<\/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<h2 class=\"wp-block-heading\" id=\"esp32-pwm-controller\">ESP32 LED PWM Controller<\/h2>\n\n\n\n<p>The ESP32 has an LED PWM controller with 6 to 16 independent channels (depending on the ESP32 model) that can be configured to generate PWM signals with different properties.<\/p>\n\n\n\n<p>There are different functions you can use to generate PWM signals and achieve the same results. You can use <span class=\"rnthl rntliteral\">analogWrite<\/span> (like in Arduino boards) or you can use LEDC functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"analogwrite\">analogWrite<\/h3>\n\n\n\n<p>The most basic function is <span class=\"rnthl rntliteral\">analogWrite<\/span> which accepts as arguments the GPIO where you want to generate the PWM signal and the duty cycle value (ranging from 0 to 255).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void analogWrite(uint8_t pin, int value);<\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void analogWrite(2, 180);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Set the Frequency and Resolution<\/h4>\n\n\n\n<p>You can set the resolution and frequency of the PWM signal on a selected pin by using the <span class=\"rnthl rntliteral\">analogWriteResolution<\/span> and <span class=\"rnthl rntliteral\">analogWriteFrequency<\/span> functions.<\/p>\n\n\n\n<p>To set the resolution:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void analogWriteResolution(uint8_t pin, uint8_t resolution);<\/code><\/pre>\n\n\n\n<p>To set the frequency:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void analogWriteFrequency(uint8_t pin, uint32_t freq);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ledc-functions\">LEDC Functions<\/h3>\n\n\n\n<p>Alternatively, you can use the Arduino-ESP32 LEDC API. First, you need to set up an LEDC pin. You can use the <span class=\"rnthl rntliteral\">ledcAttach<\/span> or <span class=\"rnthl rntliteral\">ledcAttachChannel<\/span> functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ledcAttach<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">ledcAttach<\/span> function sets up an LEDC pin with a given frequency and resolution. The LEDC channel will be selected automatically.<\/p>\n\n\n\n<pre id=\"codecell0\" class=\"wp-block-preformatted language-c\">bool ledcAttach(uint8_t pin, uint32_t<strong> <\/strong>freq, uint8_t resolution);<\/pre>\n\n\n\n<p>This function will return <span class=\"rnthl rntliteral\">true<\/span> if the configuration is successful. If <span class=\"rnthl rntliteral\">false<\/span> is returned, an error occurs and the LEDC channel is not configured.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ledcAttachChannel<\/h3>\n\n\n\n<p>If you prefer to set up the LEDC channel manually, you can use the <span class=\"rnthl rntliteral\">ledcAttachChannel<\/span> function instead.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel);<\/code><\/pre>\n\n\n\n<p>This function will return <span class=\"rnthl rntliteral\">true<\/span> if the configuration is successful. If <span class=\"rnthl rntliteral\">false<\/span> is returned, an error occurs and the LEDC channel is not configured.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ledcWrite<\/h3>\n\n\n\n<p>Finally, after setting the LEDC pin using one of the two previous functions, you use the <span class=\"rnthl rntliteral\">ledcWrite<\/span> function to set the duty cycle of the PWM signal.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void ledcWrite(uint8_t pin, uint32_t duty);<\/code><\/pre>\n\n\n\n<p>This function will return <span class=\"rnthl rntliteral\">true<\/span> if setting the duty cycle is successful. If <span class=\"rnthl rntliteral\">false<\/span> is returned, an error occurs and the duty cycle is not set.<\/p>\n\n\n\n<p>For more information and all functions of the LEDC PWM controller, <a href=\"https:\/\/espressif-docs.readthedocs-hosted.com\/projects\/arduino-esp32\/en\/latest\/api\/ledc.html\" target=\"_blank\" rel=\"noopener\" title=\"\">check the official documentation<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"dim-led-esp32\">Dimming an LED with the ESP32<\/h2>\n\n\n\n<p>To show you how to generate PWM signals with the ESP32, we&#8217;ll create two simple examples that dim the brightness of an LED (increase and decrease brightness over time). We&#8217;ll provide an example using <span class=\"rnthl rntliteral\">analogWrite<\/span> and another using the LEDC functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic<\/h3>\n\n\n\n<p>Wire an LED to your ESP32 as in the following schematic diagram. The LED should be connected to <span class=\"rnthl rntcorange\">GPIO 16<\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"700\" height=\"440\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/LED_PWM_Example.jpg?resize=700%2C440&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Dimming an LED with the ESP32\" class=\"wp-image-75667\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/LED_PWM_Example.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/LED_PWM_Example.jpg?resize=300%2C189&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure><\/div>\n\n\n<p><em>(This schematic uses the ESP32 DEVKIT V1 module version with 30 GPIOs \u2013 if you\u2019re using another model, please check the pinout for the board you\u2019re using.)<\/em><\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> You can use any pin you want, as long as it can act as an output. All pins that can act as outputs can be used as PWM pins. For more information about the ESP32 GPIOs, read: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"esp32-pwm-code-analogwrite\">ESP32 PWM Example using analogWrite &#8211; Code<\/h3>\n\n\n\n<p>Open your Arduino IDE and copy the following code. This example increases and decreases the LED brightness over time using the <span class=\"rnthl rntliteral\">analogWrite<\/span> function.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-pwm-arduino-ide\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n\/\/ the number of the LED pin\nconst int ledPin = 16;  \/\/ 16 corresponds to GPIO 16\n\nvoid setup() {\n  \/\/ set the LED as an output\n  pinMode(ledPin, OUTPUT);\n}\n\nvoid loop(){\n  \/\/ increase the LED brightness\n  for(int dutyCycle = 0; dutyCycle &lt;= 255; dutyCycle++){   \n    \/\/ changing the LED brightness with PWM\n    analogWrite(ledPin, dutyCycle);\n    delay(15);\n  }\n\n  \/\/ decrease the LED brightness\n  for(int dutyCycle = 255; dutyCycle &gt;= 0; dutyCycle--){\n    \/\/ changing the LED brightness with PWM\n    analogWrite(ledPin, dutyCycle);\n    delay(15);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_analogWrite.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You start by defining the pin the LED is attached to. In this example, the LED is attached to <span class=\"rnthl rntcorange\">GPIO 16<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int ledPin = 16;  \/\/ 16 corresponds to GPIO 16<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, you need to configure the LED as an output using the <span class=\"rnthl rntliteral\">pinMode()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(ledPin, OUTPUT);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, you vary the duty cycle between 0 and 255 to increase the LED brightness.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ increase the LED brightness\nfor(int dutyCycle = 0; dutyCycle &lt;= 255; dutyCycle++){   \n  \/\/ changing the LED brightness with PWM\n  analogWrite(ledPin, dutyCycle);\n  delay(15);\n}<\/code><\/pre>\n\n\n\n<p>Notice the use of the <span class=\"rnthl rntliteral\">analogWrite()<\/span> function to set up the duty cycle. You just need to pass as arguments the LED pin and the duty cycle.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>analogWrite(ledPin, dutyCycle);<\/code><\/pre>\n\n\n\n<p>Finally, we vary the duty cycle between 255 and 0 to decrease the brightness.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ decrease the LED brightness\nfor(int dutyCycle = 255; dutyCycle &gt;= 0; dutyCycle--){\n  \/\/ changing the LED brightness with PWM\n  analogWrite(ledPin, dutyCycle);\n  delay(15);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Upload the code to your ESP32. Make sure you have the right board and COM port selected. Look at your circuit. You should have a dimmer LED that increases and decreases brightness over time.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-2 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158721\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 0 LED off\" class=\"wp-image-158721\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158719\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 50\" class=\"wp-image-158719\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158720\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 100 maximum brightness\" class=\"wp-image-158720\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158718\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 75\" class=\"wp-image-158718\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"esp32-pwm-ledc-example-code\">ESP32 PWM Example using the LEDC API &#8211; Code<\/h3>\n\n\n\n<p>Open your Arduino IDE and copy the following code. This example increases and decreases the LED brightness over time using the ESP32 LEDC functions.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-pwm-arduino-ide\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.  \n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n*\/\n\n\/\/ 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 resolution = 8;\n \nvoid setup(){\n  \/\/ configure LED PWM\n  ledcAttach(ledPin, freq, resolution);\n  \n  \/\/ if you want to attach a specific channel, use the following instead\n  \/\/ledcAttachChannel(ledPin, freq, resolution, 0);\n}\n \nvoid loop(){\n  \/\/ increase the LED brightness\n  for(int dutyCycle = 0; dutyCycle &lt;= 255; dutyCycle++){   \n    \/\/ changing the LED brightness with PWM\n    ledcWrite(ledPin, dutyCycle);\n    delay(15);\n  }\n\n  \/\/ decrease the LED brightness\n  for(int dutyCycle = 255; dutyCycle &gt;= 0; dutyCycle--){\n    \/\/ changing the LED brightness with PWM\n    ledcWrite(ledPin, dutyCycle);   \n    delay(15);\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_ledcWrite.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You start by defining the pin the LED is attached to. In this example, the LED is attached to <span class=\"rnthl rntcorange\">GPIO 16<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int ledPin = 16;<\/code><\/pre>\n\n\n\n<p>Set the PWM properties: frequency and resolution.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ setting PWM properties\nconst int freq = 5000;\nconst int resolution = 8;<\/code><\/pre>\n\n\n\n<p>As we\u2019re using 8-bit resolution, the duty cycle will be controlled using a value from 0 to 255.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, set up the LEDC pin\u2014use the <span class=\"rnthl rntliteral\">ledcAttach<\/span> function as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>ledcAttach(ledPin, freq, resolution);<\/code><\/pre>\n\n\n\n<p>This will configure the LEDC pin with the frequency and resolution defined previously on a default PWM channel.<\/p>\n\n\n\n<p>If you want to set up the PWM channel yourself, you need to use <span class=\"rnthl rntliteral\">ledcAttachChannel<\/span> instead. The last argument of this function is the PWM channel number.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>ledcAttachChannel(ledPin, freq, resolution, 0);<\/code><\/pre>\n\n\n\n<p>Finally, in the <span class=\"rnthl rntliteral\">loop()<\/span>, you increase and decrease the brightness of the LED over time.<\/p>\n\n\n\n<p>The following lines increase the LED brightness.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ increase the LED brightness\nfor(int dutyCycle = 0; dutyCycle &lt;= 255; dutyCycle++){   \n  \/\/ changing the LED brightness with PWM\n  ledcWrite(ledPin, dutyCycle);\n  delay(15);\n}<\/code><\/pre>\n\n\n\n<p>Notice the use of the <span class=\"rnthl rntliteral\">ledcWrite()<\/span> function to set up the duty cycle. You just need to pass as arguments the LED pin and the duty cycle.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>ledcWrite(ledPin, dutyCycle);<\/code><\/pre>\n\n\n\n<p>Finally, we vary the duty cycle between 255 and 0 to decrease the brightness.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ decrease the LED brightness\nfor(int dutyCycle = 255; dutyCycle &gt;= 0; dutyCycle--){\n  \/\/ changing the LED brightness with PWM\n  ledcWrite(ledPin, dutyCycle);   \n  delay(15);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Upload the code to your ESP32. Make sure you have the right board and COM port selected. Look at your circuit. You should have a dimmer LED that increases and decreases brightness over time just like in the previous example.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-2 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158721\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 0 LED off\" class=\"wp-image-158721\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-0-off.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158719\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 50\" class=\"wp-image-158719\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-50.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158718\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 75\" class=\"wp-image-158718\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-75.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" data-id=\"158720\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 PWM with Arduino IDE- Analog Output LED Set to 100 maximum brightness\" class=\"wp-image-158720\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/06\/ESP32-PWM-with-Arduino-IDE-Analog-Output-LED-Set-to-100-max.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In summary, in this article, you learned how to use the LED PWM controller of the ESP32 with the Arduino IDE to dim an LED. The concepts learned can be used to control other outputs with PWM by setting the right properties to the signal.<\/p>\n\n\n\n<p>We have more ESP32 resources you may like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp32\/\" target=\"_blank\" rel=\"noreferrer noopener\">Getting Started with ESP32 Dev Module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-digital-inputs-outputs-arduino\/\" title=\"\">ESP32 Digital Inputs and Digital Outputs (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-adc-analog-read-arduino-ide\/\" title=\"\">ESP32 ADC \u2013 Read Analog Values with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-pir-motion-sensor-interrupts-timers\/\" title=\"\">ESP32 with PIR Motion Sensor using Interrupts and Timers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\" title=\"\"><strong>More ESP32 Tutorials and Projects<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p><em>This is an excerpt from our course:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a>. If you like ESP32 and you want to learn more, we recommend enrolling in&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE course<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn to generate PWM signals with the ESP32 using Arduino IDE. We&#8217;ll explain two different methods: using analogWrite and using the LEDC API. As an example, we&#8217;ll build a simple &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 PWM with Arduino IDE (Analog Output)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-pwm-arduino-ide\/#more-75663\" aria-label=\"Read more about ESP32 PWM with Arduino IDE (Analog Output)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":158727,"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":[276,281,277,299,264],"tags":[],"class_list":["post-75663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/10\/ESP32-PWM-Arduino-IDE.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/75663","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=75663"}],"version-history":[{"count":9,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/75663\/revisions"}],"predecessor-version":[{"id":161284,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/75663\/revisions\/161284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/158727"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=75663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=75663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=75663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}