PWM not fully off

Im using pwm to drive an LED driver but ive noticed when i output the code for the pin to be at zero it still has a very small voltage being outputted from the pin, causing the leds to stay on very dim.

I even tried just a straight connection with an led and 1k resistor from the pin and it does the same.

Is there any way of fully turning off the output from the pin in analogWrite??????

???????????????????????????????

You sure you set your pinMode() on that pin first?

i thought i didnt need to set the pinMode as im using it as analog?????

i thought you just set if if you were using it as digital????

am i being dumb?

So i set the pinMode on that pin as an OUTPUT but i still get a very dim light when its supposed to be off.

im still getting about 20mV?

Any ideas?

Does it repro in the simplest case like

#define PIN 9 
setup() {
pinMode(PIN, OUTPUT);
analogWrite(PIN, 0);
}
loop(){}

This is what i have and its still slightly dim:

// Fading LED
// by BARRAGAN http://people.interaction-ivrea.it/h.barragan

#define ledpin 5

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

void loop()
{

{
analogWrite(ledpin, 255);
delay(1000);
}
// fade out (from max to min)
{
analogWrite(ledpin, 0);
delay(1000);
}
}

This seems a bit odd. For one, you would likely not be able to see light coming from 20mv (1/50 of a volt!) even in the pitch black and even if it were hooked up to an incandescent bulb. But, to make matters worse, if you are using an LED then it will do absolutely nothing until you overcome it's forward voltage drop. The forward drop of any LED I've ever heard of is way over 20mv so 20mv would do absolutely nothing. So, I think that you might have something else going on.

Ah-ha! Look here: analogWrite() - Arduino Reference

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs.

I was programming this in my main project sketch and i found when i put the pin to analogWrite (ledpin,0); it would then flicker slightly as the arduino cycled through the program.

So i then put together the above code to find out whats going wrong????

i cant see why the pin doesnt turn off when i ask it to????

This seems a bit odd. For one, you would likely not be able to see light coming from 20mv (1/50 of a volt!) even in the pitch black and even if it were hooked up to an incandescent bulb. But, to make matters worse, if you are using an LED then it will do absolutely nothing until you overcome it's forward voltage drop. The forward drop of any LED I've ever heard of is way over 20mv so 20mv would do absolutely nothing. So, I think that you might have something else going on.

My guess: measurement is taken with a multimeter, not an oscilloscope. Instantaneous voltage will still be a full 5V at the instant the pulse is high. Next pulse it drops low. Repeat quickly and it'll make the needle hover really low above zero but still provide enough voltage to overcome the forward voltage drop of the LED.

I was programming this in my main project sketch and i found when i put the pin to analogWrite (ledpin,0); it would then flicker slightly as the arduino cycled through the program.

So i then put together the above code to find out whats going wrong????

i cant see why the pin doesnt turn off when i ask it to????

  1. Pin 0 is used by the hardware serial library.
  2. Pin 0 is not a PWM pin.
  3. Read the manual. I provided you with a URL.

The led is connected to pin 5.

The ledpin, 0 is the value for the pwm. ie:

analogWrite(ledpin, 0); // sets the value (range from 0 to 255)
delay(1000); // time delay between each pwm fade in pulse
analogWrite(ledpin, 64); // sets the value (range from 0 to 255)
delay(1000); // time delay between each pwm fade in pulse
analogWrite(ledpin, 127); // sets the value (range from 0 to 255)
delay(1000); // time delay between each pwm fade in pulse
analogWrite(ledpin, 191); // sets the value (range from 0 to 255)
delay(1000); // time delay between each pwm fade in pulse
analogWrite(ledpin, 255); // sets the value (range from 0 to 255)
delay(1000); // time delay between each pwm fade in pulse

Time to slow down on the reefer and read, man.

Pin 0: Not a PWM pin AND because you've got your 'Duino plugged in to your PC its always gonna be flickering.

Pin 5: Always going to be a little bit high when doing PWM, explained in the resource I pointed you at.

Time to slow down on the reefer and read, man.

Pin 0: Not a PWM pin AND because you've got your 'Duino plugged in to your PC its always gonna be flickering.

Pin 5: Always going to be a little bit high when doing PWM, explained in the resource I pointed you at.

The original poster never used pin 0 at all. As they stated, the 0 in their example is the PWM level. But, they apparently do need to read the part about how pins 5 and 6 have too high a duty cycle AKA they don't ever actually turn completely off. To fix this the original poster could switch to a different pin (7, 8, 9?)

Time to slow down on the reefer and read, man.

That is going to backfire... :smiley: Thank you Fjornir, you made my day. (Sorry if that seem rude, but it's 04:16 and I haven't slept in a while, and you've got to agree that was a bit funny? :))

OT: I do this:

if( PWM == 0){ digitalWrite( PIN , LOW ); }

That is going to backfire... :smiley: Thank you Fjornir, you made my day. (Sorry if that seem rude, but it's 04:16 and I haven't slept in a while, and you've got to agree that was a bit funny? :))

Ha! No need to apologize. The egg on my face in this one is fully deserved so I will accept it with glee and mirth. Vancouver Reefer it is YOU who I owe an apology -- sorry about that. I swapped the arguments around in my head I guess

OT: I do this:

if( PWM == 0){ digitalWrite( PIN , LOW ); }

AlphaBeta Can you confirm that this special case is only needed for pins 5 and 6?

Well this is one of those threads where when you first start to read it one might think it's a all too frequent wiring or coding mistake type situation, However as others brought nuggets of info and suggestions it got more interesting. So I did what any good hardware guy would do, break out the Tek scope and try and recreate the symptom.

Ran the Reefer's code and checked out pin 5 switching. What is that 4 usec positive leading pulse doing there, during what should be a 0% duty cycle?

Move the code to pin 11 and scope pin 11 output. At duty %0 duty there is no positive pulses to be seen, just flat-line zero volts. Bump the code up to duty cycle 1% and see a 8 usec positive pulse.

So there is some timer interaction on Pin 4 that cause a loss of a true zero output that results in a 1/2% duty cycle output when it should be at 0% duty output. I went ahead and made the code output a duty % of one to see if the error is cumulative or just a zero offset error. I measured 8 usec when at 1% duty so the error is only seen at duty cycle = to 0 and does not seem to effect higher percentage values.

Lastly I put a standard red led on pin 5, 150 ohm resistor to ground and ran the Reefer's program again and yes indeed even at the 'zero' output portion of the program there is a dim glow that can clearly be seen.

So lessons to be learned:

A digital multimeter is not the proper instrument to troubleshoot PWM outputs, a scope is best.

They probably should not have named the function "analogwrite" as that implies attributes that are not true for a digital PWM signal. Beginners are frequently putting a voltmeter on a PWM pin and expecting something other then what is really happening.

I'm sure there there are other lessons here but I'll let the group take in what I found and see if they have comments.

So once again software artifacts cause zero to not be zero and us poor hardware guys have to figure out how to work around it or maybe ignore it, it's just 1/2% error. ;D

Lefty

retrolefty: Can you use your scope and confirm that the problem doesn't happen on a 'normal' PWM pin ?

AlphaBeta Can you confirm that this special case is only needed for pins 5 and 6?

Unfortunantly I've edited the analogWrite, and I do not have an original copy installed :-/

"retrolefty: Can you use your scope and confirm that the problem doesn't happen on a 'normal' PWM pin ?"

As I said in my write up, pin 11 did not show the problem that I could see on pin 5.

Lefty