The Demo

The RGB LED

The RGB LED is like three separate LEDS combined. You can control the amount of ech color by using pulse-width modulation. This is simple to do, it only requires using analogWrite() instead of digitalWrite().

Materials and Skills

  • Arduino UNO R3
  • RGB LED
  • 270 Ohm Resistors
  • Jumper Wires
  • The Schematic

    The Code

    // Define this as true if you are using a common anode rgb LED
    bool anode = false;
    
    // To reset time, press the reset button 
    
    int r = 9;
    int g = 11; 
    int b = 10;
    
    int countdownTime = 5; // Seconds
    int lastMillis = 0;
    
    int currentColor[] = {0,255,0};
    
    int countInterval;
    
    void setup()
    {
      Serial.begin(9600);
      pinMode(r, OUTPUT);
      pinMode(g, OUTPUT);
      pinMode(b, OUTPUT);
      
      countInterval = 255/countdownTime;
    }
    
    void loop()
    {
      Serial.println(String(currentColor[0])+" "+String(currentColor[1])+" "+String(currentColor[2]));
      int currentMillis = millis();
      if (currentMillis - lastMillis >= 1000 && countdownTime > 0) {
        lastMillis = currentMillis;
       	currentColor[0] += countInterval; 
        currentColor[1] -= countInterval; 
        countdownTime -= 1;
        updatePins(currentColor);
      } else if (countdownTime == 0) {
        int off[] = {0,0,0};
        while (countdownTime == 0) {
          updatePins(currentColor);
          delay(1000);
          updatePins(off);
          delay(1000);
        }
      }
    }
    
    void updatePins(int* colors) {
      if(!anode) {
        analogWrite(r,colors[0]);
        analogWrite(g,colors[1]);
        analogWrite(b,colors[2]);
      } else if(anode) {
        analogWrite(r,255-colors[0]);
        analogWrite(g,255-colors[1]);
        analogWrite(b,255-colors[2]);
      }
    }
    

    The Quiz

    What is the difference between common anode and cathode?

    Do you have a project idea?

    BUILD IT!

    This project won’t build itself! 

    Make sure to build the project and post a photo in the comments! 

    Do NOT follow this link or you will be banned from the site!