At the moment I'm trying to learn microcontrollers with Arduino, and have a problem with the timer.

Arduino works at 16MHz clock, and the best prescaler is 1024, which means the timer increments the value every 64 us. Timer 3 is a 16 bit timer, which gives us a maximum time of 64*65535= 4194240, ca. 4s

But what if I want to do a job every 8 seconds? Any idea?

Here's my code:

TCCR3A = 0;
TCCR3B = 0;  
TCCR3B |= (1<<WGM32); //CTC
TCCR3B |= (1 << CS32);
TCCR3B |= (1 << CS30);// 1024 prescaler 
TIMSK3 |= (1<<OCIE3A); 
  • 2
    Schedule it every second time the interrupt fires. Commented Jan 9, 2017 at 22:31

3 Answers 3


You can't - not directly. Instead you do a job at a higher frequency which just counts. When it has counted enough you do your other job.

For instance, you might trigger your interrupt once a second. That interrupt just adds 1 to a count and returns. Unless that count just hit 8, when you reset the count to 0 and run your 8 second routine.

  • 1
    +1 This is indeed generally how it is done. Though if the system doesn't need to do much else or more frequently, you can ultimately save power by activating the divider on the MCU clock itself (and/or put the MCU to sleep in between events). However those are advanced topics and introduce various complications that may not be warranted for the current goal. Commented Jan 9, 2017 at 23:35

You know that the inbuilt function millis() would let you achieve that goal?

For example:

unsigned long startTime;

void loop ()
  if (millis () - startTime >= 8000)
    startTime = millis ();  // reset start time to now
    // do something that needs doing every 8 seconds

  // do other stuff

  }  // end of loop

If the 5% error (~4.2 sec vs. 4.0 sec) isn't a problem, 2 counter overflows will give 8 seconds with the same 5% error, or ~8.4 sec. If you need higher accuracy, either start the timer with a slightly smaller initial counter value, (62500 * 64 = 4000000 uSec), choose a divider value that naturally overflow at convenient sub-multiples of the intervals your program will need, and again count N overlows of the timer before your code responds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.