SunFounder ESP32 Starter Kit
void setup() {
ledcSetup(0, 5000, 8); // Configure the PWM channel (0) with 5000Hz␣
˓→frequency and 8-bit resolution
ledcAttachPin(ledPin, 0); // Attach the LED pin to the PWM channel
}
Here we use the (LED control) peripheral which is primarly designed to control the intensity of LEDs,
although it can also be used to generate PWM signals for other purposes.
• uint32_t ledcSetup(uint8_t channel, uint32_t freq, uint8_t
resolution_bits);: This function is used to setup the LEDC channel frequency and
resolution. It will return frequency configured for LEDC channel. If 0 is returned, error occurs
and ledc channel was not configured.
– channel select LEDC channel to config.
– freq select frequency of pwm.
– resolution_bits select resolution for ledc channel. Range is 1-14 bits (1-20 bits for
ESP32).
• void ledcAttachPin(uint8_t pin, uint8_t chan);: This function is used to attach the
pin to the LEDC channel.
– pin select GPIO pin.
– chan select LEDC channel.
3. The loop() function contains the main logic of the program and runs continuously. It updates the LED’s bright-
ness, inverts the fade amount when the brightness reaches the minimum or maximum value, and introduces a
delay.
void loop() {
ledcWrite(0, brightness); // Write the new brightness value to the PWM␣
˓→channel
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(50); // Wait for 20 milliseconds
}
• void ledcWrite(uint8_t chan, uint32_t duty);: This function is used to set duty for
the LEDC channel.
– chan select the LEDC channel for writing duty.
– duty select duty to be set for selected channel.
1.6. 2.2 Fading 25