Controlling an RGB LED with a Potentiometer

In this post, I’ll describe how to change the color of an anode RGB LED with a potentiometer. I’ll be using an Arduino UNO, and components from this RadioShack components kit. The motivation for this post was to have an LED change color in response to the reading from a thermistor next to my stove, but when I read about how I’d first need to calibrate the thermistor with some kind of thermometer, my motivation scurried under the sofa like a terrier in a thunderstorm. As a compromise I substituted the thermistor with a trim-pot, reasoning that a variable resistance was a variable resistance.

Anode/Cathode RGB LEDs

RGB LED come in two flavors, common anode, and common cathode. Recalling the mnemonic ACID (Anode Current Into Device), we can infer that a common anode RGB LED has current driving one pin, and that a common cathode RGB LED is grounded on one pin. Either way, this anode or cathode will be the longest of the four pins coming out of the LED. Unfortunately, these guys aren’t always labelled clearly as to what they are. In this example, I have worked out the wiring for a common anode RGB LED; most other guides describe a common cathode wiring.

The Wiring

This image was created using the Fritzing graphical editor that is available for free.

The Sketch

I used this tutorial on the AdaFruit site to figure out how to change the colors of the RGB LED, and this tutorial on the Arduino site to figure out how to work with a potentiometer.

int trim = 0 ;
int val = 0 ;

int redPin = 9 ;
int greenPin = 11 ;
int bluePin = 10 ;

void setup()
  Serial.begin(9600) ;
  pinMode( redPin, OUTPUT ) ;
  pinMode( greenPin, OUTPUT ) ;
  pinMode( bluePin, OUTPUT ) ;

void loop()
  val = analogRead( trim ) ;
  Serial.println(val) ;
  if( val < 100 )
    setColor( 255, 0, 0 ) ;
  if( val >= 100 )
    if( val < 300 )
      setColor( 255, 255, 0 ) ;
  if( val > 300 )
    setColor( 0, 255, 0 ) ;

void setColor(int red, int green, int blue)
  analogWrite( redPin, 255 - red ) ;
  analogWrite( greenPin, 255 - green ) ;
  analogWrite( bluePin, 255 - blue ) ;  

2 thoughts on “Controlling an RGB LED with a Potentiometer”

Comments are closed.