SunFounder ESP32 Starter Kit
Code
Note:
• You can open the file 6.5_color_gradient.ino under the path of esp32-starter-kit-main\c\codes\
6.5_color_gradient.
• After selecting the board (ESP32 Dev Module) and the appropriate port, click the Upload button.
• Always displaying “Unknown COMxx”?
This project uses an RGB LED and a potentiometer to create a color mixing effect. The potentiometer is used to adjust
the hue value of the LED, which is then converted into RGB values using a color conversion function. The RGB values
are then used to update the color of the LED.
How it works?
This project builds upon the 2.3 Colorful Light project by adding a potentiometer to adjust the hue value of the LED.
The hue value is then converted to RGB values using a color conversion function.
1. In the loop function, read the value of the potentiometer and convert it to a hue value (0-360).
int knobValue = analogRead(KNOB_PIN);
float hueValue = (float) knobValue / 4095.0;
int hue = (int) (hueValue * 360);
2. Convert the hue value to RGB values using the HUEtoRGB() function, and update the LED with the new color
values.
int red, green, blue;
HUEtoRGB(hue, &red, &green, &blue);
setColor(red, green, blue);
3. The setColor() function sets the value of the red, green, and blue channels using the LEDC library.
void setColor(int red, int green, int blue) {
ledcWrite(redChannel, red);
ledcWrite(greenChannel, green);
ledcWrite(blueChannel, blue);
}
4. The HUEtoRGB function converts a hue value to RGB values using the HSL color model.
void HUEtoRGB(int hue, int* red, int* green, int* blue) {
float h = (float) hue / 60.0;
float c = 1.0;
float x = c * (1.0 - fabs(fmod(h, 2.0) - 1.0));
float r, g, b;
if (h < 1.0) {
r = c;
g = x;
b = 0;
...
124 Chapter 1. For Arduino User