Go Down

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


"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!


Could you possibly offer me insight on this: http://forum.arduino.cc/index.php?PHPSESSID=r9lmn6vkt6qrau0o6gnhmc41d6&topic=184538.0

I am trying to put this project on an attiny84. Everytime I try to verify I get an error. Please have a look. Thanks again.


Okay scratch that. What would I change this to?
Code: [Select]
analogWrite(redPin, map(colors[0], 0, 255, 0, max_red));
  analogWrite(greenPin, map(colors[1], 0, 255, 0, max_green));
  analogWrite(bluePin, map(colors[2], 0, 255, 0, max_blue));
Same issue here with the code being written for common cathode. As you are aware I am using a common anode. I already changed the OUTPUT to INPUT and it works it's just the colors don't match on the LED when I select the corresponding color from my android app. Thanks again.


I already changed the OUTPUT to INPUT and it works

Then change them back, the pins must be an output.
Using PWM. The values are reversed, so if you have a value val then you need to subtract it from 255.
Code: [Select]

analogWrite(pin,val); // for common cathode
analogWrite(pin,255 - val); // for common anode

So in your code you an change the map values or simply do
Code: [Select]

analogWrite(bluePin, 255 - map(colors[2], 0, 255, 0, max_blue));


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 color pins goes to they'er respective pin in the IC, already configured in software as an INPUT or OUTPUT.

This is rubbish, please do not post rubbish.

What happens when you do this is that instead of setting spin high or low you switch the pull up resistor on or off. This resistor is about 30K so if you just use a scope it will look to have inverted things but the current you get out of it is very limited. So under a restricted set of conditions you get the illusion it is working.


Mike when I leave the code as OUTPUT the RGB LED stays on at all colors. Even when changing the code as you suggested. I change them to INPUT and all works fine. So I am confused as to why you say leave as OUTPUT?


I say use it as outputs because that is what you must do to output to a device.
If you have the pins as inputs all the current supplied goes through the internal pull up and the current output is very limited.
If you say it does not work when the pins are outputs you have something else wrong. To find out what it is we need to see a schematic of how you have wired it up along with all the code you are using.
What resistor values are you using?


Ahhh! Okay so I changed them back to OUTPUT and changed the code as you suggested and it is working just fine!! Weird that it works as INPUT and OUTPUT??? I am using the 3.3 volt on Arduino and my LED is rated at 4.0 max @ 50mA so I am not using any resistors. Thanks Mike!!!!!! My next question is how do I program this code to an Attiny84 to use without the Arduino?? When I try to upload to the attiny84 I get an error. I made a thread here about it: http://forum.arduino.cc/index.php?topic=184538.0 You would be my hero if you can help with that. Thanks again!!!


Aug 27, 2013, 10:05 am Last Edit: Aug 27, 2013, 10:08 am by Grumpy_Mike Reason: 1
so I am not using any resistors.

No you must use resistors otherwise you will damage the arduino.
The 40mA limit is where damage starts to occours. You should not permit more current to flow than this. There is nothing in the arduino that limits the current to a safe level.

Your other question is answered in the thread you linked to.

jack wp

In the reference for analogWrite()
You do not need to call pinMode() to set the pin as an output before calling analogWrite().


Your other question is answered in the thread you linked to.

Are you referring to the NewSoftSerial? I have it as I have Arduino 1.0.5. How would the code be different though from the one I posted?


How would the code be different though from the one I posted?

You would look at the software serial example in your IDE.
File -> examples -> SoftwareSerial -> SoftwareSerialExample
This shows you how to use the library.
You change your sketch to have the #include line and declare an instance as shown in the line following, in this case called mySerial but it could be called anything. Note you have to decide what pins you will use here, again they can be anything you choose.
Then in setup call mySerial.begin(baud_rate) with what ever baud rate you like.
Then change the calls from Serial.print to mySerial.print and the serial data comes out of those pins.

Finally if you want to see that serial data on a PC or laptop you need to connect these pins to a TTL serial to USB cable.

Don't forget about the resistors with the LEDs, see this for more info:-


Thanks I am still getting an error on this line when verifying using the Attiny84
Code: [Select]
int red = Serial.parseInt();
The whole code where the Serial starts is
Code: [Select]
void setup() {

// initialize serial:


// 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 )");



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, (255 - red));

analogWrite(greenPin, (255 - green));

analogWrite(bluePin, (255 - blue));


analogWrite(redPin2, (255 - red));

analogWrite(greenPin2, (255 - green));

analogWrite(bluePin2, (255 - blue));

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




I am unsure of what to change to "mySerial." I did add the lines: #include <SoftwareSerial.h>
                                                                                        SoftwareSerial mySerial(10, 11);


In your code you must not have any instances of Serial, they should all be replaced by mySerial.

jack wp

Have we solved the threads subject? Have we moved to another subject now? Should we start a new thread with the current subject? I am new here, but I thought that way the SOP here.

Go Up