Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: RGB LED Common Anode

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    14

    RGB LED Common Anode

    So I'm using a common anode RGB LED. I didn't intend on using anode but I ordered the wrong ones. Whatever.... So I've got it wired up correctly and I'm able to get the correct colors I want. I'm just trying to blink on and off through a series of different colors. The problem is when the LED is supposed to be completely off. It's very dim white but not completely off. Has anyone run across this before?

    I tried different RGB LED's including a common cathode LED I had. (I altered the wiring for cathode LED of course.)

    Also, I tried it with an Arduino Uno and it worked perfectly. Is this possibly a difference between Ardunio and Teensy?

    Here's the code:

    int redPin = 5; // Red LED, connected to digital pin 3
    int greenPin = 4; // Green LED, connected to digital pin 4
    int bluePin = 3; // Blue LED, connected to digital pin 5

    void setup() {
    // put your setup code here, to run once:

    }

    void loop() {
    // put your main code here, to run repeatedly:
    rgbOrange();
    delay(1000);
    rgbOff();
    delay(1000);

    rgbPurple();
    delay(1000);
    rgbOff();
    delay(1000);

    rgbYellow();
    delay(1000);
    rgbOff();
    delay(1000);
    }

    void rgbOff()
    {
    analogWrite(redPin, 255); // Write current values to LED pins
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 255);
    }

    // These color values are based on Common Anode RGB LED's, not Cathode so LOW = 1, HIGH = 0
    void rgbOrange()
    {
    analogWrite(redPin, 0); // Write current values to LED pins
    analogWrite(greenPin, 175);
    analogWrite(bluePin, 255);
    }

    void rgbPurple()
    {
    analogWrite(redPin, 0); // Write current values to LED pins
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
    }

    void rgbYellow()
    {
    analogWrite(redPin, 0); // Write current values to LED pins
    analogWrite(greenPin, 75);
    analogWrite(bluePin, 255);
    }
    Click image for larger version. 

Name:	RGB LED.png 
Views:	2057 
Size:	177.9 KB 
ID:	6677

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,088
    analogWrite(pin, 255) will cause a PWM signal that's high 255 out of 256 timer clocks. So you'll still get a tiny amount of light this way.

    To completely turn the LED off, use analogWrite(pin, 256), or pinMode(pin, OUTPUT) and digitalWrite(pin, HIGH).

  3. #3
    Junior Member
    Join Date
    Mar 2015
    Posts
    14
    Thanks for the quick reply.

    I should have mentioned that I started out using pinMode("pin", OUTPUT) and setting the pins to HIGH. When I do that or set the pins to 256, the LED stays a dim red. It's strange because the Arduino Uno doesn't do that.

  4. #4
    Junior Member
    Join Date
    Mar 2015
    Posts
    14
    Ok, so I just noticed that I was using 5V instead of 3.3V. It's working now. I'm assuming that using 5V will eventually damage the LED. It's so much brighter at 5V. Thanks!

  5. #5
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,874
    All the math will be out for a 5V supply, and when output is high you still have 5-3.3=1.7V across the LEDs, giving the result you were seeing. Brightness defined by the resistors you should be using
    http://www.evilmadscientist.com/2012...tors-for-leds/

    to work out what size they should be to maximise brightness while not exceeding the LED max current nor the teensy max current per pin.

  6. #6
    Junior Member
    Join Date
    Mar 2015
    Posts
    14
    I read through the article and I think I understand. I think I have plenty of resistance according to the math. I may be doing it wrong though.

    I'm using 6V to power the Teensy.

    And just a reminder, these LED's are common anode.
    Red: 6V - 2.5V = 3.5V (left over) ==> 3.5V/.020A = 175 Ohms
    Green and Blue: 6V - 4.5V = 1.5V ==> 1.5V/.020A = 75 Ohms

    I'm using 220 Ohms resistors. Shouldn't that be more than enough to take care of the extra voltage? Or am I doing this wrong? I must be because when I power it from the USB the LED's are very dim when off and when I'm powering it from 4 AA's it's much brighter when off.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •