CNAP

RGB LED Reference

Three LEDs (red, green, blue) in one package sharing a common cathode.

Pin names

NameDescription
RRed anode
GGreen anode
BBlue anode
COMCommon cathode (negative)

Attributes

This component has no configurable attributes.

Usage

Connect each of R, G, B through a 220 Ω current-limiting resistor to a PWM-capable GPIO pin, and COM to GND. Use analogWrite() on each channel to mix colours — 0 is off, 255 is full brightness.

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;

void setColor(int r, int g, int b) {
  analogWrite(PIN_R, r);
  analogWrite(PIN_G, g);
  analogWrite(PIN_B, b);
}

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}

void loop() {
  setColor(255, 0, 0); delay(500); // red
  setColor(0, 255, 0); delay(500); // green
  setColor(0, 0, 255); delay(500); // blue
  setColor(255, 255, 0); delay(500); // yellow
}

Examples

DescriptionAttrs
Default RGB LED{}

See also

  • LED — single-colour equivalent
  • NeoPixel — addressable RGB LED using one data pin instead of three PWM pins
  • Resistor — one per channel (R, G, B), 220 Ω typical