The longer lead goes to positive, the flat spot points toward the cathode (negative). The current goes from the anode (positive) to the cathode (negative).
Connect the longer pin (common anode) to +5V, and the red, green, and blue each go to microcontroller PWM digital output through a resistor. Use a slightly higher value resistor (220 to 330 ohm) for the red than for the green and blue (100 to 220 ohm).
/* Single RGB LED (common anode or common cathode) */ ///////////////////////////////////////////////////////////////////////// // RGB LED (assign PWM digital output or analog output pins) const byte pinRed = 11; const byte pinGreen = 10; const byte pinBlue = 9; const boolean IsCommonAnode = true; const unsigned int iDelay = 1000; void setup () { pinMode(pinRed, OUTPUT); pinMode(pinGreen, OUTPUT); pinMode(pinBlue, OUTPUT); } // setup() void loop() { SetRGBcolor(255, 0, 0); // red delay(iDelay); SetRGBcolor(0, 255, 0); // green delay(iDelay); SetRGBcolor(0, 0, 255); // blue delay(iDelay); SetRGBcolor(0, 255, 255); // cyan / aqua delay(iDelay); SetRGBcolor(255, 255, 0); // yellow delay(iDelay); SetRGBcolor(80, 0, 80); // purple delay(iDelay); SetRGBcolor(255, 0, 255); // magenta delay(iDelay); SetRGBcolor(255, 255, 255); // white delay(iDelay); SetRGBcolor(0, 0, 0); // off delay(iDelay); } // loop() //////////////////////////////////////////////////////////////// void SetRGBcolor(byte red, byte green, byte blue) { if (IsCommonAnode == true) { // invert the color value red = 255 - red; green = 255 - green; blue = 255 - blue; } analogWrite(pinRed, red); analogWrite(pinGreen, green); analogWrite(pinBlue, blue); } // SetRGBcolor()
Connect the longer pin (common cathode) of the RGB (common cathode) to ground (of microcontroller), and the red, green, and blue pins each through a resistor to the microcontroller digital output PWM pins. Use a slightly higher value resistor (220 to 330 ohm) for the red than for the green and blue (100 to 220 ohm).
Do you need help developing or customizing a IoT product for your needs? Send me an email requesting a free one hour phone / web share consultation.
The information presented on this website is for the author's use only. Use of this information by anyone other than the author is offered as guidelines and non-professional advice only. No liability is assumed by the author or this web site.