Programming RGB LEDs

Learn how to program both common cathode and common anode RGB LEDs.

ARDUINO LESSONSProgrammingLED ProjectsPWM
S
Shaun Sosi
How to easily program RGB LEDs

Introduction to RGB LED Programming

RGB LEDs are versatile components that can create any color by combining red, green, and blue light. Understanding how to program them effectively opens up endless possibilities for creative lighting projects.

RGB LED Color Mixing
RGB LEDs can create any color by mixing red, green, and blue light

Think of RGB LEDs Like This:

Just like mixing primary colors in painting, RGB LEDs mix different intensities of red, green, and blue light to create any color you can imagine!

Get Your Arduino Kit

To follow along with this RGB LED programming tutorial:

Digital Control with digitalWrite

Let's start with the basics of controlling RGB LEDs using digital signals.

Key Concepts:

  • Digital Control - Simple on/off control of each color channel
  • Common Cathode - Ground is shared, HIGH turns LED on
  • Common Anode - Power is shared, LOW turns LED on

Common Cathode RGB LED

CommonCathodeDigital.ino
int red = 3; int green = 5; int blue = 6; void setup() { pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } void loop() { digitalWrite(red, HIGH); // Red on digitalWrite(green, LOW); // Green off digitalWrite(blue, LOW); // Blue off delay(1000); digitalWrite(red, LOW); // Red off digitalWrite(green, HIGH); // Green on digitalWrite(blue, LOW); // Blue off delay(1000); digitalWrite(red, LOW); // Red off digitalWrite(green, LOW); // Green off digitalWrite(blue, HIGH); // Blue on delay(1000); }

    Common Anode RGB LED

    CommonAnodeDigital.ino
    int red = 3; int green = 5; int blue = 6; void setup() { pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } void loop() { digitalWrite(red, LOW); // Red on digitalWrite(green, HIGH); // Green off digitalWrite(blue, HIGH); // Blue off delay(1000); digitalWrite(red, HIGH); // Red off digitalWrite(green, LOW); // Green on digitalWrite(blue, HIGH); // Blue off delay(1000); digitalWrite(red, HIGH); // Red off digitalWrite(green, HIGH); // Green off digitalWrite(blue, LOW); // Blue on delay(1000); }

      Important Considerations:

      • Always use current-limiting resistors for each color channel
      • Verify whether you have a common cathode or common anode LED
      • Double-check pin connections before applying power

      Analog Control with analogWrite

      For more sophisticated color control, we can use PWM to create varying intensities of each color.

      PWM Control:

      Using analogWrite allows us to set 256 different brightness levels (0-255) for each color channel, enabling millions of possible color combinations!

      Common Cathode RGB LED with PWM

      CommonCathodePWM.ino
      int red = 3; int green = 5; int blue = 6; void setup() { pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } void loop() { analogWrite(red, 255); // Red full brightness analogWrite(green, 0); // Green off analogWrite(blue, 0); // Blue off delay(1000); analogWrite(red, 0); // Red off analogWrite(green, 255); // Green full brightness analogWrite(blue, 0); // Blue off delay(1000); analogWrite(red, 0); // Red off analogWrite(green, 0); // Green off analogWrite(blue, 255); // Blue full brightness delay(1000); }

        Common Anode RGB LED with PWM

        CommonAnodePWM.ino
        int red = 3; int green = 5; int blue = 6; void setup() { pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } void loop() { analogWrite(red, 0); // Red full brightness analogWrite(green, 255); // Green off analogWrite(blue, 255); // Blue off delay(1000); analogWrite(red, 255); // Red off analogWrite(green, 0); // Green full brightness analogWrite(blue, 255); // Blue off delay(1000); analogWrite(red, 255); // Red off analogWrite(green, 255); // Green off analogWrite(blue, 0); // Blue full brightness delay(1000); }

          Tips and Best Practices

          1. Hardware Setup

            • Use appropriate current-limiting resistors (220Ω-330Ω typically)
            • Connect to PWM-capable pins (marked with ~) for analog control
            • Verify common anode/cathode configuration before wiring
          2. Programming Practices

            • Initialize all pins in setup()
            • Use variables for pin numbers for easy modification
            • Comment your code to document color combinations
          3. Color Mixing

            • Red + Green = Yellow
            • Red + Blue = Magenta
            • Green + Blue = Cyan
            • All Colors = White

          Pro Tip:

          Create helper functions for common colors to make your code more organized and reusable!

          Advanced Color Control

          Here's a more sophisticated example that creates smooth color transitions:

          ColorFade.ino
          // For Common Cathode RGB LED int red = 3; int green = 5; int blue = 6; void setup() { pinMode(red, OUTPUT); pinMode(green, OUTPUT); pinMode(blue, OUTPUT); } // Function to set a specific color void setColor(int redValue, int greenValue, int blueValue) { analogWrite(red, redValue); analogWrite(green, greenValue); analogWrite(blue, blueValue); } void loop() { // Fade from red to green for(int i = 0; i <= 255; i++) { setColor(255-i, i, 0); delay(10); } // Fade from green to blue for(int i = 0; i <= 255; i++) { setColor(0, 255-i, i); delay(10); } // Fade from blue to red for(int i = 0; i <= 255; i++) { setColor(i, 0, 255-i); delay(10); } }

            Coming Up Next:

            In future episodes, we'll explore more advanced topics like creating custom lighting effects and integrating RGB LEDs with sensors!

            Connect With Me

            Support Our Work

            Help us create more amazing content

            $

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