9

I've read that the millis() function uses the same timer as a couple of PWM pins.

If you're using those PWM pins, will millis() still return the correct value?

6

They do not conflict as millis() strictly reads the immediate value in TCNT0 whereas PWM via timer 0 uses the hardware's ability to compare the value of TCNT0 with the values in OCR0x without affecting the value of any of them.

  • millis() just uses Timer 0 overflow counts. micros() reads the immediate value in TCNT0. – Nick Gammon Mar 24 '16 at 22:53
5

Just to add to @Ignacio's answer which has directly answered your question. The "conflict" you speak of is in relation to Timer0's prescaler.

For the most part, you can use those pins (incidentally pins 5 and 6 on the UNO) with PWM without an issue, and read the correct value of millis() (as well as get the expected delay from delay)

Where you run into problems is if you want to change the prescaler (usually to modify the frequency of the PWM signal). If you change this, then you directly affect the calculation and reporting of millis() and the length of delay(x).

  • You can read your timer's prescaler by the bottom bits of Serial.println(TCCR0A,BIN); If they are '11' then your prescaler is clk/64. You could then calculate the factor for modifying millis() and delay(). – Dave X Feb 24 '16 at 14:59
  • That's a very good point and I've always just steered clear of modifying it, but there's no reason if you really want/need to. The other point is that if you've modified it you already know the new prescaler and could in turn write wrappers for millis () or delay () anyway – Madivad Feb 24 '16 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.