{"id":132565,"date":"2023-08-23T11:09:30","date_gmt":"2023-08-23T11:09:30","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=132565"},"modified":"2023-09-20T14:18:30","modified_gmt":"2023-09-20T14:18:30","slug":"raspberry-pi-pico-pwm-analogwrite-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-analogwrite-arduino\/","title":{"rendered":"Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)"},"content":{"rendered":"\n<p>Learn how to generate PWM signals with the Raspberry Pi Pico using Arduino IDE. As an example, we\u2019ll show you how to dim the brightness of an LED by changing the duty cycle over time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-PWM-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Fading an LED using PWM with Arduino IDE\" class=\"wp-image-132568\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-PWM-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-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\/2023\/06\/Raspberry-Pi-Pico-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\/2023\/06\/Raspberry-Pi-Pico-PWM-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgreen\">We have a similar guide using MicroPython firmware:<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-micropython\/\" title=\"\"> Raspberry Pi Pico: PWM Fading an LED (MicroPython)<\/a>.<\/p>\n\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#rpi-pico-arduino-ide\" title=\"\">Install the Raspberry Pi Pico in Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"#introducing-pwm\" title=\"\">Introducing PWM<\/a><\/li>\n\n\n\n<li><a href=\"#rpi-pico-pwm-gpios\" title=\"\">Raspberry Pi Pico GPIOs and PWM<\/a><\/li>\n\n\n\n<li><a href=\"#pwm-machine-class\" title=\"\">analogWrite() and other functions<\/a><\/li>\n\n\n\n<li><a href=\"#fade-led-code-rpi-pico-arduino-ide\" title=\"\">Fading an LED with Raspberry Pi Pico &#8211; Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-pico-arduino-ide\">Prerequisites<\/h2>\n\n\n\n<p>You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven&#8217;t already:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\" title=\"\">Programming Raspberry Pi Pico with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introducing-pwm\">Introducing PWM (Pulse-Width Modulation)<\/h2>\n\n\n\n<p>PWM, or Pulse Width Modulation, is a technique that controls the power delivered to devices. It achieves this by turning the power on and off very quickly. <\/p>\n\n\n\n<p>For example, if you alternate an LED\u2019s voltage between HIGH and LOW very fast, your eyes can\u2019t keep up with the speed at which the LED switches on and off; you\u2019ll simply see some gradations in brightness. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"895\" height=\"182\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/led-fade.png?resize=895%2C182&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Fading LED\" class=\"wp-image-86976\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/led-fade.png?w=895&amp;quality=100&amp;strip=all&amp;ssl=1 895w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/led-fade.png?resize=300%2C61&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/led-fade.png?resize=768%2C156&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 895px) 100vw, 895px\" \/><\/figure><\/div>\n\n\n<p>That\u2019s basically how PWM works \u2014 by producing an output that changes between HIGH and LOW at a very high frequency.<\/p>\n\n\n\n<p>The duty cycle refers to the percentage of time the power is on compared to the total time of the on-off cycle\u2014take a look at the following diagram.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"711\" height=\"658\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/PWM-how-it-works.png?resize=711%2C658&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"How PWM works\" class=\"wp-image-86977\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/PWM-how-it-works.png?w=711&amp;quality=100&amp;strip=all&amp;ssl=1 711w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/07\/PWM-how-it-works.png?resize=300%2C278&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 711px) 100vw, 711px\" \/><\/figure><\/div>\n\n\n<p>By adjusting the duty cycle, we can control the average power delivered to the device. For instance, a higher duty cycle means the device receives more power, while a lower duty cycle results in less power. This enables us to control things like brightness in LEDs, speed in motors, or volume in speakers.<\/p>\n\n\n\n<p>For example, a duty cycle of 50 percent results in 50 percent LED brightness, a duty cycle of 0 means the LED is fully off, and a duty cycle of 100 means the LED is fully on. Changing the duty cycle is how you produce different levels of brightness.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rpi-pico-pwm-gpios\">Raspberry Pi Pico GPIOs<\/h2>\n\n\n\n<p>The Raspberry Pi Pico comes with 40 pins, 26 of which are programmable GPIOs that you can use to connect peripherals. All Raspberry Pi Pico GPIOs can output PWM signals\u2014they are marked in the diagrams in light green color.<\/p>\n\n\n\n<p>You can use the following pinouts as a reference to identify and locate each GPIO on your board. The pinout is slightly different for the Pico and Pico W.<\/p>\n\n\n\n<p>The following picture shows the Raspberry Pi Pico pinout (which functions are supported by each pin).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"842\" height=\"642\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=842%2C642&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Pinout\" class=\"wp-image-130916\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?w=842&amp;quality=100&amp;strip=all&amp;ssl=1 842w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=300%2C229&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-pinout.png?resize=768%2C586&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><figcaption class=\"wp-element-caption\">Image source: <a href=\"https:\/\/raspberrypi.com\" target=\"_blank\" rel=\"noopener\" title=\"\">raspberrypi.com<\/a><\/figcaption><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"620\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=861%2C620&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico W pinout\" class=\"wp-image-130917\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?w=861&amp;quality=100&amp;strip=all&amp;ssl=1 861w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=300%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-W-Pinout.png?resize=768%2C553&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/figure><\/div>\n\n\n<p>The pins marked in red are power pins that output 3.3V. The black pins are GND pins. All pins in light green can be used as &#8220;regular&#8221; GPIOs (input and output). <\/p>\n\n\n\n<p class=\"rntbox rntclgreen\">To learn more about the Pico Pinout, read the following guide:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-w-pinout-gpios\/\">Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Raspberry Pi Pico &#8211; PWM Channels<\/h3>\n\n\n\n<p>The Raspberry Pi Pico has <strong>8 independent PWM generators<\/strong> called slices. Each slice has two channels, which makes a total of <strong>16 PWM channels<\/strong>. <\/p>\n\n\n\n<p>The frequency of the PWM signal can range between 8Hz and 62.5MHz, while the microcontroller is running at a frequency of 125MHz.<\/p>\n\n\n\n<p>Two channels of the same slice run at the same frequency, but can have a different duty rate.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"pwm-analogwrite\">analogWrite() and Other Functions<\/h2>\n\n\n\n<p>When it comes to generating PWM signals on the Raspberry Pi Pico in an easy way using Arduino C\/C++, you can use the following functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">analogWriteFreq(uint32_t <strong>frequency<\/strong>)<\/span> to set the frequency of the PWM signal. It supports a frequency of 100Hz to 1MHz*.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">analogWriteRange(uint32_t <strong>range<\/strong>)<\/span> to set the range of the PWM signal \u2014 the maximum value you&#8217;ll use to set 100% duty cycle. It supports a value from 16 to 65535.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">analogWriteResolution (int <strong>resolution<\/strong>)<\/span> to set the resolution of the PWM signal \u2014 up to 16-bit.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">analogWrite(<strong>GPIO<\/strong>, <strong>duty cycle<\/strong>)<\/span> to output a PWM signal to a specified pin with a defined duty cycle. It continuously outputs a PWM signal until a <span class=\"rnthl rntliteral\">digitalWrite()<\/span> or other digital output is performed. <\/li>\n<\/ul>\n\n\n\n<p>* <em>there are some restrictions to the combination of frequency and ranges. According to the documentation: &#8220;The PWM generator source clock restricts the legal combinations of frequency and ranges. For example, at 1MHz only about 6 bits of range are possible. When you define an analogWriteFreq and analogWriteRange that can\u2019t be fulfilled by the hardware, the frequency will be preserved but the accuracy (range) will be reduced automatically. Your code will still send in the range you specify, but the core itself will transparently map it into the allowable PWM range<\/em>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wiring the Circuit<\/h2>\n\n\n\n<p>Now, let&#8217;s create an example to better understand those PWM concepts. We&#8217;ll create a simple example to dim an LED (increase and decrease brightness smoothly).<\/p>\n\n\n\n<p>Before proceeding, connect an LED to the Raspberry Pi Pico. We&#8217;re connecting to GPIO 20, but you can use any other GPIO.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-breadboard.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico LED PWM\" class=\"wp-image-132548\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-breadboard.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-breadboard.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Here\u2019s a list of the parts you need to build the circuit:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-pico-w\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Raspberry Pi Pico<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener\" title=\"\">5 mm LED<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener\" title=\"\">330 Ohm resistor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">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<h3 class=\"wp-block-heading\">Schematic \u2013 Raspberry Pi Pico<\/h3>\n\n\n\n<p>You can use the following diagram as a reference to connect the LED to the Raspberry Pi Pico board.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"681\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/raspberry-pi-pico-led-gpio20_bb.jpg?resize=500%2C681&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico LED connected to GPIO 20 diagram\" class=\"wp-image-132546\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/raspberry-pi-pico-led-gpio20_bb.jpg?w=500&amp;quality=100&amp;strip=all&amp;ssl=1 500w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/raspberry-pi-pico-led-gpio20_bb.jpg?resize=220%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 220w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"fade-led-code-rpi-pico-arduino-ide\">RPi Pico Fading an LED &#8211; Arduino Sketch<\/h2>\n\n\n\n<p>The following code creates a PWM signal on GPIO 20 and increases and decreases the duty cycle over time to dim the LED.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/raspberry-pi-pico-pwm-analogwrite-arduino\/\n  \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\/\/ Pin connected to the LED\nconst int ledPin = 20;\n\n\/\/ Time interval between brightness steps (in milliseconds)\nconst int fadeInterval = 10;\n\n\/\/ Maximum and minimum brightness values\nconst int maxBrightness = 255;\nconst int minBrightness = 0;\n\n\/\/ Current brightness value\nint brightness = minBrightness;\n\n\/\/ Direction flag for fading (true = increasing, false = decreasing)\nbool fadingDirection = true;\n\nvoid setup() {\n  \/\/ Initialize the LED pin as an output\n  pinMode(ledPin, OUTPUT);\n  \/\/ Adjust PWM properties if needed\n  \/*analogWriteFreq(5000);\n  analogWriteRange(65535);\n  analogWriteResolution(16);*\/\n}\n\nvoid loop() {\n  \/\/ Update the brightness value based on the fading direction\n  if (fadingDirection) {\n    for (int i = brightness; i &lt;= maxBrightness; i++) {\n      brightness = i;\n      Serial.println(brightness);\n      \n      \/\/ Set the LED brightness\n      analogWrite(ledPin, brightness);\n      \n      \/\/ Delay for the fade interval\n      delay(fadeInterval);\n    }\n    \n    fadingDirection = false;\n  } else {\n    for (int i = brightness; i &gt;= minBrightness; i--) {\n      brightness = i;\n      Serial.println(brightness);\n      \n      \/\/ Set the LED brightness\n      analogWrite(ledPin, brightness);\n      \n      \/\/ Delay for the fade interval\n      delay(fadeInterval);\n    }\n    \n    fadingDirection = true;\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\/Raspberry-Pi-Pico\/Arduino\/AnalogWrite_Fade_LED.ino\" 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>Here are the main steps you need to take into account when controlling an LED with PWM.<\/p>\n\n\n\n<p>First, define the pin that the LED is attached to. In this case, it is connected to GPIO 20. If you&#8217;re using a different GPIO number, change that on the code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int ledPin = 20;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, set 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\">setup()<\/span>, you can adjust the properties of the PWM signal using the <span class=\"rnthl rntliteral\">analogWriteFreq()<\/span>, <span class=\"rnthl rntliteral\">analogWriteRange()<\/span>, and <span class=\"rnthl rntliteral\">analogWriteResolution()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  \/\/ Adjust PWM properties if needed\n  \/*analogWriteFreq(5000);\n  analogWriteRange(65535);\n  analogWriteResolution(16);*\/<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t specify the range and resolution of the PWM signal, it will use an 8-bit resolution with a maximum range of 255 (100% duty cycle).<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, you increase and decrease the duty cycle to increase and decrease the LED brightness. The following line outputs the PWM signal with the defined duty cycle values.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>analogWrite(ledPin, brightness);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the Code to the Raspberry Pi Pico<\/h2>\n\n\n\n<p>For you to be able to upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.<\/p>\n\n\n\n<p>If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico Bootloader mode\" class=\"wp-image-130903\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/05\/Raspberry-Pi-Pico-Bottloader-mode.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.<\/p>\n\n\n\n<p>Now, select your COM port in <strong>Tools <\/strong>&gt; <strong>Port<\/strong>. It may be the case that the COM port is grayed out. If that&#8217;s the case, don&#8217;t worry it will automatically find the port once you hit the upload button.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"684\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?resize=595%2C684&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Raspberry Pi Pico - COM port not found Arduino IDE\" class=\"wp-image-132415\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?w=595&amp;quality=100&amp;strip=all&amp;ssl=1 595w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-COM-port-not-showing.png?resize=261%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 261w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n<p>Upload the code.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"30\" height=\"30\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-2-0-upload-button.png?resize=30%2C30&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino 2.0 Upload Button\" class=\"wp-image-103678\"\/><\/figure><\/div>\n\n\n<p>You should get a success message.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"249\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?resize=595%2C249&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Done uploading code Raspberry Pi Pico Arduino IDE\" class=\"wp-image-132414\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?w=595&amp;quality=100&amp;strip=all&amp;ssl=1 595w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/success-uploading-code-Rpi-pico.png?resize=300%2C126&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After uploading the code, the LED should start &#8220;breathing&#8221;. It will increase and decrease brightness over time gradually.<\/p>\n\n\n\n<div class=\"wp-block-jetpack-tiled-gallery aligncenter is-style-square\"><div class=\"tiled-gallery__gallery\"><div class=\"tiled-gallery__row columns-3\"><div class=\"tiled-gallery__col\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM0.jpg?resize=422%2C422&#038;strip=info&#038;ssl=1 422w\" alt=\"\" data-height=\"422\" data-id=\"132549\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=132549#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM0.jpg\" data-width=\"750\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM0.jpg?ssl=1&amp;resize=422%2C422\" data-amp-layout=\"responsive\"\/><\/figure><\/div><div class=\"tiled-gallery__col\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM1.jpg?resize=422%2C422&#038;strip=info&#038;ssl=1 422w\" alt=\"\" data-height=\"422\" data-id=\"132551\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=132551#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM1.jpg\" data-width=\"750\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM1.jpg?ssl=1&amp;resize=422%2C422\" data-amp-layout=\"responsive\"\/><\/figure><\/div><div class=\"tiled-gallery__col\"><figure class=\"tiled-gallery__item\"><img decoding=\"async\" srcset=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM2.jpg?resize=422%2C422&#038;strip=info&#038;ssl=1 422w\" alt=\"\" data-height=\"422\" data-id=\"132550\" data-link=\"https:\/\/randomnerdtutorials.com\/?attachment_id=132550#main\" data-url=\"https:\/\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM2.jpg\" data-width=\"750\" src=\"https:\/\/i1.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-LED-PWM2.jpg?ssl=1&amp;resize=422%2C422\" data-amp-layout=\"responsive\"\/><\/figure><\/div><\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>To wrap up, to output a PWM signal on a specified pin of the Raspberry Pi Pico, first, you need to declare that pin as an output. After that, you simply use the <span class=\"rnthl rntliteral\">analogWrite()<\/span> function and pass as arguments the GPIO number and the duty cycle.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you&#8217;re just getting started with the Raspberry Pi Pico, make sure you read our getting started guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-raspberry-pi-pico-w\/?preview=true&amp;_thumbnail_id=130915\" title=\"\">Getting Started with Raspberry Pi Pico (and Pico W)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/programming-raspberry-pi-pico-w-arduino-ide\/\">Programming Raspberry Pi Pico with Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-outputs-inputs-arduino\/\">Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-analog-inputs-arduino\/\" title=\"\">Raspberry Pi Pico: Read Analog Inputs (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/randomnerdtutorials.com\/projects-raspberry-pi-pico\/\">Check out all our Raspberry Pi Pico Guides \u00bb<\/a><\/p>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to generate PWM signals with the Raspberry Pi Pico using Arduino IDE. As an example, we\u2019ll show you how to dim the brightness of an LED by changing &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/raspberry-pi-pico-pwm-analogwrite-arduino\/#more-132565\" aria-label=\"Read more about Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":132568,"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":[324,325],"tags":[],"class_list":["post-132565","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi-pico","category-raspberry-pi-pico-arduino-ide"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2023\/06\/Raspberry-Pi-Pico-PWM-Arduino-IDE.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\/132565","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=132565"}],"version-history":[{"count":19,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132565\/revisions"}],"predecessor-version":[{"id":136660,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/132565\/revisions\/136660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/132568"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=132565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=132565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=132565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}