Other Arduino Codes and Videos by Robojax

Fading 2 or more LEDS using ESP32 and Arduino IDE

دروس آردوینو به فارسی

Fading 2 or more LEDS using ESP32 on Arduino IDE

This video shows how to fade or dim LED lights using ESP32.

Timing in the video

The link to use in the "preferences" of Arduino IDE for ESP32 board https://dl.espressif.com/dl/package_esp32_index.json Watch video for instructions.

Resources for this sketch

Fading 2 or more LEDs using ESP32

 * Original code taken from ESP32 provided driver code
 * This is Arduino code to control 2 or more LEDs and fade them
 * using PWM and  LEDC library of ESP32
 * watch the video for explanation of this video:https://youtu.be/VCM6KMMvBfE

 * Written/updated by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
 * Date: Dec 17, 2017, in Ajax, Ontario, Canada
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * this code has been downloaded from http://robojax.com/learn/arduino/
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: http://robojax.com/L/?id=63 (watch until end of this video to list of my Patrons)

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

// use first channel of 16 channels (started from zero)
#define LEDC_CHANNEL_0     0
#define LEDC_CHANNEL_1     1
// use 13 bit precission for LEDC timer
#define LEDC_TIMER_13_BIT  13

// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ     5000

// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN0            17
#define LED_PIN1            16
int brightness0 = 0;    // how bright the LED is
int brightness1 = 0;    // how bright the LED is

int fadeAmount0 = 5;    // how many points to fade the LED by
int fadeAmount1 = 10;    // how many points to fade the LED by

// Arduino like analogWrite
// value has to be between 0 and valueMax
void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
  // calculate duty, 8191 from 2 ^ 13 - 1
  uint32_t duty = (8191 / valueMax) * min(value, valueMax);

  // write duty to LEDC
  ledcWrite(channel, duty);

void setup() {
  // Robojax.com ESP32 Fading LEDs 20191217
  // Setup timer and attach timer to a led pin
  ledcAttachPin(LED_PIN0, LEDC_CHANNEL_0);

  ledcAttachPin(LED_PIN1, LEDC_CHANNEL_1);  

void loop() {
  // Robojax.com ESP32 Fading LEDs 20191217
  // set the brightness on LEDC channel 0
  ledcAnalogWrite(LEDC_CHANNEL_0, brightness0);
  ledcAnalogWrite(LEDC_CHANNEL_1, brightness1);
  // change the brightness for next time through the loop:
  brightness0 = brightness0 + fadeAmount0;
  brightness1 = brightness1 + fadeAmount1;

 // brightness1 +=100;
  // reverse the direction of the fading at the ends of the fade:
  if (brightness0 <= 0 || brightness0 >= 255) {
    fadeAmount0 = -fadeAmount0;

  if (brightness1 <= 0 || brightness1 >= 255) {
    fadeAmount1 = -fadeAmount1;
  // wait for 30 milliseconds to see the dimming effect
  // Robojax.com ESP32 Fading LEDs 20191217

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal