Timers used by Nano

I have successfully used timer 2 and interrupts on previous projects but was not using PWM on any pin. On a new project I would like to use timer 2 to time certain events, but I also want to use several PWMs. I have searched, but but find contradictory information on which timers are used by which pins for the analog write function on the Nano. I would like to avoid any conflict between my use of a timer and the analog write functions.

Is there an authoritative description that indicates what each timer is used for in the Nano prior to invoking any of the libraries?

Classic nano uses atmega328, same as Uno. Timers will be used just the same as Uno. Perhaps you can find information on Uno.

What kind of events do you want to time?

Note that timer0 is also used for the millisecond tick.

Here it is:
pwm328x

1 Like

Yes, there is an authoritative description that indicates which timers are used for PWM on the Arduino Nano.

In general, the Arduino Nano uses three timers: Timer0, Timer1, and Timer2. Timer0 is used for the delay() function, so it should not be used for anything else. Timer1 is used for the Servo library, so it should not be used for PWM unless you don't plan to use Servo.

Timer2 is used for the Tone() function and for the PWM output on pins 3 and 11. The other PWM outputs on the Nano (pins 5, 6, 9, and 10) are controlled by Timer0 and Timer1, so you can use those timers for PWM if you're not using the Servo library.

If you want to use Timer2 for your own timing functions, you can still use PWM on pins 3 and 11 by using the analogWrite() function. However, you should not use Timer2 for any other purpose while using PWM on those pins.

Overall, it's a good idea to carefully plan your use of timers on the Nano to avoid conflicts between different functions. You can refer to the datasheet for the ATmega328P microcontroller used in the Nano for more detailed information on timer usage.

1 Like

(written by AI?)

The millis()/delay() functions only use the timer0 overflow interrupt, which means that you can use other timer features without disturbing it, as long as you don't change the overall period/prescaler. (that's how it's used for analogWrite(). But you could also tie an ISR to one or more of the compare match registers to get an additional periodic interrupt, for example.

2 Likes

At what prescaler TC0 is running? Is there any preset count for TC0? Knowing these parameters will help to create suitable Time Ticks using compare match events.

Prescaler is 64 (16MHz/64/256 = 976Hz overflow frequency. (1024 us))
Since it's using overflow of the 8bit timer, there's no other "count" involved.

The overflow event occurs at 1024 us interval. How has delay(1) been implemented -- is it based on OCF0A/OCF0B (Fig-1) event which can be mad to occur at exactly 1 ms interval by counting 250 (1000/4) driving clock pulses?


Figure-1:

AI generated content ?

And yet, there are cool Text Books in the market, posts in Arduino Forum; because, the data sheets' description, in most cases, are of bureaucratic type.

Microcontroller timers certainly seem to come with a lot of weird-looking features that are probably aimed as SOME particular application that is not fully described in the datasheet (for instance, I'd be hard-pressed to explain why the difference between "fast PWM" and "phase correct PWM" in the AVR timers is ever important, or exactly why there are 5 different types of timer in the new AVR-Dx chips.)

But the datasheet is certainly the starting point. Followed by vendor "App notes" and other info. The text book you'd need might be some obscure tome about motor control, rather than an "intro to AVR" book.

How has delay(1) been implemented -- is it based on OCF0A/OCF0B (Fig-1) event which can be mad to occur at exactly 1 ms interval by counting 250 (1000/4) driving clock pulses?

It ends up calling micros() rather than millis(), and micros() checks the timer registers. It doesn't use the compare registers; it does essentially ("ticks * 1024 + TCNT0*4)
I wouldn't count in delay() being accurate to withing 1ms (though I think it should be withing 2ms.) (It's better than it used to be, BTW.)

(Heh. This means that the oft-touted "blink without delay" methodology is going to generate less accurate pulses than delay() ! Not that that's relevant for the intervals that it is talking about.)

1 Like

Thank you. I had looked there without success and it may be one of some contradictory info I got.

On this project I want to do something conditionally 400 us or 700 us after the falling edge of PWM on pin 5. To test it out I timed it successfully by polling in the main loop (using micros() - old_micros > 400)test, but do not want to burn CPU cycles.

Thanks,
I do not fully understand this snip from the pins.h file and will have to study it further. Thanks for the direction and response.

Thanks,
This is clear and concise. It is consistent with my further experiments. Since my post, I wrote code to set up timer 2 in CTC mode, and an ISR on the COMPA_vect. With the timer off, I loaded 0 into the counter, 50 for 400 us into compare register, then turned on the prescale to divide by 128. 417 us later the diagnostic bit I toggle in the ISR toggled. It worked. Further the timing of PWM running on pin 5 remained unchanged, which proved that the pin 5 PWM was not on timer 2.

I make the last statement because one source, which I can no longer find, said that PWM on Nano pins 5 and 17 use timer 2.

My oscilloscope and cheap 8 bit logic analyzer confirm the period of PWM on pin 5 is 1024 us. I presume the chart you showed is a rounded value.

Thanks,
I think timer 0 is also the base for the micros() function and I'd certainly not want to mess with it. I frequently use micros() in my projects.

I have not used either the servo library nor the Tone() function.
I agree, I want to carefully plan my timer use and hence the reason for my question after finding contradictory information.

I proceeded to experiment and partially answered my question. See my response to Golam Mustafa.

1 Like

Yes, the datasheets are very dense with detail and sometimes very hard to read. In most cases, I have found they do contain the detail often left out by Arduino in the interest of simplifying the broad use case. However my question was not how to use a timer but how was it used by Arduino so I could avoid conflicting with their use.

The fast PWM mode starts a pulse at the same time in a period whereas with the phase correct mode the pulse can be centered in the period. An analogy might help. We usually stand near the highest point of a child's swing and push once the child reaches the peak. This would be like the fast PWM mode. But one might stand to the side of the lowest point and push as the child swings by from a little before to a little after the swing goes by. This would be analogous to the phase correct mode. Some systems are sensitive to the phase change inherent in the fast PWM mode.

20 years ago, stand alone timer chips were much more complex than these, These I notice focus on PWM probably because so many microcontrollers use PWM functions to create switching power supplies. I have used timers to count external events, time events, create delays, synthesize frequencies, block data for a period of time. It was often possible to trigger one timer from another without software intervention.

I agree with you the data sheet and any app notes are often the best sources of information.

What is pin 17?
A0 - A5 have been assigned numerical values: 14 - 19.
So, 17 corresponds to A3 on which there is no PWM signal.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.