Wednesday, January 5, 2011

Arduino RGB Mixer with single Potentiometer

Hi there I wish you happy new year 2011 ! Today I will show you my first Arduino project. It’s RGB Mixer.

Parts you’ll need :
Arduino (I’ve used Duemilanove, but you can use different type)
Breadboard
1x 1W RGB diode
3x 100 ohm resistor
2x 1K resistor
1x Potentiometer (I’ve used 2k2)
1x Momentary switch
Few jumpers
When you upload sketch into Arduino, you can now adjust brightness of LED’s with Potentiometer and browse between LED’s with switch

How to connect :

Red LED > digital pin 9 (and through 100 ohm resistor to ground)
Green LED > digital pin 10 (and through 100 ohm resistor to ground)
Blue LED > digital pin 11 (and through 100 ohm resistor to ground)
Switch > connect one side to 5V and second side through 1K resistor to ground and through another 1K resistor to digital pin 2
Potentiometer > analog pin  0
Here’s the Code :

int brightness = 0;
int potPin = A0;
int switchPin = 2;
int blue = 11;
int green = 10;
int red = 9;

int val;                       
int val2;                    
int switchValue;              

int mode = 0;
void setup() {
 
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(switchPin, INPUT);
 
  switchValue = digitalRead(switchPin);
   
}
void loop() {
   val = digitalRead(switchPin);     
  delay(10);                        
  val2 = digitalRead(switchPin);    
  if (val == val2) {                
    if (val != switchValue) {         
      if (val == LOW) {              
        if (mode == 0) {         
          mode = 1;             
        } else {
          if (mode == 1) {      
            mode = 2;                    
            } else {
            if (mode == 2) {
                mode = 0;         
              }
   }
          }
        }
      }
    }
    switchValue = val;              
 
  brightness = analogRead(potPin);
  if (mode == 0) {
  analogWrite(red, brightness / 4);  
   }
  
  if (mode == 1) {
  analogWrite(green, brightness / 4);  
   }
  
  if (mode == 2) {
  analogWrite(blue, brightness / 4);  
   }
  
 

 Here you can see some pictures and video.

8 comments:

  1. thanks a lot for this post! Just one typo in the code.
    line 44 - should be - analogRead(potPin)

    ReplyDelete
  2. I'm glad you like it :) And thanks for finding that mistake

    ReplyDelete
  3. Hi is there a way to make this keep the same color choice when the power is disconnected and reconnected?

    ReplyDelete
  4. There are multiple ways of how to do that. I think easiest way is to use EEPROM on atmega chip to store the values.

    ReplyDelete
    Replies
    1. That is one way I was thinking of but don't know how to implement it. This will be used as a RGB controller for accent lighting in my car.

      Delete
    2. I myself would programm it that everytime you turn it on it reads from EEPROM 3 bytes one for each color, and than I would implement a pushbutton for color saving.

      Delete
  5. Hey i didn't get why you need teo 1k resistors to connect the switch; also in the schematic you don't show! Thanks!:)

    ReplyDelete
  6. green light doesn't work for me! any tips?

    ReplyDelete