Go Down

Topic: bug found in core, wiring_digital.c, function turnOffPWM(uint8_t timer) (Read 2585 times) previous topic - next topic


Jul 02, 2014, 08:02 am Last Edit: Jul 04, 2014, 07:34 am by gonzoveliki Reason: 1
Attention: This BUG affects only AVR based platforms which are using "STANDARD" Arduino Core, and not the new ARM based ones like DUE and Teensy. Also it doesn't affect Teensy 2.0 which is featuring AVR32U4 chip and is using completly different code instead of digital_wiring...

Recently I've found out that the function from core library wiring_digital.c
void turnOffPWM(uint8_t timer);
simply doesn't  switch off the associated timer properly, but only in case when it concerns TIMER0B (pwm pin 5 on UNO).
This erratic behaviour was burdened me for a while, so I focused to sort it out.
It has been out there since 2010, after the removing of previous inline functions for code optimisation, perhaps by David Mellis or Mark Sproul who did these modifications:

// - Removed inline. Save 170 bytes on atmega1280
// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
// - Added more #ifdefs, now compiles for atmega645

This could be a typo mistake, or copy/paste on the wrong place, what do you think?

Now I think that I do solved it.


void setup() {
 analogWrite(5, 127); // 127 could be any between 1  and 254
 digitalWrite(5, LOW); // LOW or HIGH doesn't matter it will not work
 // and even
 analogWrite(5, 0); // 0 or 255 is again will don't make any change
void loop(){};....

In general, once PWM operation is commenced on pin 5, you can
only change the duty cycle only from 1 to 254 ...


I think it should be
#if defined(TCCR0A,) && defined(COM0B1)
instead of
#if defined(TIMER0B) && defined(COM0B1)

Am I correct or missing something here?


static void turnOffPWM(byte timer) {
 switch (timer)  {   
... ... ...
#if defined(TIMER0B) && defined(COM0B1)
      case  TIMER0B:  cbi(TCCR0A, COM0B1);    break;
... ... ...


No time to confirm, so please create an issue here - https://github.com/arduino/Arduino -

+ provide a good title e.g. "wiring_digital.c, function  turnOffPWM(uint8_t timer) does not switch off properly "
+ platform involved
+ add link to this thread
+ add your proposal for solution

(it might take some time)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)




Hi, I have posted the report and solution here:


Hope it will be accepted and fixed,



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131