Arduino Nano ESP32 - LED - Fade

This tutorial provides instructions on how to use:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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.

Overview of LED

LED Pinout

LED includes two pins:

  • Cathode(-) pin: connect this pin to GND (0V)
  • Anode(+) pin: is used to control LED's state
LED Pinout

How LED Works

After connecting the cathode(-) to GND:

  • If We connect GND to the anode(+), LED is OFF.
  • If We connect VCC to the anode(+), LED is ON.
  • 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.
    • If PWM value is 0, it is equivalent to GND ⇒ LED is OFF
    • If PWM value is 255, it is equivalent to VCC ⇒ LED is ON at the highest brightness.
    How LED Works

    ※ NOTE THAT:

    Usually, it requires a resistor to protect LED from burning. The resistance's value depends on the LED's specification.

    Arduino Nano ESP32 - fade LED

    The ESP32's digital output pins can be programmed to generate PWM signal. By connecting LED's anode(+) pin to an Arduino Nano ESP32's pin, LED's cathode(-) to GND, and then programming Arduino Nano ESP32 to change the duty cytle of PWM, we We can fade LED.

Wiring Diagram between LED and Arduino Nano ESP32

The wiring diagram between Arduino Nano ESP32 and LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Configure an Arduino Nano ESP32's pin as a digital output pin by using pinMode() function. For example, pin D5:
pinMode(D5, OUTPUT);
  • Set brightness of LED by generating PWM signal with corresponding duty cycle by using analogWrite() function:
analogWrite(D5, brightness);

Where brightness is a value from 0 to 255.

Arduino Nano ESP32 Code - Simple Fade Example

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the below code and paste it to Arduino IDE.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-led-fade */ #define LED_PIN D5 // The Arduino Nano ESP32 pin D5 connected to LED int brightness = 0; // how bright the LED is int fade_amount = 5; // how many points to fade the LED by // The setup function runs once on reset or power-up void setup() { // configure the Arduino Nano ESP32 pin as an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // set the brightness analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_amount; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_amount = -fade_amount; } // wait for 30 milliseconds to see the dimming effect delay(30); }
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
Arduino IDE Upload Code
  • See the brightness of LED

Line-by-line Code Explanation

The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!

※ NOTE THAT:

The above example uses the delay() function to fade-in and fade-out. The delay() function makes the LED fade unsmoothly and blocks other code. In the next parts, we will learn how to fade-in and fade-out smoothly without blocking other code by using millis() function

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

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

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

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

Video Tutorial

Language References

※ 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!