Timer Programming with Interrupts

Achieving a consistent timer circuit on your program may be quite a challenge, especially when the datasheets for your uController is 500 pages long and doesn’t speak clearly. The following code will get you a 1 second interrupt that will allow you to accurately time events on an Arduino Uno. Have Fun!

boolean addCount = false;
int LEDPIN = 13;

void setup() {

TCCR1A = 0;
TCCR1B = 0;                           // same for TCCR1B
OCR1A = 15624;                    // set compare match register to desired timer count:
TCCR1B |= (1 << WGM12);   // turn on CTC mode:
TCCR1B |= (1 << CS10);        // Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);     // enable timer compare interrupt:
sei();                                      // enable global interrupts

void loop(){
Serial.println(” Normal Code”);
Serial.println(“****Special Code****”);
addCount = false;

addCount = true; //Set variable to run portion of code
digitalWrite(LEDPIN, !digitalRead(LEDPIN)); //Blink LED on board to verify code is running

Follow the link below to dig into this deeper.

AVR and Arduino timer interrupts


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s