'Arduino | RGB LED Strip Controller
I have an Arduino UNO and I am attempting to turn ON and OFF a color (red, green, or blue) light of the strip. Here is my code, but All the lights remain lit. The issue is that for example I might want to only show the color RED, but I can't seem to get any of the colors to turn off. I have the pins as follows:
- RED: 5
- GREEN: 6
- BLUE: 3
Relevant Code:
int ledPinR = 5;
int ledPinG = 6;
int ledPinB = 3;
void setup() {
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinB, OUTPUT);
}
void loop() {
analogWrite(ledPinR, 0);
analogWrite(ledPinG, 0);
analogWrite(ledPinB, 0);
}
Solution 1:[1]
you have to add pinMode() to setup.
void setup() {
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinB, OUTPUT);
}
Solution 2:[2]
You have to delay the operations, before your turn the next light on or off like so:
EDIT: a complete fader for your case, you might want to skip 4 in the loop, but I will let you handle that.
int ledPinR = 5;
int ledPinG = 6;
int ledPinB = 3;
int brightness = 0;
int fade = 5;
void setup() {
pinMode(ledPinR, OUTPUT);
pinMode(ledPinG, OUTPUT);
pinMode(ledPinB, OUTPUT);
}
void loop() {
for(int LED_PIN = 3; LED_PIN<=6; LED_PIN++) {
analogWrite(LED_PIN, brightness); //setting the brightness at LED pin
brightness += fade; //brightness increasE
if(brightness == 0 || brightness == 255){
fade = -fade;
}
delay(30); //time of delay is in miliseconds
}
}
EDIT2: OK, saw your setup, I was way off on what you wanted to do with the colors, don't know if I should delete the answer, it is misguiding now. Hope the delay helped though.
Solution 3:[3]
I was facing the same problem. My issue was that I was inverting the Emitter and Collector wiring at the transistor.
For the TIP125 transistor suggested by showp1984, you'll want to make sure that the LED strip is connected to the Emitter pin and the Collector pin must be connected to Arduino GND. Connect the Base of the transistor to the Arduino output pin (See below). Use a 1K Ohm resister to make the Base connection.
Here is my setup:
- 3x Transistor TIP125 Darlington
- 1x Arduino Nano
- 1x 12V LED Strip
- 3x 1KiloOhm Resistors
Wiring
Use one transistor for each LED color (R, G, B):
RED
- Transistor Base --> RESISTOR --> Arduino PIN D5
- Transistor Collector --> Arduino GND
- Transistor Emitter --> LED Red
GREEN
- Transistor Base --> RESISTOR --> Arduino PIN D6
- Transistor Collector --> Arduino GND
- Transistor Emitter --> LED Green
BLUE
- Transistor Base --> RESISTOR --> Arduino PIN D3
- Transistor Collector --> Arduino GND
- Transistor Emitter --> LED Blue
Code
// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
#define FADESPEED 5 // make this higher to slow down
void setup() {
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}
void loop() {
int r, g, b;
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
}
Observations
Here are some more hurdles that I came across and took me some time to figure out.
Inverted values
- While using the TIP125, you actually need to set your output PIN to 255 to shut that PIN down and set it to 0 in order to bring it up:
analogWrite(REDPIN, 255); //RED is off
analogWrite(REDPIN, 0); //RED is on
Short at the transistor heatsink
This might sound obvious, but it cost me several hours. I was testing this setup on a breadboard and two of the three transistor heatsinks were leaning backwards just enough as to make light contact with the resistor legs. This was actually one of the main causes for the LEDs connected to these transistors to light up unexpectedly...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vojt?ch Hudec |
| Solution 2 | |
| Solution 3 | Dharman |



