Boris LeonovSam KristoffArthur Brown
Published © MIT

RGB Light Control with Arduino

Set up an RGB LED to phase through the colors of the rainbow, or use twist knobs to make a custom color.

BeginnerShowcase (no instructions)1 hour25,933
RGB Light Control with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
OpenScope MZ
Digilent OpenScope MZ
×1
RGB Diffused Common Anode
RGB Diffused Common Anode
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
Any pot will work, but I recommend ones that have a knob to twist.
×1

Software apps and online services

Digilent WaveForms Live

Story

Read more

Code

Basic Red LED

Arduino
Set the brightness of the LED through code by changing the dutyCycle variable.
//Set the duty cycle percentage: change to any value 0-100
float dutyCycle = 50;

//set I/O pins
//pin 3 will connect to the base of the NPN transistor and set the LED brightness
int LED = 3;

//set the duty cycle percentage (0-100)
float dutyCycle = 50;

void setup() {
   pinMode(LED, OUTPUT);
   digitalWrite(LED, HIGH);

}

void loop() {
  analogWrite(LED, ((float)(dutyCycle/100))*255);

}

Brightness Toggle

Arduino
Based on a button click, the code switches through a set number of brightness levels between full bright and off. Change number of levels by changing the numSteps variable.
//Set the number of steps between full bright and off by changing to any value
//greater than zero
float numSteps = 4;

//keep track of which step we're on
float stepNum = numSteps;

//set I/O pins
//pin 3 will connect to the base of the NPN transistor and set the LED brightness
int LED = 3;

//pin 4 will be used to monitor the button
int SWTCH = 4;

void setup() {
   //set pin modes
   pinMode(LED, OUTPUT);
   pinMode(SWTCH, INPUT);

   //initially set LED to full brightness
   digitalWrite(LED, HIGH);

   //initiallize serial interface to print the PWM value for each button click
   Serial.begin(9600);
   Serial.println((stepNum/numSteps)*255);
}

void loop() {
  //monitor the switch pin for a drop in voltage
  if(digitalRead(SWTCH) == 0){
    toggle();
  }
  }

void toggle(){
  //wait for the signal to stabilize
  delay(1);
  //make sure the inital drop wasn't random noise
  if(digitalRead(SWTCH) == 0){
    
    //reduce stepNum by 1
    stepNum--;

    //reset numSteps if it's past zero
    if(stepNum < 0){
      stepNum = numSteps;
    }

    //set new PWM duty cycle
    analogWrite(LED, (stepNum/numSteps)*255);

    //print new duty cycle to serial monitor
    Serial.println((stepNum/numSteps)*255); 
    
    //wait for the signal to go high again before doing anything else
    while(digitalRead(SWTCH) == 0){
    
    }
    
    //wait for the signal to stabilize
    delay(1);
  }
}

Rainbow/Custom Color LED

Arduino
Iterates through 6 colors in the rainbow with 1s delay for each color, or allows you to set the color with potentiometer dials. In loop(), switch commenting between setColor() and rainbowStrobe() to toggle the two modes.
//set I/O pins
//pin 3 will connect to the base of the NPN transistor and trigger the LED on/off
int LED = 3;

//digital pins 9, 10, 11 are PWM enabled, so they will be used to sink the current for the Red, Green, and Blue LEDs in the RGB LED
int red = 9;
int green = 10;
int blue = 11;

//analog pins A0-A2 will be used for potentiometer input
int rLevel = A0;
int gLevel = A1;
int bLevel = A2;

//define the RGB values for each color of the rainbow: red, orange, yellow, green, blue, violet
float rainbowArrayRGB[][6] = {{255,255,255,0,0,139},{0,127,255,255,0,0},{0,0,0,0,255,255}};

//array to indicate which PWM line to set the values to
int colorsArray[] = {red, green, blue};

//light intensity
int brightness = 30;

//track which mode LED is in
bool toggle;

void setup() {
  //set pin input/output modes
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(LED, OUTPUT);
  pinMode(rLevel, INPUT);
  pinMode(gLevel, INPUT);
  pinMode(bLevel, INPUT);

  //initially turn on the LED
  digitalWrite(LED, HIGH);
}

void loop() {
  //swap comments between these two functions to access one or the other mode: color selection or rainbow.
  setColor();
//  rainbowStrobe();

}

void setColor(){
  float colorLevels[3] = {analogRead(rLevel), analogRead(gLevel), analogRead(bLevel)};
  float levelSum = 0;
  for(int i = 0; i < sizeof(colorsArray); i++){
    if(colorLevels[i] > 1024){
      colorLevels[i] = 1024;
    }
    else if(colorLevels[i] < 8){
      colorLevels[i] = 0;
    }
    levelSum += colorLevels[i];
  }

  for(int i = 0; i < sizeof(colorsArray); i++){
    analogWrite(colorsArray[i], 255 - brightness*(colorLevels[i]/levelSum));
  }
}

void rainbowStrobe(){
  for(int i = 0; i < sizeof(rainbowArrayRGB[0])/sizeof(rainbowArrayRGB[0][1]); i++){
    for(int j = 0; j < sizeof(rainbowArrayRGB)/sizeof(rainbowArrayRGB[0]); j++){
      analogWrite(colorsArray[j], 255-brightness*(rainbowArrayRGB[j][i]/(rainbowArrayRGB[0][i]+rainbowArrayRGB[1][i]+rainbowArrayRGB[2][i])));
    }
    delay(1000);
  } 
}

Credits

Boris Leonov

Boris Leonov

10 projects • 24 followers
Electrical engineering student in the Seattle area. Electrical and mechanical DIY enthusiast. Fixer of things, large and small.
Sam Kristoff

Sam Kristoff

35 projects • 54 followers
R&D Director at NI
Arthur Brown

Arthur Brown

14 projects • 30 followers
Applications engineer and digital logic geek

Comments