EasyManuals Logo

SunFounder ESP32 User Manual

Default Icon
771 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #36 background imageLoading...
Page #36 background image
SunFounder ESP32 Starter Kit
(continued from previous page)
}
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 cycles through various colors (red, green, blue, yellow, purple, and cyan) with one-second
intervals between each color change.
void loop() {
setColor(255, 0, 0); // Red
delay(1000);
setColor(0, 255, 0); // Green
delay(1000);
setColor(0, 0, 255); // Blue
delay(1000);
setColor(255, 255, 0); // Yellow
delay(1000);
setColor(80, 0, 80); // Purple
delay(1000);
setColor(0, 255, 255); // Cyan
delay(1000);
}
4. The setColor() function sets the desired color by writing the appropriate duty cycle values to each PWM
channel. The function takes in three integer arguments for red, green, and blue color values.
void setColor(int red, int green, int blue) {
// For common-anode RGB LEDs, use 255 minus the color value
ledcWrite(redChannel, red);
ledcWrite(greenChannel, green);
ledcWrite(blueChannel, blue);
}
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.
30 Chapter 1. For Arduino User

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the SunFounder ESP32 and is the answer not in the manual?

SunFounder ESP32 Specifications

General IconGeneral
BrandSunFounder
ModelESP32
CategoryController
LanguageEnglish