The setColor Function for RGB LEDs

Master the powerful setColor function to create beautiful color combinations.

ARDUINO LESSONSProgrammingLED ProjectsFunctions
S
Shaun Sosi
How to program RGB LEDs with the setColor function

The Power of setColor

The setColor function is a game-changer for RGB LED programming, making your code cleaner, more maintainable, and easier to understand.

RGB LED Color Wheel
The RGB color wheel showing primary and secondary color combinations

Think of setColor Like This:

Instead of individually adjusting red, green, and blue knobs on a mixing board, setColor lets you control all three channels with a single command - like having a master control for your RGB LED!

Get Your Arduino Kit

To follow along with this RGB LED programming tutorial:

Common Cathode Implementation

Here's how to implement the setColor function for a common cathode RGB LED:

CommonCathodeSetColor.ino
int redPin = 3; int greenPin = 5; int bluePin = 6; void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { setColor(255, 0, 0); // Red delay(1000); setColor(0, 255, 0); // Green delay(1000); setColor(0, 0, 255); // Blue delay(1000); setColor(255, 255, 0); // Yellow delay(1000); setColor(0, 255, 255); // Cyan delay(1000); setColor(255, 0, 255); // Magenta delay(1000); setColor(255, 165, 0); // Orange delay(1000); setColor(128, 0, 128); // Purple delay(1000); setColor(255, 255, 255); // White delay(1000); }

    Color Values Explained:

    • Primary Colors: Red (255,0,0), Green (0,255,0), Blue (0,0,255)
    • Secondary Colors: Yellow (255,255,0), Cyan (0,255,255), Magenta (255,0,255)
    • Custom Colors: Orange (255,165,0), Purple (128,0,128)
    • Special Values: White (255,255,255), Black/Off (0,0,0)

    Common Anode Implementation

    For common anode RGB LEDs, we need to invert the values:

    CommonAnodeSetColor.ino
    int redPin = 3; int greenPin = 5; int bluePin = 6; void setColor(int redValue, int greenValue, int blueValue) { // Invert values for common anode analogWrite(redPin, 255 - redValue); analogWrite(greenPin, 255 - greenValue); analogWrite(bluePin, 255 - blueValue); } void setup() { pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); } void loop() { // Colors work the same way, but values are inverted internally setColor(255, 0, 0); // Red delay(1000); setColor(0, 255, 0); // Green delay(1000); setColor(0, 0, 255); // Blue delay(1000); }

      Value Inversion Explained:

      • Common Anode: LED is connected to power (5V)
      • Brightness Inverse: Lower voltage = brighter LED
      • Formula: 255 - value inverts the brightness
      • Example: 255 becomes 0, 0 becomes 255

      Advanced Color Mixing

      Let's create some beautiful color transitions using our setColor function:

      ColorMixing.ino
      // For Common Cathode RGB LED int redPin = 3; int greenPin = 5; int bluePin = 6; void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } // Function to create smooth transitions void colorFade(int startRed, int startGreen, int startBlue, int endRed, int endGreen, int endBlue, int steps, int duration) { int redDiff = endRed - startRed; int greenDiff = endGreen - startGreen; int blueDiff = endBlue - startBlue; for(int i = 0; i <= steps; i++) { int red = startRed + (redDiff * i / steps); int green = startGreen + (greenDiff * i / steps); int blue = startBlue + (blueDiff * i / steps); setColor(red, green, blue); delay(duration / steps); } } void loop() { // Sunset colors colorFade(255, 100, 0, // Orange 255, 0, 100, // Pink 50, 1000); // 50 steps over 1 second // Ocean colors colorFade(0, 100, 255, // Light blue 0, 50, 100, // Deep blue 50, 1000); // Forest colors colorFade(50, 255, 0, // Bright green 0, 100, 0, // Dark green 50, 1000); }

        Color Theory Tips:

        1. Complementary Colors

          • Red ↔ Cyan (0,255,255)
          • Green ↔ Magenta (255,0,255)
          • Blue ↔ Yellow (255,255,0)
        2. Color Temperature

          • Warm: Higher red values
          • Cool: Higher blue values
          • Neutral: Balanced values
        3. Intensity Control

          • Bright: Values closer to 255
          • Pastel: Mix with white (add all colors)
          • Muted: Lower overall values

        Creating Color Presets

        Here's a useful way to organize your favorite colors:

        ColorPresets.ino
        // Color structure for organizing RGB values struct Color { int red; int green; int blue; const char* name; }; // Preset color definitions const Color colors[] = { {255, 0, 0, "Red"}, {0, 255, 0, "Green"}, {0, 0, 255, "Blue"}, {255, 255, 0, "Yellow"}, {0, 255, 255, "Cyan"}, {255, 0, 255, "Magenta"}, {255, 165, 0, "Orange"}, {128, 0, 128, "Purple"}, {255, 192, 203, "Pink"}, {0, 128, 0, "Dark Green"} }; void setColorByName(const char* colorName) { for(int i = 0; i < sizeof(colors)/sizeof(colors[0]); i++) { if(strcmp(colors[i].name, colorName) == 0) { setColor(colors[i].red, colors[i].green, colors[i].blue); return; } } } void loop() { // Use colors by name setColorByName("Red"); delay(1000); setColorByName("Cyan"); delay(1000); setColorByName("Pink"); delay(1000); }

          Coming Up Next:

          In future episodes, we'll explore even more advanced topics like creating custom light patterns, responsive color changes, and integrating RGB LEDs with other sensors and components!

          Connect With Me

          Support Our Work

          Help us create more amazing content

          $

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