Understanding RGB LEDs

Master the differences between common anode and cathode RGB LEDs.

ARDUINO LESSONSElectronicsLED Projects
S
Shaun Sosi
How Common Anode and Cathode RGB LEDs work

Understanding RGB LEDs

RGB LEDs combine three individual LEDs (Red, Green, and Blue) in a single package, allowing you to create any color by mixing these primary colors at different intensities.

RGB LED Types and Configurations
Common Anode vs Common Cathode RGB LED configurations

Think of RGB LEDs Like This:

Imagine mixing paint colors - just as you combine red, green, and blue paint to create different colors, an RGB LED mixes different intensities of colored light to create any color you want.

Get Your Arduino Kit

To follow along with this RGB LED tutorial:

Common Cathode RGB LEDs

Key Features:

Common cathode RGB LEDs share a single negative (ground) connection, with separate control pins for each color.

  1. Pin Configuration

    • Four pins total: one common cathode and three color pins
    • Common cathode connects to Arduino GND
    • Color pins connect to PWM pins through resistors
  2. Advantages

    • More commonly available
    • Straightforward programming logic (higher value = brighter)
    • Compatible with most LED libraries
CommonCathodeRGB.ino
// Pin definitions for common cathode RGB LED const int redPin = 9; // PWM pin for red const int greenPin = 10; // PWM pin for green const int bluePin = 11; // PWM pin for blue void setup() { // Set all color pins as outputs pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { // Example: Create purple color (red + blue) analogWrite(redPin, 255); // Full red analogWrite(greenPin, 0); // No green analogWrite(bluePin, 255); // Full blue delay(1000); // Turn off all colors analogWrite(redPin, 0); analogWrite(greenPin, 0); analogWrite(bluePin, 0); delay(1000); }

    Common Anode RGB LEDs

    Key Features:

    Common anode RGB LEDs share a single positive connection, with separate control pins for each color that work with inverted logic.

    1. Pin Configuration

      • Four pins total: one common anode and three color pins
      • Common anode connects to Arduino 5V
      • Color pins connect to PWM pins through resistors
    2. Special Considerations

      • Inverted logic (0 = full brightness, 255 = off)
      • May require code modification for LED libraries
      • Less common but equally capable
    CommonAnodeRGB.ino
    // Pin definitions for common anode RGB LED const int redPin = 9; // PWM pin for red const int greenPin = 10; // PWM pin for green const int bluePin = 11; // PWM pin for blue void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { // Example: Create purple color (red + blue) // Note the inverted values (255 - desired brightness) analogWrite(redPin, 0); // Full red (inverted) analogWrite(greenPin, 255); // No green (inverted) analogWrite(bluePin, 0); // Full blue (inverted) delay(1000); // Turn off all colors (all pins HIGH) analogWrite(redPin, 255); analogWrite(greenPin, 255); analogWrite(bluePin, 255); delay(1000); }

      Color Mixing Function

      Here's a helpful function for creating colors with either type of RGB LED:

      ColorMixing.ino
      // Function to set RGB color (works with both types) void setColor(int red, int green, int blue, bool isCommonAnode = false) { // Invert values for common anode if (isCommonAnode) { red = 255 - red; green = 255 - green; blue = 255 - blue; } analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } // Example usage: // For common cathode: setColor(255, 0, 255, false); // Purple // For common anode: setColor(255, 0, 255, true); // Purple

        Important Considerations:

        • Current Limiting - Always use appropriate resistors (220Ω-330Ω)
        • PWM Pins - Use only PWM-capable pins for color control
        • Power Supply - Consider power requirements for full brightness
        • Heat Management - Avoid running at full brightness for extended periods

        Common Color Combinations

        Here are some popular RGB color values:

        • Red - (255, 0, 0)
        • Green - (0, 255, 0)
        • Blue - (0, 0, 255)
        • Yellow - (255, 255, 0)
        • Cyan - (0, 255, 255)
        • Magenta - (255, 0, 255)
        • White - (255, 255, 255)
        • Orange - (255, 165, 0)

        Pro Tip:

        Create smooth color transitions using the map() function we learned in the previous lesson:

        ColorFade.ino
        // Smooth color transition for(int i = 0; i < 255; i++) { // Fade from red to blue setColor(255-i, 0, i, isCommonAnode); delay(10); }

          Troubleshooting Tips

          Common issues and solutions:

          • Wrong Colors - Double-check LED type and pin connections
          • Dim Output - Verify resistor values and power supply
          • Flickering - Ensure stable power supply and proper PWM frequency
          • No Light - Check LED orientation and common pin connection

          Project Ideas

          Try these projects with RGB LEDs:

          • Mood Lamp - Create ambient lighting with smooth color transitions
          • Color Organ - Sync colors to music or sound
          • Temperature Indicator - Display temperature ranges with colors
          • Interactive Art - Use sensors to control colors

          Coming Up Next:

          Stay tuned for more exciting Arduino tutorials where we'll explore more components and create even more colorful projects!

          Connect With Me

          Support Our Work

          Help us create more amazing content

          $

          Your contribution helps us create more amazing content. Thank you! 💖