Nicholas Johnson
Nicholas Johnson

Reputation: 1022

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:

My setup: enter image description here

Default controller photo: enter image description here

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);
}

Upvotes: 1

Views: 2117

Answers (4)

thePHPHero
thePHPHero

Reputation: 189

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

TIP125 Datasheet

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...

Upvotes: 0

Secko
Secko

Reputation: 7716

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.

Upvotes: 0

showp1984
showp1984

Reputation: 378

I think you misunderstood my last comment under @Secko's answer, so here is what I meant:

int r = 5;
int g = 6;
int b = 3;

void setup() {
    pinMode(r, OUTPUT);
    pinMode(g, OUTPUT);
    pinMode(b, OUTPUT);
}

void loop() {
    analogWrite(r, 128);
    delay(50);
    analogWrite(r, 0);
    analogWrite(g, 128);
    delay(50);
    analogWrite(g, 0);
    analogWrite(b, 128);
    delay(50);
    analogWrite(b, 0);
    delay(50);
}

You need to turn off the last color you turned on in order to get a red, green and blue blink light. Otherwise if you turn on green after red, the strip will be yellow for a short time. If you then turn on blue it will be white and stay white, since no colors are ever turned off.

The second parameter is the voltage applied to your pins. It ranges from 0 - 255, where 0 is 0V and 255 is 5V. 128 is right in the middle with 2.5V.

If your strip is large (=>3 LEDs with power efficient LEDs. Otherwise >1), I hope you use transistors to offload the current draw from the arduino to the transistor. Otherwise you will blow the arduino very soon.

EDIT: You probably blew the arduino already by pulling too much current (too much LEDs) from it's pins.

You will need a PNP transistor in between each color and the arduino that can handle a large current (since you drive a lot of LEDs), which means a darlington transistor array integrated circuit.

Something like the TIP125 comes to mind. (https://www.fairchildsemi.com/datasheets/TI/TIP125.pdf)

It has a maximum collector current of 5A. Put this right in the middle of your VCC and the strip (VCC to collector, the strip to emitter) and connect the arduino to the base of the transistor. You will need 3, one for each color.

EDIT2: Here is a very quickly made schematic to show you the basics. It should be fully functional for a 12V LED strip with an individual color current draw of up to 5A (so 15A total).

You may want to add resistors on the transistor base to sink current though.

tip125/arduino transistor schematic

EDIT3: Since you are probably new to electronics: A PNP transistor will enable the connection between collector and emitter if the base is low. Meaning: analogWrite(r, 0) will turn the color on the strip on and analogWrite(r, 255) will turn it off. It is inverted with the transistor in between.

Also, and I hope this is obvious, DO NOT ROUTE 5A PER COLOR THROUGH A BREADBOARD OR THROUGH TINY WIRES. THEY WILL GO UP IN SMOKE.

Upvotes: 1

Vojtěch Hudec
Vojtěch Hudec

Reputation: 9

you have to add pinMode() to setup.

void setup() {
    pinMode(ledPinR, OUTPUT);
    pinMode(ledPinG, OUTPUT);
    pinMode(ledPinB, OUTPUT);
}

Upvotes: 0

Related Questions