Controlling RGBW 5050 LED Strip with ESP32
MechApe opened this issue · 3 comments
Hello,
First ever GIT post! Firstly, thank you so much for putting this library together. I am a noob programmer and I can (almost!) get this code working. So hats off to everyone involved! I've been able to get some basic tests working with LEDs, an ESP32 and my Echo Dot...great! Now I would like to connect an RDGW 5050 LED strip.
Am I correct in thinking that I should use an GPIO pin for each of the R, G, B and W lines wired through a transistor? Then in my code I should declare the pin: const int redPin = 2;
then
void setup()
...
pinMode(redPin, OUTPUT);
Now, this is where I get a bit stuck. In the callback function, I can see that I can do something like the following:
analogWrite(redPin, d->getR())
But how does this relate to the brightness or d->getValue()
. And will this also work the the W line?
Any help would be very much appreciated!
Hi, the ESP32 has a different way for pwm output:
only example code copied from my project
// RGBW-Handling **************************
const int channel_r = 0; // channels for pins/ports
const int channel_g = 1;
const int channel_b = 2;
const int channel_w = 3;
// RGBW-Handling **************************
const uint8_t port_LED_r = 23; // example ESP32 output pwm pins/ports for RGBW
const uint8_t port_LED_g = 19;
const uint8_t port_LED_b = 21;
const uint8_t port_LED_w = 22;
setup()
{
// RGBW-Handling **************************
// Strip RGBWW ports init
//channel 0-15 resolution 1-16bits freq limits depend on resolution
//double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);
//void ledcWrite(uint8_t channel, uint32_t duty);
const int freq = 5000; // PWM frequency
const int resolution = 8; // 8 Bit resolution
ledcSetup(channel_r, freq, resolution);
ledcSetup(channel_g, freq, resolution);
ledcSetup(channel_b, freq, resolution);
ledcSetup(channel_w, freq, resolution);
ledcAttachPin(port_LED_r, channel_r);
ledcAttachPin(port_LED_g, channel_g);
ledcAttachPin(port_LED_b, channel_b);
ledcAttachPin(port_LED_w, channel_w);
ledcWrite(channel_r, 0); // all off
ledcWrite(channel_g, 0);
ledcWrite(channel_b, 0);
ledcWrite(channel_w, 0);
}
void colorcallback(uint8_t brightness, uint32_t rgb)
{
uint16_t r,g,b;
int32_t br_r, br_g, br_b; // new values RGB with brightness
//example for calculating one color
// RGB LED color 0-255
r = alexadevice_rgb->getR(); // red color
br_r = (r * (uint16_t)brightness ) / 255; // 8 Bit value
... blue
... green
ledcWrite(channel_r, br_r); // write final colors to strip
ledcWrite(channel_g, br_g);
ledcWrite(channel_b, br_b);
}
void whitecallback(uint8_t brightness)
{
ledcWrite(channel_w, brightness); // white
}
For all my lightning-projects with RGBW-Strips i always implemented two espalaexadevices, on for color and one for white.
And i made a fade function (not in the example above), so the callback function only set new color or white value and a fade flag and the color/brightness change is in an ISR. The ISR fades in 2 seconds from old to the new one color or brightness (can also be off).
The hardware, i always use ULN2003 chips with pullups on outputs 1,8kohm to 12Volt followed by an RGBW-Amplifier.
Switching with a transistor is also possible.
Thank you @barneyz ! I will take a look this evening. 👍
Am I correct in thinking that you separated out the W from the RGB since you cannot get the W component from the EspalexaDeviceType::extendedcolor
device type?
I separated WW(warm white) and RGB in my RGBWW-strips because of more options like pastel colors or double the brightness if needed . When you have only one device you never can switch both WW and RGB.
The ULN2003 or ULN 2803 can direct drive 5V relais, so i have separate 5V (ESP32) and 12V supplys, the ESP can switch the 12V supply of for less power consumption when no LEDS are on.