Skip to content

Commit f1e87d9

Browse files
Allanme-no-dev
Allan
authored andcommitted
Create ledcWrite_RGB.ino (espressif#688)
* Create ledcWrite_demo_ESP32_RGB.ino adding the public domain example ledcWrite_demo_ESP32.ino to this repo. Added RGB to the name for people searching, added some comments, and renames things to make a bit more sense. * Update ledcWrite_demo_ESP32_RGB.ino renamed to ledcWrite_RGB.ino and added a couple more comments based on espressif#689 * Rename libraries/ESP32/examples/AnalogOut/ledcWrite_demo_ESP32_RGB/ledcWrite_demo_ESP32_RGB.ino to libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino Renamed.
1 parent 3fea101 commit f1e87d9

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
ledcWrite_RGB.ino
3+
Runs through the full 255 color spectrum for an rgb led
4+
Demonstrate ledcWrite functionality for driving leds with PWM on ESP32
5+
6+
This example code is in the public domain.
7+
8+
Some basic modifications were made by vseven, mostly commenting.
9+
*/
10+
11+
// Set up the rgb led names
12+
uint8_t ledR = A4;
13+
uint8_t ledG = A5;
14+
uint8_t ledB = A18;
15+
16+
uint8_t ledArray[3] = {1, 2, 3}; // three led channels
17+
18+
const boolean invert = true; // set true if common anode, false if common cathode
19+
20+
uint8_t color = 0; // a value from 0 to 255 representing the hue
21+
uint32_t R, G, B; // the Red Green and Blue color components
22+
uint8_t brightness = 255; // 255 is maximum brightness, but can be changed. Might need 256 for common anode to fully turn off.
23+
24+
// the setup routine runs once when you press reset:
25+
void setup()
26+
{
27+
Serial.begin(115200);
28+
delay(10);
29+
30+
ledcAttachPin(ledR, 1); // assign RGB led pins to channels
31+
ledcAttachPin(ledG, 2);
32+
ledcAttachPin(ledB, 3);
33+
34+
// Initialize channels
35+
// channels 0-15, resolution 1-16 bits, freq limits depend on resolution
36+
// ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
37+
ledcSetup(1, 12000, 8); // 12 kHz PWM, 8-bit resolution
38+
ledcSetup(2, 12000, 8);
39+
ledcSetup(3, 12000, 8);
40+
}
41+
42+
// void loop runs over and over again
43+
void loop()
44+
{
45+
Serial.println("Send all LEDs a 255 and wait 2 seconds.");
46+
// If your RGB LED turns off instead of on here you should check if the LED is common anode or cathode.
47+
// If it doesn't fully turn off and is common anode try using 256.
48+
ledcWrite(1, 255);
49+
ledcWrite(2, 255);
50+
ledcWrite(3, 255);
51+
delay(2000);
52+
Serial.println("Send all LEDs a 0 and wait 2 seconds.");
53+
ledcWrite(1, 0);
54+
ledcWrite(2, 0);
55+
ledcWrite(3, 0);
56+
delay(2000);
57+
58+
Serial.println("Starting color fade loop.");
59+
60+
for (color = 0; color < 255; color++) { // Slew through the color spectrum
61+
62+
hueToRGB(color, brightness); // call function to convert hue to RGB
63+
64+
// write the RGB values to the pins
65+
ledcWrite(1, R); // write red component to channel 1, etc.
66+
ledcWrite(2, G);
67+
ledcWrite(3, B);
68+
69+
delay(100); // full cycle of rgb over 256 colors takes 26 seconds
70+
}
71+
72+
}
73+
74+
// Courtesy http://www.instructables.com/id/How-to-Use-an-RGB-LED/?ALLSTEPS
75+
// function to convert a color to its Red, Green, and Blue components.
76+
77+
void hueToRGB(uint8_t hue, uint8_t brightness)
78+
{
79+
uint16_t scaledHue = (hue * 6);
80+
uint8_t segment = scaledHue / 256; // segment 0 to 5 around the
81+
// color wheel
82+
uint16_t segmentOffset =
83+
scaledHue - (segment * 256); // position within the segment
84+
85+
uint8_t complement = 0;
86+
uint16_t prev = (brightness * ( 255 - segmentOffset)) / 256;
87+
uint16_t next = (brightness * segmentOffset) / 256;
88+
89+
if(invert)
90+
{
91+
brightness = 255 - brightness;
92+
complement = 255;
93+
prev = 255 - prev;
94+
next = 255 - next;
95+
}
96+
97+
switch(segment ) {
98+
case 0: // red
99+
R = brightness;
100+
G = next;
101+
B = complement;
102+
break;
103+
case 1: // yellow
104+
R = prev;
105+
G = brightness;
106+
B = complement;
107+
break;
108+
case 2: // green
109+
R = complement;
110+
G = brightness;
111+
B = next;
112+
break;
113+
case 3: // cyan
114+
R = complement;
115+
G = prev;
116+
B = brightness;
117+
break;
118+
case 4: // blue
119+
R = next;
120+
G = complement;
121+
B = brightness;
122+
break;
123+
case 5: // magenta
124+
default:
125+
R = brightness;
126+
G = complement;
127+
B = prev;
128+
break;
129+
}
130+
}

0 commit comments

Comments
 (0)