Trybotics Logo

Simple RGB LED Tutorial(Digital Colour Mixer and Controlling Using Potentiometer)

DESCRIPTION

A simple tutorial to show the functionality of a common Anode RGB LED and generating a 7 Colour Palette by mixing digital inputs A Potentiometer is used in the same circuit to control the displayed colours with a different arduino code to show analog control. The potentiometer values can be mapped to different sensor values to make the RGB LED function as a status indicator. 

We will be using a common Anode RGB LED and a thing to keep in mind are the values used in the analog or digital inputs. 
Sending a LOW to Red pin makes the LED glow Red.

Description:

1. Common Anode RGB LED
2. 3 resistors(Preferrably around 300Ω(a bit higher should also be good. We do not want too much current flowing through our LED. Check LED specifications to calculate appropriate resistor)
3. Potentiometer
4. 2 Push Buttons
5. Arduino Uno(Micro Controller Platform. For details visit the Arduino Website)
6. Connector Wires
7. Breadboard

Note: We are using the Potentiometer to double up as a third switch for the colour mixer.

The longest leg of the LED is the anode. A diagram is attached showing the colours mapped to the legs.

Description:

Connect the horizontally connected strips on the breadboard to +5V and GND on the Arduino board accordingly and connect all the other points on the board from the strips on the breadboard itself.

RGB LED

1. Connect the common anode(longest leg) to the +5V pin
3. Connect the legs Red, Green and Blue to digital inputs 13, 12 and 8 through a resistor each

Potentiometer

1. Connect the left end to +5V pin
2. Connect the right end to GND
3. Connect the middle end to Analog input,A0

Push Buttons

Connect one of the buttons to GND and the other ends to Digital inputs 7 and 2 respectively

Note: We will be using the internal pull up resistors for the buttons so we do not need to add an extra resistor for the buttons

Description:

The following code uses the Potentiometer as a switch for the Red colour. The potentiometer gives analog values with 500 being used as a threshold to make it function as a digital input.

Try pressing the buttons in various combinations and turning the Potentiometer to get different colours. 

Note: The RGB Colour palette can be generated by mixing different values of RGB. Connect 2 more potentiometers and the RGB LED outputs to analog outputs. Replace digitalWrite() by analogWrite() method to pass values between 0 to 255 for each colour to generate more colours. Give attention to the variables in use and also that 0 gives maximum brightness.

//Code Begins here
/* Lights up a common Anode RGB LED using a potentiometer
two pushdown switches as input
Created: Ravi Kanth Kosuru
Date: 28/12/2013 */

int rPin = 13;
int gPin = 12;
int bPin = 8;
int pot = A0;
int gButton = 7;
int bButton = 2;
char rVal, gVal, bVal;
void setup()

{

  pinMode(rPin, OUTPUT);

  pinMode(gPin, OUTPUT);

  pinMode(bPin, OUTPUT);

  pinMode(pot, INPUT_PULLUP);

  pinMode(gButton, INPUT_PULLUP);

  pinMode(bButton, INPUT_PULLUP);

  Serial.begin(9600);
}



void loop()

{
  int potValue = analogRead(pot);
  Serial.println(potValue);

  //Makes the potentiometer act as a switch by turning it on when potentiometer
  //reading is more than 500

  if (potValue < 500){
    rVal = LOW;
  }
  else
  {
    rVal = HIGH;
  }

  if (digitalRead(gButton) == HIGH){
    gVal = HIGH;
  }
  else
  {
    gVal = LOW;
  }

  if (digitalRead(bButton) == HIGH){
    bVal = HIGH;
  }
  else
  {
    bVal = LOW;
  }

  setColour(rVal, gVal, bVal);



}


void setColour(char red, char green, char blue)

{

  digitalWrite(rPin, red);

  digitalWrite(gPin, green);

  digitalWrite(bPin, blue);

}
/*Code Ends here */

Description:

My friend was wondering if we could change colour to show optimum gear shift on his car like in NFS drag races.
This code just uses potentiometer to change the colour of the LED based on the values. But RGB LEDS can be used for cooler stuff but hooking it up to the correct input sources. If you could get a RPM, Gear Ratio and Torque map of your car, you could program the circuit to light up Green for that correct RPM or think of more cool stuff. Meanwhile just download and run this program to turn your imaginary knob.

//Code begins here
/* Changes colours of an RGB LED based on potentiometer
Created: Ravi Kanth Kosuru
Date: 28/12/2013 */

int rPin = 13;
int gPin = 12;
int bPin = 8;
int switchCase = 0;
int pot = A0;
int gButton = 7;
int bButton = 2;
char rVal, gVal, bVal;
void setup()

{

  pinMode(rPin, OUTPUT);

  pinMode(gPin, OUTPUT);

  pinMode(bPin, OUTPUT);

  pinMode(pot, INPUT_PULLUP);

  pinMode(gButton, INPUT_PULLUP);

  pinMode(bButton, INPUT_PULLUP);

  Serial.begin(9600);
}



void loop()

{
  //Divide Potentiometer Values by 4 to get a smaller range
  int potValue = analogRead(pot)/4;
  Serial.println(potValue);
  //Starts with Blue for values below 100 and Red for values above 15
  // Could be used to map optimum settings with
  //Green between 115 - 145. Values before and after Green zones change colour
  //to show boundary condtions for optimum values
  if (potValue > 0 && potValue < 100){
    switchCase = 0;
  }
      else if (potValue >= 100 && potValue < 115){
        switchCase = 1;
      }
      else if (potValue >= 115 && potValue < 135){
        switchCase = 2;
      }
      else if (potValue >= 135 && potValue < 150){
        switchCase = 1;
      }
      else {
        switchCase = 4;
      }

switch(switchCase){
  case 0:
    setColour(HIGH, HIGH, LOW);
    break;
  case 1:
    setColour(LOW, LOW, LOW);
    break; 
  case 2:
    setColour(HIGH, LOW, HIGH);
    break;
  case 4:
    setColour(LOW, HIGH, HIGH);
    break; 
}

}


void setColour(char red, char green, char blue)
{

  digitalWrite(rPin, red);

  digitalWrite(gPin, green);

  digitalWrite(bPin, blue);

}
//Code ends here


YOU MIGHT ALSO LIKE