I have LEDs (with current limiting resistors) on pin 3 and 11. However, when I run the code, they glow. When I ramp up the duty cycle, they get brighter as expected. Also, shortening the duty cycle, they dim. However, setting OCR2A or B to 0 still generates a pulse. Is there a way around this issue short of changing the pin to input mode at 0? I have seen a 0% duty cycle with the phase corrected mode. Just can't get that in Fast PWM mode.
I had a similar issue and that's how I fixed it. But I used the following code controlling the duty cycle with a potentiometer....
const int led = 3; //set digital pin 3 as led pin
const int pot = A0; //set analog pin 0 as input
int pwm_dc =0; //set a value to control duty cycle
void setup();
{
pinMode(led, OUTPUT); //set led(pin 3) as output
TCCR3A = _BV(COM3A0) | _BV(COM3B1) | _BV(WGM31); //Set PWM frequency to 245hz on pin 3
TCCR3B = _BV(CS21) | _BV(CS20);
}
void loop()
{
pwm_dc = map(analogRead(po)0,1023,0,255); //read anaolg pin and map it's value
analogWrite(led,pwm_dc); //writes pwm to led depending on pot value "pwm_dc"
}
This code will vary the brightness of the led depending on the output of the potentiometer.
NOTE: this is on Timer3 using a Mega 2560. You can use whatever timer you prefer as long as it doesn't affect millis() or micros().