PWM pins only sending 0v at 0 and +5v at 255 - nothing in between

Hi,

I am using a Mega 2560.

I am trying to simply fade an LED. I have the negative lead of the LED connected to ground and the positive lead connected to pin 9 (though I've tried various PWM pins too) of the arduino via a 200ohm resistor.

The issue is that the LED only comes on when I write 255 to the pin and only goes off when I write 0. No other values have any effect; if the LED is on 1-254 will not turn it off, and if it is off 1-254 will not turn it on. I have measured the voltage with a multimeter and confirmed that the pin is outputting ~+5v at 255 and 0v at 0.

Here is some example code which makes the LED blink:

int led = 9;
bool on = true;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
 if (on) {
   analogWrite(led, 255);
 } else {
   analogWrite(led, 0);
 }
 on = !on;
 delay(1000);
}

If I change the first analogWrite call to use 254 (or any other value), the LED never comes on and I never see anything other than 0v between the pin and ground. If I change the second analogWrite call to use 1 (or any other value), the opposite happens - the LED never turns off and I only ever see +5v.

I can't see where I'm going wrong here. I've also tried the example fade sketch and that behaves similarly - only 0 and 255 have any effect.

Am I looking at a hardware issue?

Hi, Have you tried a different PWM pin?

Tom.. :)

Changing LED pin to 13 (onboard LED), and analogWrite value to e.g. 32 works fine (dim flashing onboard LED). Leo..

You show us code that works (using code tags! kudos for you!)

Then you describe a condition that does not do what you expect but do not show us the code. Can you show us the code that does something other than what you expect?

Also, in your description of what is connected where, you do not mention a current limiting resistor for the LED. Most examples do put a resistor in series with the LED.

Oh, using a multi-meter to read PWM is going to give confusing results. The pin changes from 0 to full voltage and back very quickly. A scope will show that graphically. Multi-meters, not so much.

Thanks all for your responses.

TomGeorge: Hi, Have you tried a different PWM pin?

Tom.. :)

Yes I have tried various PWM pins (eg 2, 3, 12) with the same result.

Wawa: Changing LED pin to 13 (onboard LED), and analogWrite value to e.g. 32 works fine (dim flashing onboard LED). Leo..

I have tried disconnecting everything and using pin 13. Passing 0 and 255 to the analogWrite calls causes the LED to blink on and off as expected. Interestingly other values do not cause the same behaviour as using an external LED. I get a different flashing pattern with the internal LED (not 1s on and 1s off, more of a long flash followed by a short flash over and over). This happens for any value other than 255.The brightness is the same regardless of whether I use 32, 254 or any other value and I only get a stable 1s blink when I use 0 and 255.

Wawa: Changing LED pin to 13 (onboard LED), and analogWrite value to e.g. 32 works fine (dim flashing onboard LED). Leo..

vinceherman: Then you describe a condition that does not do what you expect but do not show us the code. Can you show us the code that does something other than what you expect?

Sure. The following prevents the LED from ever coming on. The only change from the working code is I've used 254 rather than 255 for the "on" analogWrite call. As I understand it, this should result in the LED coming on, albeit marginally dimmer.

int led = 9;
bool on = true;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
 if (on) {
   analogWrite(led, 254);
 } else {
   analogWrite(led, 0);
 }
 on = !on;
 delay(1000);
}

The following prevents the LED from ever turning off. Again I've just made one change from the working code; using 1 rather than 0 for the "off". I would expect an extremely dim if not off LED here, but that does not happen.

int led = 9;
bool on = true;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
 if (on) {
   analogWrite(led, 255);
 } else {
   analogWrite(led, 1);
 }
 on = !on;
 delay(1000);
}

vinceherman: Also, in your description of what is connected where, you do not mention a current limiting resistor for the LED. Most examples do put a resistor in series with the LED.

I actually did mention this but perhaps wasn't clear :)

I have 2 x 100 ohm resistors in series with the LED. My circuit is basically the following, all in series (apologies for the lack of proper diagram).

Ground -> LED cathode -> LED anode -> 100ohm resistor -> 100ohm resistor -> PWN pin.

vinceherman: Oh, using a multi-meter to read PWM is going to give confusing results. The pin changes from 0 to full voltage and back very quickly. A scope will show that graphically. Multi-meters, not so much.

With a 1s delay between each analogWrite call I hoped it would be enough to at least rule out an issue with my LED circuit. FWIW, when I use and 0 and 255 I do see the expected switching between 0 and +5v every 1s. Unfortunately I don't have a scope.[/quote]

Hi, Can you post a picture of your project so we can see your component layout please?

Thanks.. Tom.. :)

Try compiling and uploading this minimal program for testing PWM pin 13 on a Mega.

void setup() {
}
void loop() {
  for(int i=0;i<256;i++){
    analogWrite(13,i);
    delay(10);
  }
}

Load Examples > 01.Basics > Fade

Change int led = 9; to int led = 13;

Nothing attached to the Mega, just the USB lead.

If that does not pulse/fade the onboard LED, then the Mega might be damaged.
Leo…