PWM not Fully Off at 0 [SOLVED]

Having read this post

PWM not fully off - Frequently-Asked Questions - Arduino Forum

and the analogWrite reference, I am still at a loss as to why an analogWrite of 0 to pin 11 (or 9 or 10) does not kill the LED completely. It maintains a nice dim glow, not enough to notice unless you are looking. Pins 5 and 6, OK, so there is timer interaction, but 9,10,11 (different timers 9,10 and 11) is what has me befuddled.

This is revamped old code, but I do not remember it leaving the LED lit while working on it in IDE V1.0. Now I am at V1.6.6, but I don't see why that would make a difference.

/*Write a slow LED fade routine without delay.
* Tried pins 9, 10, 11
* 20mm LED anode to pin 10 to 255 ohm resistor to GND.
* Could not get LED to fully extinguish 
* without using digital write and setting pin 10 to low.
* (digitalWrite commented out in fadeHold().
* Is there a better way?
* 
* 1/3/16 -fab
* 
*/

const int LED_PWM_PIN = 10;
int brightness = 255;
int fadeAmount = 3;
unsigned long previousMillis = 0;
unsigned long interval0 = 100;
unsigned long interval1 = 5000;
unsigned long currentMillis = 0;

void setup(){
  pinMode(LED_PWM_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  currentMillis = millis();
  if(brightness <= 0){
    fadeHold();
  }
  else{
    fade();
  }
}

void fade(void){
  if (currentMillis - previousMillis > interval0){
    previousMillis = currentMillis;
    Serial.println(brightness);
    analogWrite(LED_PWM_PIN, brightness);
    brightness-=fadeAmount;
    }
}

void fadeHold(void){
//  digitalWrite(LED_PWM_PIN,LOW); //added to fully extinguish LED
  Serial.print("5 sec 'Hold', brightness = ");
  Serial.println(brightness);
    if (currentMillis - previousMillis > interval1){
      brightness = 255;
      }
}

Looking for a better way than the digitalWrite 'low' to pin after hitting pwm 0. Seems like that is what should be an unnecessary byte load in the code.

Actually, this is an experiment to learn to fade a bank of high power LEDs from a Meanwell dimming puck, and having tested the puck, it makes no difference as the power LEDs are OFF. Still, I need to know if I am making some error somewhere, and, if there is a better way to fade LEDs.

The final project will start out fading up or down slowly (20 mins), and if a button is pushed while fading, fade up or down quickly(20 sec). The button will toggle the lights from current state to opposite state when pressed while not fading. If held, the button will cause full on or full off depending on starting state.

Having this happen so early in my learning curve for this code, is a bit unnerving, so i wanted to check it out here on the forum!

Thanks,
-fab

I am still at a loss as to why an analogWrite of 0

You assume your code actually writes a zero.

CodingBadly,
Serial.print shows a zero when placed directly before the analogWrite. Also, to get to the fadeHold function, the comparison must be 0 or less.

What am I missing?

-fab

Some kind of wiring diagram?

analogWrite is defined in wiring_analog.c. The levels 0 and 255 are executed as digitalWrite LOW and HIGH.
I'm not sure at what revision the not turning off bug was fixed. Maybe 1.0?

void analogWrite(uint8_t pin, int val)
{
 // We need to make sure the PWM output is enabled for those pins
 // that support it, as we turn it off when digitally reading or
 // writing with them.  Also, make sure the pin is in output mode
 // for consistenty with Wiring, which doesn't require a pinMode
 // call for the analog output pins.
 pinMode(pin, OUTPUT);
 if (val == 0)
 {
 digitalWrite(pin, LOW);
 }
 else if (val == 255)
 {
 digitalWrite(pin, HIGH);
 }
 else
 {//more code for pins and timers)}

You never analogWrite(pin,0). The last level in fade() will be 1. When you test for <=0 you have commented out the digitalWriite(pin,LOW), but have not replaced it with analogWrite(pin,0).

cattledog:
You never analogWrite(pin,0). The last level in fade() will be 1.

Three.

fabelizer:
Serial.print shows a zero when placed directly before the analogWrite.

How do you differentiate between the value associated with the analogWrite and the value not associated with an analogWrite?

Coding Badly,

You say the last value will be 3? And you are correct. I did not realize the 0 was never written to the analog pin.

If I had left the digitalWrite uncommented, the issue would not be visible.

So based on cattledog's reply, I did write the 0 in fadeHold(), and all is well.

The Serial.print showed zero, so I thought I was writing a zero to the pin, but in fact I last wrote a 3.

Thanks to you and cattledog for illuminating my error. It was a stupid one, but isn't that what you do when you're inexperienced?

cattledog, thanks for your help!

-fab