Skip to content

Exercise 03: Control Blue LED on GPIO2 (Breathing Light Effect)

Mohamad Ariffin Zulkifli edited this page Apr 18, 2021 · 1 revision

This exercise is a continuation from Exercise 2.

In Exercise 2, we use for() function to automatically generate decremental PWM value from 255-0 for the (active-low circuit) to produce an output of glowing light effect, while in this exercise we produce breathing light effect.

It's easy ... we just need to add another for() function to automatically generate incremental PWM value from 0-255, so the program as follows:

Complete Sketch

void setup() {
  // configure PWM controller congfiguration
  ledcSetup(0, 5000, 8);
  // declare the GPIO number for PWM signal output
  ledcAttachPin(2, 0);
}

void loop() {
  // function for() to create decremental value by 1 start from 255 --> 0
  // from OFF LED to linear increasing brightness, for active-low circuit
  for(int brightness = 255; brightness >= 0; brightness--){   
    ledcWrite(0, brightness);
    delay(15);
  }
  // wait for 0.2 seconds before start again
  delay(200);
  
  // function for() to create incremental value by 1 start from 0 --> 255
  // from ON LED to linear decreasing brightness, for active-low circuit
  for(int brightness = 0; brightness <= 255; brightness++){   
    ledcWrite(0, brightness);
    delay(15);
  }

  delay(200);
}

Now, we can upload the complete sketch to ESP32, then observe the output. By observation, the output of the blue LED is repeated breathing light effect, which the results from decremental and incremental value of the PWM signal output.

🚀 Tutorial Improvement & Suggestions

Clone this wiki locally