9
votes

Just got an Arduino and I'm messing around having some problems with the lights.

I believe I am using a common anode led, so I have the three color pins going through a 270 ohm resistor to 10, 11, and 12 on the arduino. The last is hooked directly to VSS.

Much like this: http://www.instructables.com/id/RGB-LED-Tutorial-using-an-Arduino-RGBL/step2/Testing/

Now, it is working oppositely to what I would predict. When I write analogWrite( red, 0 ) , the led is lit red, and 255 turns it off completely.

Now, adjusting the value from 0-254 barely adjusts the brightness at all. 255 is completely off after it flashes for a second.

Can anyone explain what exactly is going on?

3
Hm... Pin 12 is not a PWM pin on my Arduino. So analogWrite will work not as expected for that pin.A.H.

3 Answers

9
votes

Your LED is common anode so it works just the opposite of common cathode. Try this:

int PWM_value = xxx;
analogWrite(red, 255 - PWM_value);
3
votes

AS A.H. points out, Pin 12 is not a PWM pin. Try to change your code to use pins 9,10,11 instead of 10,11,12 and give it another shot.

Here's a modified version of the documentation sample to illustrate the idea:

int rPin = 9;     
int gPin = 10;     
int bPin = 11;     
int analogPin = 0;   // potentiometer connected to analog pin 0
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(rPin, OUTPUT);   // sets the pin as output
  pinMode(gPin, OUTPUT);   // sets the pin as output
  pinMode(bPin, OUTPUT);   // sets the pin as output
}

void loop()
{
  val = analogRead(analogPin);   // read the input pin
  analogWrite(rPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
  analogWrite(gPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
  analogWrite(bPin, val / 4);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}

Note that you need a sensor hooked to analog pin 0 to change led values.

1
votes
[http://arduino.cc/en/Reference/analogWrite][1]

Here you have not altered anything.Just updating PWM pins