Skip to main content
replaced http://arduino.stackexchange.com/ with https://arduino.stackexchange.com/
Source Link

The Why does an LED sometimes flash when increasing brightness?Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9). That question refers to this post at forum.arduino.cc which gives the beginnings of an answer.

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9). That question refers to this post at forum.arduino.cc which gives the beginnings of an answer.

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9). That question refers to this post at forum.arduino.cc which gives the beginnings of an answer.

simplified solution, greatly expanded explaination
Source Link

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9). That question refers to this post at forum.arduino.cc which gives the beginnings of an answer.

ForThe problem is caused by the fact that analogWrite(led, 0) does not use PWM, it just sets the pin 9to LOW. So when you call analogWrite(led, 0), the value in the Output Compare Register (1AOCR) is left at 255. When you call analogWrite(led, 1), the OCR value is changed to 1, but due to double-buffering this doesn't take effect until the next timer cycle begins. So for a fraction of the current timer cycle the output pin is left at HIGH.

The solution is to either avoid calling analogWrite(led, 0) or to explicitly set the OCR value to 0 when analogWrite(led, 0) is called. This will only work if another call to analogWrite() is not made in the current timer cycle. Given that the default timer cycle takes 32 microseconds, this is unlikely to happen. 32 microseconds = 256 (ticks per timer cycle) / 16MHz (clock ticks per second) * 2 (default timer prescale factor).

For pin 9, the solution was to add these lines immediately after the brightness was set to 0 at the bottom of the example codeanalogWrite() call:

if (brightness >== 2550) {
    brightnessOCR1A = 0;
}

For other pins, use the correct Output Compare Register. For the Arduino UNO they are:

+-------------+----------+-------+
| Arduino Pin | sbi(TCCR1A,Chip COM1A1);Pin //| connectOCR pwm to pin|
+-------------+----------+-------+
| on timer 1, channel A      3 |        5 | OCR2B |
|           5 |       11 | OCR0B |
|           6 |       12 | OCR0A |
|           9 |       15 | OCR1A =|
| 0; // set pwm duty     10 |       16 | OCR1B |
}|          11 |       17 | OCR2A |
+-------------+----------+-------+

Of course this solution will need to be modified if you have changed anything related to timers such as the PWM mode.

The solution given by the forum.arduino.cc post indicated that the Timer/Counter Control Register Secrets of Arduino PWM article is must-read for anyone trying(TCCR) needed to understandbe modified in addition to setting the issueOCR value. This modification was to configure the PWM mode, but the PWM mode would have already been correctly configured by any previous call to analogWrite() for that pin.

Others have suggested that calling analogWrite(led, 1) before analogWrite(led, 0) would be a simpler solution, but that still results in a flash, albeit shorter.

Resources:

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9).

For pin 9 (1A), the solution was to add these lines after the brightness was set to 0 at the bottom of the example code:

if (brightness > 255) {
    brightness = 0;
    sbi(TCCR1A, COM1A1); // connect pwm to pin on timer 1, channel A
    OCR1A = 0; // set pwm duty
}

The Secrets of Arduino PWM article is must-read for anyone trying to understand the issue.

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9). That question refers to this post at forum.arduino.cc which gives the beginnings of an answer.

The problem is caused by the fact that analogWrite(led, 0) does not use PWM, it just sets the pin to LOW. So when you call analogWrite(led, 0), the value in the Output Compare Register (OCR) is left at 255. When you call analogWrite(led, 1), the OCR value is changed to 1, but due to double-buffering this doesn't take effect until the next timer cycle begins. So for a fraction of the current timer cycle the output pin is left at HIGH.

The solution is to either avoid calling analogWrite(led, 0) or to explicitly set the OCR value to 0 when analogWrite(led, 0) is called. This will only work if another call to analogWrite() is not made in the current timer cycle. Given that the default timer cycle takes 32 microseconds, this is unlikely to happen. 32 microseconds = 256 (ticks per timer cycle) / 16MHz (clock ticks per second) * 2 (default timer prescale factor).

For pin 9, the solution was to add these lines immediately after the analogWrite() call:

if (brightness == 0) {
    OCR1A = 0;
}

For other pins, use the correct Output Compare Register. For the Arduino UNO they are:

+-------------+----------+-------+
| Arduino Pin | Chip Pin | OCR   |
+-------------+----------+-------+
|           3 |        5 | OCR2B |
|           5 |       11 | OCR0B |
|           6 |       12 | OCR0A |
|           9 |       15 | OCR1A |
|          10 |       16 | OCR1B |
|          11 |       17 | OCR2A |
+-------------+----------+-------+

Of course this solution will need to be modified if you have changed anything related to timers such as the PWM mode.

The solution given by the forum.arduino.cc post indicated that the Timer/Counter Control Register (TCCR) needed to be modified in addition to setting the OCR value. This modification was to configure the PWM mode, but the PWM mode would have already been correctly configured by any previous call to analogWrite() for that pin.

Others have suggested that calling analogWrite(led, 1) before analogWrite(led, 0) would be a simpler solution, but that still results in a flash, albeit shorter.

Resources:

Source Link

The Why does an LED sometimes flash when increasing brightness? question does exactly describe my problem (except for the substitution of pin 11 for pin 9).

For pin 9 (1A), the solution was to add these lines after the brightness was set to 0 at the bottom of the example code:

if (brightness > 255) {
    brightness = 0;
    sbi(TCCR1A, COM1A1); // connect pwm to pin on timer 1, channel A
    OCR1A = 0; // set pwm duty
}

The Secrets of Arduino PWM article is must-read for anyone trying to understand the issue.