> For the complete documentation index, see [llms.txt](https://robotlabs.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://robotlabs.gitbook.io/docs/esp32/platformio/3.1-pwm-led-fade.md).

# 3.1 PWM - LED Fade

We have used LED to make a running light before. In this example, we will learn how to make a fading light with PWM. The brightness of the LED light will change as the PWM duty cycle.&#x20;

{% embed url="<https://youtu.be/VBuEXz_m4G4>" %}

## Introduction

PWM stands for Pulse-Width Modulation, a technique that uses a varying duty cycle to represent a signal as a rectangular wave. It's used to control the average amplitude or power of an electrical signal.

The two primary components of a Pulse Width Modulation (PWM) signal are frequency and duty cycle:&#x20;

* **Frequency**The rate at which a PWM signal switches between high and low voltages, measured in Hertz (Hz).
* **Duty cycle**The percentage of time a PWM signal is at a high voltage, expressed as a ratio or percentage.

<figure><img src="/files/3SKnXiaBybqWbPT9uUVA" alt="" width="563"><figcaption></figcaption></figure>

PWM can be used to control the brightness of an LED by adjusting the duty cycle. If We generate a PWM signal to the anode(+) pin, the LED's brightness is in proportion to PWM duty cycle. PWM duty cycle varies from 0 to 255. The bigger the PWM duty cycle is, the brighter the LED is.

<figure><img src="/files/qqjx9cjl2GOAa0Ctl5t6" alt="" width="375"><figcaption></figcaption></figure>

## Hardware Circuit

### 1. Parts Required

Here’s a list of the parts to you need to build the circuit:

* ESP32-S3-N16R8
* LED
* 330 - 1k Ohm resistor
* Breadboard
* Jumper wires

### 2. Wiring

Before proceeding, you need to wire a circuit with an LED. The positive pole of the LED is connected to the pin 14 of the development board, and a resistor is connected in series. The negative pole is connected to GND, as shown below:

<figure><img src="/files/dbE6KnGqYMbd4hjiGSde" alt="" width="361"><figcaption></figcaption></figure>

{% hint style="info" %}
Usually, it requires a resistor to protect LED from burning. The resistance's value depends on the LED's specification.
{% endhint %}

## Coding and Testing

### 1. Arduino functions for PWM

The `analogWrite()` function writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call on the same pin.&#x20;

```arduino
analogWrite(pin, value)
```

* pin: the Arduino pin to write to. Allowed data types: int
* value: the duty cycle between 0 (off) and 255 (on). Allowed data types: int

Copy the following code to your IDE.

```cpp
#include <Arduino.h>
// define GPIO pin number
#define LED_PIN   12

void setup() {
  // set GPIO pin
  pinMode(LED_PIN, OUTPUT);

}

void loop() {
  // Brighter control
  for(int i=0;i<256;i++) {
    // Set brightness
    analogWrite(LED_PIN, i);
    // Delay 10ms
    delay(10);
    }
  // Fading control
  for(int i=255;i>=0;i--) {
    // Set brightness as the analog value
    analogWrite(LED_PIN, i);
    // Delay 10ms
    delay(10);
    }
}

```

### 2. Uploading the Code

Refer the previous tutorial: [2.1 GPIO - LED Blink](/docs/esp32/platformio/2.1-gpio-led-blink.md)

## Summary

In this section, we use analogWrite() function writes an analog value (PWM wave) to a pin to change the LED brightness.

Finally, if you want to learn more about other controllers, take a look at our Youtube channel:

{% embed url="<https://www.youtube.com/@RobotLabs>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://robotlabs.gitbook.io/docs/esp32/platformio/3.1-pwm-led-fade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
