Pages: [1]   Go Down
Author Topic: Red Pin in RGB Led won't respond to values below 128?  (Read 623 times)
0 Members and 1 Guest are viewing this topic.
Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello! For some reason even though I can dim my blue and green LED pins, the red pin won't dim (it either is completely off or maximum brightness).

I'm using a clear anode RGB LED (link: https://www.sparkfun.com/products/10820 and below is an image of how I've set up my board:



Here's my code:

Code:
int redPin = 12;
int greenPin = 11;
int bluePin = 10;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
}

void loop()
{
  setColor(100, 0, 0); // Red
  delay (1000);

  setColor(0, 100, 0); // Green
  delay (1000);

  setColor(0, 0, 100); // Blue
  delay (1000);
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, 255-red); //I did the 255- because I'm using an anode LED
  analogWrite(greenPin, 255-green);
  analogWrite(bluePin, 255-blue);  
}

If you look at my code, when I try that RGB value my LED red pin won't turn on at all, even though the green and blue pins do light up but very dim. I'm trying to get my red pin to dim to help better fuse colors but it's not working.

Any value below 128 for the red doesn't work, and anything above it turns it on and it acts as if it were 255. Any help would be appreciated!  smiley-cry
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3844
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's not on a PWM pin
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow thank you so much!!!! Being new to all this, I can't believe how dumb my mistake was. May I ask why it would work on a PWM pin but not on one like pin 12? Just curious thanks!
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3844
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because analogWrite provides a "pseudo voltage" between 0 and 5 by rapidly switching between 0 and 5. If you analogWrite a value of 255, it's permanently on at 5v, value of 0 is off. A value like 128 is 50/50 off and on between 0 and 5, and it gives the illusion of 2.5v to the device. It's not really ever 2.5 of course, just seems like it on average. A value of say 200 is 200/250 = 0.8 so it looks like 4v

PWM = Pulse Width Modulation is the fancy name given to that technique of rapid offing and onning. As you now know, not all digital pins can do that, only the ones marked as such on the board.
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Los Angeles
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That definitely sums it up! Thanks a lot!!!
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3844
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And by the way just in case it's not clear, if you analogWrite to a non-PWM pin, which only knows about always off or always on, with no rapid switching, then if you send a value under 128 it takes that as 100% off, and above it's 100% on, which is what was happening to you.

More here.
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Pages: [1]   Go Up
Jump to: