I am working on an RGB led mixer. I can control the colours by moving a sliding potentiometer. However, I want this to work with 3 other leds. How can I replicate this over a total of 4 leds? Right now, the pins of the led are 9, 10, 11 and the potentiometer is connected to analog 0.
I am modifying someone else's code for my own personal use.
Code:
/*
* Program: rgbled.ino
* Author: Julian White
* Created: 10/13/2012
* Description: Use single 10K potentiometer to cycle through all
* hues in the color wheel. The RGB LED is wired as common anode
* (+5V). 330 ohm resistor wired in series with LED anode.
*/
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int potPin = A0;
int val = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//Read the potentiometer (color wheel).
val = analogRead(potPin);
Serial.println(val);
//Red-orange-yellow-green spectrum
if ( val <= 341 )
{
//We now know the val is between 0 and 341.
//Map the val to 0-255 for the LED.
val = map(val, 0, 341, 0, 255);
//Higher val turns red down.
//Lower val turns red up (common anode).
analogWrite(redPin, val);
//Write the complimentary amount to the green pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(greenPin, 255-val);
//Turn blue pin off.
analogWrite(bluePin, 255);
}
//Green-teal-blue spectrum
else if ( val <= 683 )
{
//We now know the val is between 341 and 683.
//Map the val to 0-255 for the LED.
val = map(val, 341, 683, 0, 255);
//Higher val turns green down.
//Lower val turns green up (common anode).
analogWrite(greenPin, val);
//Write the complimentary amount to the blue pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(bluePin, 255-val);
//Turn red pin off.
analogWrite(redPin, 255);
}
//Blue-indigo-purple-red spectrum.
else
{
//We now know the val is between 683 and 1023.
//Map the val to 0-255 for the LED.
val = map(val, 683, 1023, 0, 255);
//Higher val turns blue down.
//Lower val turns blue up (common anode).
analogWrite(bluePin, val);
//Write the complimentary amount to the red pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(redPin, 255-val);
//Turn green pin off.
analogWrite(greenPin, 255);
}
//Sampling rate.
delay(20);
}
Hi, an Arduino output can only really source enough current for one led (40mA absolute max but 20~30mA for regular use). You could drive 4 leds, but you would have to increase the value of the series resistors to stay under the current limit. This would make each of the leds only one quarter as bright, which kind of defeats the object.
To get around the pin current limit, you need to use some transistors. If your leds are common cathode, you need 3 pnp transistors such as bc327 or 3 p-channel fets. What do you have like that?
So are you suggesting that I used transistors as a "switch"? I'm don't have much knowledge regarding electronics and their components. Does the voltage emitter output pin of a transistor scale with the bases's input?
Well, I won't start explaining about transistors now, there are plenty of good tutorials on the net. But you are correct to say that you can use them as high speed switches, a bit like a relay but much faster and they don't wear out. You need to use them as current amplifiers. A small current from the Arduino pin gets amplified to a large enough current to drive 4 leds.
Please describe your current circuit further. Do you have a link to a data sheet, or at least a web page about these rgb leds? Are they common-cathode or common-anode? What series resistors are you using?
The RBG leds are from ebay and have their own resistors (I believe they are 110 ohm). I just have one more question.
If a small current from the arduino allows small current to flow from the emitter, will a larger current allow a larger current to flow from the emitter assuming input to collector is the same?
chairr:
If a small current from the arduino allows small current to flow from the emitter, will a larger current allow a larger current to flow from the emitter assuming input to collector is the same?
Yes and no. That can happen when a transistor is used in what is called it's "linear region". That's how audio amplifiers work, for example. But when you use a transistor as a switch, you don't want to be in the linear region, you want it to be in the "saturated region". You do this by sending enough current through the base to fully switch the transistor on to its maximum. Then, extra current through the base makes almost no difference to the current through the collector/emitter. The reason you want the transistor to be saturated is so that it's resistance is as low as possible, so it does not get hot even when quite a lot of current is flowing through it.
Yes, you can indeed fade them (I knew you were leading up to this question!). Using a technique called Pulse Width Modulation (PWM). This is what the Arduino function analogWrite() does. Despite its name, it does not vary the voltage or current from an Arduino pin. Instead, it switches the pin on and off at high speed, much faster than the eye can see. The % of the time that the pin is high vs. low determines how bright an led attached to the pin appears.
PaulRB:
Yes, you can indeed fade them (I knew you were leading up to this question!). Using a technique called Pulse Width Modulation (PWM). This is what the Arduino function analogWrite() does. Despite its name, it does not vary the voltage or current from an Arduino pin. Instead, it switches the pin on and off at high speed, much faster than the eye can see. The % of the time that the pin is high vs. low determines how bright an led attached to the pin appears.
How would I power the collector pin of the transistors? What would be a suitable power source for the collector pins?
Wait what, how does that work? I thought it worked like this: http://iamtechnical.com/sites/default/files/transistor-as-a-switch-pnp_2.gif
...where current that goes through the base is amplified and allows current from the collector to run through the emitter. Sorry, could you clarify this for me a bit more? Thanks
Back in post #3 I asked "Are they common-cathode or common-anode?". Perhaps you din't understand the question and so ignored it. The leds you have will have 4 pins. 3 go to Arduino pins and the 4th goes to either 5V (= common anode) or 0V (= common cathode). If your leds are common cathode, you should use pnp transistors, if common anode you should use npn transistors.