Go Down

Topic: Common anode RGB LED (Read 4 times) previous topic - next topic

FallenDemon

So I picked up an RGB LED and it is common anode (+) and I am wondering how to change this code to work with this LED. The code is:
Code: [Select]
//pins for the LEDs:

const int redPin = 3;

const int greenPin = 5;

const int bluePin = 6;




const int redPin2 = 9;

const int greenPin2 = 10;

const int bluePin2 = 11;




#define REDPIN 3

#define GREENPIN 5

#define BLUEPIN 6




#define FADESPEED 5




void setup() {

// initialize serial:

Serial.begin(9600);




// make the pins outputs:

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);




pinMode(redPin2, OUTPUT);

pinMode(greenPin2, OUTPUT);

pinMode(bluePin2, OUTPUT);




Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");

Serial.print('\n');

}




void loop() {




// if there's any serial available, read it:

while (Serial.available() > 0) {

 

// look for the next valid integer in the incoming serial stream:

int red = Serial.parseInt();

// do it again:

int green = Serial.parseInt();

// do it again:

int blue = Serial.parseInt();




int red2 = Serial.parseInt();

// do it again:

int green2 = Serial.parseInt();

// do it again:

int blue2 = Serial.parseInt();




// look for the newline. That's the end of your

// sentence:

if (Serial.read() == '\n') {

           

   // constrain the values to 0 - 255 and invert

   // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"

   

   //red = 255 - constrain(red, 0, 255);

   //green = 255 - constrain(green, 0, 255);

   //blue = 255 - constrain(blue, 0, 255);

   

red = constrain(red, 0, 255);

green = constrain(green, 0, 255);

blue = constrain(blue, 0, 255);

   

red2 = constrain(red2, 0, 255);

green2 = constrain(green2, 0, 255);

blue2 = constrain(blue2, 0, 255);




   // fade the red, green, and blue legs of the LED:

analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue);

   

analogWrite(redPin2, red2);

analogWrite(greenPin2, green2);

analogWrite(bluePin2, blue2);




   // print the three numbers in one string as hexadecimal:

Serial.print("Data Response : ");

Serial.print(red, HEX);

Serial.print(green, HEX);

Serial.println(blue, HEX);

}

}




}


jack wp

That is not a sketch! 
Give us the sketch you are trying to use, and the pins used . How many resistors at what value. Looks like you are almost there.
Good luck, Jack

FallenDemon

#2
Aug 27, 2013, 12:33 am Last Edit: Aug 27, 2013, 12:38 am by FallenDemon Reason: 1
Well it is the code I copied and pasted directly from my arduino software. It works great when I use common ground on separate LEDs but I just bought a common anode LED and it doesn't work now. The pins are right there in the code..3, 5, 6. I'm a bit confused by your response. The code I posted is what I am uploading to the arduino board. But it has to be changed so that in will work with a common anode LED. So I need to set the pins 3, 5, 6 to output as negative and not positive. What do I need to change in the code above for that to happen?

Grumpy_Mike

Wire the anode to +5V and the cathodes through your resistor to the input pin. Then the code will be logically inverted. That is a low will turn the LED on and a high off.
This is the normal way to drive LEDs and it is called current sinking.

FallenDemon

So instead of the code saying Output I should change it to input? I have already wired it all up but the RGB LED just lights up all three colors. What do I change in the code for it to work? Thanks for your help.

Grumpy_Mike

No they should not be inputs.
A digital write to a pin will set the output. Set it low to turn the led on and high for it to be off.
If you have already wired them up then you will have to rewire them.

FallenDemon

Yea I just rewired them the way you said with positive to common anode and the rgb pins to the arduino. However there is no High or Low in the code I posted. It is Analog write and constrain. This is for an app on my android phone which works awesome with a common cathode. But like I said I am now using a common anode LED and it doesn't work because I do not know what to change in the code. I understand the High and Low thing but this code doesn't have that in it.

jack wp

Quote
I understand the High and Low thing but this code doesn't have that in it.

For the code it is not High and Low: it is HIGH and LOW
Good luck, Jack

FallenDemon

Yes it is HIGH and LOW. However like I said the code I am using doesn't use that. All I need is the pins 3, 5, 6 to be ground and not positive. What do I need to change in the code I posted to achieve this? Thanks.

ehrja

PMW should work wit common cathode and common anode as well

You should change pinMode() as an INPUT when you are using common anode(+)
if you are using common cathode(-) use pinMode() as an OUTPUT...

the the common pin goes to 5v if is an anode or to GND if it is cathode

the color pins goes to they'er respective pin in the IC, already configured in software as an INPUT or OUTPUT.

jack wp

Quote
Yes it is HIGH and LOW. However like I said the code I am using doesn't use that. All I need is the pins 3, 5, 6 to be ground and not positive. What do I need to change in the code I posted to achieve this? Thanks.

I don''t understand that. ?

First thing, I can't read your code. Try reformat it.
For pin 3 to be ground,
Code: [Select]
 pinMode ( 3, OUTPUT );
 digitalWrite ( 3, LOW);

Good luck, Jack

FallenDemon

Code: [Select]
const int redPin = 3;

const int greenPin = 5;

const int bluePin = 6;




const int redPin2 = 9;

const int greenPin2 = 10;

const int bluePin2 = 11;




#define REDPIN 3

#define GREENPIN 5

#define BLUEPIN 6




#define FADESPEED 5




void setup() {


Serial.begin(9600);



pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);




pinMode(redPin2, OUTPUT);

pinMode(greenPin2, OUTPUT);

pinMode(bluePin2, OUTPUT);




Serial.print("Arduino control RGB LEDs Connected OK ( Sent From Arduinno Board )");

Serial.print('\n');

}




void loop() {



while (Serial.available() > 0) {



int red = Serial.parseInt();


int green = Serial.parseInt();


int blue = Serial.parseInt();


int red2 = Serial.parseInt();


int green2 = Serial.parseInt();

// do it again:

int blue2 = Serial.parseInt();


if (Serial.read() == '\n') {

 
red = constrain(red, 0, 255);

green = constrain(green, 0, 255);

blue = constrain(blue, 0, 255);

   

red2 = constrain(red2, 0, 255);

green2 = constrain(green2, 0, 255);

blue2 = constrain(blue2, 0, 255);


analogWrite(redPin, red);

analogWrite(greenPin, green);

analogWrite(bluePin, blue);

   

analogWrite(redPin2, red2);

analogWrite(greenPin2, green2);

analogWrite(bluePin2, blue2);


Serial.print("Data Response : ");

Serial.print(red, HEX);

Serial.print(green, HEX);

Serial.println(blue, HEX);

}

}




}

No where in that code is digitalWrite, HIGH, LOW, etc.... They are analog write on pins 3, 5, and 6. As the code sits, pins 3,5, and 6 all have positive voltage coming from them. I need the code to be changed so that they are ground as I am using a common anode LED. Thanks.

FallenDemon

I changed the code to say INPUT instead of OUTPUT and uploaded code to arduino. But now when I open the app to control the RGB LED all 3 colors are always on and when I move the sliders to change the color the LED gets dimmer as I move up. It's like backwards. But they never shut off. I am really close I think I just need some more help. Any other thoughts please??

Runaway Pancake

#13
Aug 27, 2013, 03:51 am Last Edit: Aug 27, 2013, 03:55 am by Runaway Pancake Reason: 1
Yes, with your common anode device, full-on will be with analogWrite(pin,0) and full-off will be with analogWrite(pin,255)

Change
Code: [Select]

analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

to
Code: [Select]

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

> > > I hope we have a resistor between each LED cathode and its corresponding Arduino output/pwm pin.
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

FallenDemon

Thank you Pancake!! You just made my night. Possibly my week! Works awesome. Thank you again!!! XD

Go Up