ESP32 S3 UNO - Fade LED

This ESP32 S3 Uno fade LED tutorial shows you how to make an LED slowly get brighter and dimmer with Arduino code. You will wire one LED to the ESP32 S3 Uno form board and change its brightness with a PWM signal. The result is a smooth "breathing" light, and you will learn how to dim an LED both with and without blocking the rest of your program.

In this tutorial, you will:

  1. Wire an LED and a resistor to a PWM pin of the ESP32 S3 Uno
  2. Change LED brightness with the analogWrite() function
  3. Fade an LED in and out using delay()
  4. Fade in LED and fade out LED over a set time using millis(), without delay()
ESP32 S3 Uno fade LED

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×LED Kit
1×LED (red)
1×LED Module
1×Alternatively, Button and LED Kit
1×220Ω Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED

Before you write any code, it helps to know how an LED is built and why its brightness can change. Once you understand these two ideas, the fade code will make sense right away.

Pinout

An LED has only two legs, and the direction matters. If you connect it the wrong way round, it will simply stay dark.

Cathode (-)

This is the shorter leg. Connect it to GND (0V).

Anode (+)

This is the longer leg. The voltage on this leg controls whether the LED is on, off, or somewhere in between.

LED Pinout

How It Works

With the cathode tied to GND, the anode decides what the LED does. Putting GND on the anode turns the LED off. Putting a supply voltage on the anode turns it fully on.

Brightness with PWM

A Pulse Width Modulation (PWM) signal switches the anode on and off very fast. Your eye sees the average, so the LED looks dimmer or brighter. In Arduino code, the PWM value goes from 0 to 255. A value of 0 works like GND, so the LED is off. A value of 255 works like a steady supply, so the LED is at full brightness. Values in between give you every step of dimming.

How LED Works

※ NOTE THAT:

Most LEDs need a resistor in series with the anode. It limits the current so the LED and the board pin are not damaged. The right value depends on your LED; 220 ohm is a safe choice for a common LED on the 3.3V ESP32 S3 Uno pins.

ESP32 S3 Uno - fade LED

To fade an LED, you need a pin that can output PWM. On the ESP32 S3 Uno, almost every Uno header pin supports PWM, so you have many choices. Connect the LED anode (through the resistor) to the chosen pin, connect the cathode to GND, and then drive that pin with a PWM value in your code.

The ESP32 S3 Uno pins work at 3.3V, not 5V. An LED still lights up well at 3.3V, but it may look a little less bright than on a 5V board.

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D2, A0, SDA…) and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

Connect the LED and the 220 ohm resistor to pin D9 as shown below.

The wiring diagram between ESP32 S3 Uno LED

This image is created using Fritzing. Click to enlarge image

LED Pin ESP32 S3 Uno Pin
Anode (+) through 220 ohm resistor D9 (GPIO46)
Cathode (-) GND

WARNING

D9 is GPIO46 on the ESP32 S3 Uno, and GPIO46 is a boot strapping pin. The board reads it at startup to pick the boot mode. An LED with a 220 ohm resistor to GND is normally fine, but if the board does not start or code will not upload, unplug the LED wire from D9, upload again, and then plug it back in. You can also move the LED to another PWM pin such as D7 (GPIO14) and change LED_PIN to 14 in the code.

How To Program

Fading an LED takes only two functions. You set the pin up once, and then you keep writing new brightness values to it.

  1. Make the pin an output with pinMode(). For pin D9 (GPIO46) on the ESP32 S3 Uno:
pinMode(46, OUTPUT);
  1. Send a PWM value to the pin with analogWrite():
analogWrite(46, brightness);

The brightness value can be anything from 0 (off) to 255 (full brightness).

ESP32 S3 Uno Code - Fade Example from Arduino IDE

This sketch is the classic Fade example, set up for the ESP32 S3 Uno. It raises the brightness by 5 steps every 30 milliseconds, and when it reaches 0 or 255 it turns around. The LED keeps fading up and down forever.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-fade-led */ #define LED_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to the LED int brightness = 0; // how bright the LED is int fade_step = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare GPIO46 (D9) to be an output: pinMode(LED_PIN, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of GPIO46 (D9): analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_step; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_step = -fade_step; } // wait for 30 milliseconds to see the dimming effect delay(30); }

Detailed Instructions

Follow these steps in order:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into the Arduino IDE.
  6. Upload the code by clicking the Upload button.
Arduino IDE Upload Code
  1. Watch the LED slowly get brighter, then dimmer, again and again.
  2. Tip: Change fade_step or the delay(30) value to make the fade faster or slower.

Code Explanation

Each line of the sketch has a comment that says what it does. The idea is simple: one variable holds the brightness, and another holds the step size. When the brightness hits an end, the step changes sign, so the fade turns around.

※ NOTE THAT:

This sketch uses delay() to slow the fade down. While delay() waits, the board can do nothing else, so you cannot read a button or a sensor at the same time. The next two examples use millis() instead. They fade the LED over a fixed time and keep the rest of the program free to run.

How to fade-out LED in a period without using delay()

This sketch dims the LED from full brightness to off over 3 seconds, then starts again. It checks how much time has passed with millis() and turns that time into a brightness with map(), so the loop never stops to wait.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-fade-led */ #define LED_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to the LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long fade_start_ms; // the setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // declare GPIO46 (D9) to be an output fade_start_ms = millis(); } // fade-out in loop, and restart after finishing void loop() { unsigned long progress = millis() - fade_start_ms; if (progress <= FADE_PEDIOD) { long brightness = 255 - map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { fade_start_ms = millis(); // restart fade again } }

How to fade-in LED in a period without using delay()

This sketch does the opposite: the LED goes from off to full brightness over 3 seconds, then starts again. Change FADE_PEDIOD to make the fade longer or shorter.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-fade-led */ #define LED_PIN 46 // The ESP32 S3 Uno pin GPIO46 (D9) connected to the LED #define FADE_PEDIOD 3000 // fade time is 3 seconds unsigned long fade_start_ms; // the setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // declare GPIO46 (D9) to be an output fade_start_ms = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - fade_start_ms; if (progress <= FADE_PEDIOD) { long brightness = map(progress, 0, FADE_PEDIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { fade_start_ms = millis(); // restart fade again } }

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

Function References

These are the Arduino functions used in this tutorial. Open each link to learn more about them.

FAQ

Does analogWrite() work on the ESP32 S3 Uno?

Yes. The esp32 by Espressif Systems core (version 2.0.11 or newer, and 3.x) supports analogWrite() on PWM pins. It uses a 0–255 range by default, the same as other Arduino boards. If you get a compile error, update the esp32 by Espressif Systems package in the Boards Manager.

Which ESP32 S3 Uno pins can fade an LED?

Almost every Uno header pin can output PWM, including D2–D13. This tutorial uses D9 (GPIO46). If you have boot or upload problems, D7 (GPIO14) or D5 (GPIO20) are good choices. Avoid D0 and D1, because they are used for the USB serial port.

Why is my LED a bit dimmer than on the UNO R4?

The ESP32 S3 Uno pins output 3.3V, while the UNO R4 pins output 5V. With the same 220 ohm resistor, less current flows, so the LED is less bright. It is still bright enough for most projects. Do not remove the resistor to make it brighter.

Why use millis() instead of delay() to fade an LED?

delay() stops the whole program while it waits. With millis(), the code only checks the time and moves on, so it can read buttons or sensors while the LED fades. This makes your project feel smooth and responsive.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog. GPIO45 and GPIO35–GPIO42 support I/O and PWM. GPIO47 and GPIO48 are output only with PWM. Solder pin headers to use them and write the GPIO number in code. For extra fading LEDs, GPIO15, GPIO16 and GPIO35–GPIO42 are good picks. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port shows up in Arduino IDE USB driver missing or board not in upload mode Install the CP210x or CH340 driver, use a data USB cable, or hold BOOT while pressing RESET
Upload fails or board does not boot with the LED connected D9 is GPIO46, a boot strapping pin Unplug the LED wire from D9, upload, then reconnect it, or move the LED to D7 (GPIO14) and set LED_PIN to 14
LED does not light up at all LED is reversed or wired to the wrong pin Put the long leg (anode) toward D9 through the resistor and the short leg to GND
LED turns on and off but does not fade Wrong pin number in code Use the GPIO number 46 in code, not 9 or D9
Compile error about analogWrite Old esp32 board package Update esp32 by Espressif Systems in the Boards Manager
Fade is too fast or too slow Step size or timing values Change fade_step and delay(30), or change FADE_PEDIOD in the millis() sketches
LED is very dim Resistor value too large for 3.3V Use a 100 to 220 ohm resistor and check the breadboard connections

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!