Copy and paste the code below into a blank Arduino sketch. Then compile and upload it to your Metro.  

// CIRC12 - RGB LED 

//  RGB LED PINS
// three pins:
// 9 = RED
// 10 = GREEN
// 11 = BLUE
int ledDigitalOne[] = {9, 10, 11};

// define on as low
// (because you use a common anode RGB LED)
const boolean ON = LOW;
// define off as high
const boolean OFF = HIGH;

//  Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};

//An Array that stores the predefined colors
const boolean* COLORS[] =
  {RED, GREEN, BLUE,YELLOW, CYAN, MAGENTA,
  WHITE, BLACK};

void setup() {
  for(int i = 0; i < 3; i++){
    //  set the 3 pins as outputs
    pinMode(ledDigitalOne[i], OUTPUT);
  }
}

void loop() {
  // set the color of the LED
  setColor(ledDigitalOne, CYAN);
  // randomize it
  // randomColor();
}

void randomColor(){
  //  get random number within range of colors
  int rand = random(0, sizeof(COLORS) / 2);
  setColor(ledDigitalOne, COLORS[rand]);
  delay(1000);
}

void setColor(int* led, const boolean* color) {
  for(int i = 0; i < 3; i++){
    digitalWrite(led[i], color[i]);
  }
}

Having Trouble? 

LED Remains Dark or Shows Incorrect Color

With the four pins of the LED so close together, it’s sometimes easy to misplace one. Try double checking each pin is where it should be.

Green and Blue seem to be reversed

Some RGB LEDs have green and blue swapped, change your code so the pins are swapped and re-upload!

Seeing Red

The red diode within the RGB LED may be a bit brighter than the other two. To make your colors more balanced, try using a higher ohm resistor (or two resistors in series).

Looking For More?

If you’re looking to do more why not check out all the lovely extra bits and bobs available from the Adafruit Shop.

This guide was first published on Aug 18, 2017. It was last updated on Aug 18, 2017.
This page (Code) was last updated on Jul 13, 2020.