Marcazzan_M
Published © GPL3+

Internal Timers of Arduino

In this tutorial I will explain how to use the TIMER0 of Arduino

IntermediateFull instructions provided1 hour77,163
Internal Timers of Arduino

Things used in this project

Story

Read more

Schematics

schema_jlNkRtLRSE.jpg

Code

Internal timer

Arduino
/*
This program turns on and off a LED on pin 13 each 1 second using an internal timer
*/

int timer=0;
bool state=0;
void setup() {
  pinMode(13,OUTPUT);
    
  TCCR0A=(1<<WGM01);    //Set the CTC mode   
  OCR0A=0xF9; //Value for ORC0A for 1ms 
  
  TIMSK0|=(1<<OCIE0A);   //Set the interrupt request
  sei(); //Enable interrupt
  
  TCCR0B|=(1<<CS01);    //Set the prescale 1/64 clock
  TCCR0B|=(1<<CS00);

 
    
}

void loop() {
  //in this way you can count 1 second because the nterrupt request is each 1ms
  if(timer>=1000){
    state=!state;
    timer=0;
  }
  
  digitalWrite(13,state);
  
}

ISR(TIMER0_COMPA_vect){    //This is the interrupt request
  timer++;
}

Credits

Marcazzan_M

Marcazzan_M

6 projects • 67 followers
I'm an Italian student, I'm studying electronic engineering. Sufficiently advanced technology is indistinguishable from magic.

Comments