Getting Started with Timers and Interrupts-

what are the parameters i need, to learn about timers and interrupt?
how can i use interrupt to implement a blinking led at 1Hz?
how can i use interrupt to create a frequency counter?

Hello elijah57

You will find a lot of practical examples in the IDE.

Take a view to gain the knowledge.

How ??, i want to understand how to use interrupts not from the ide but the whole idea of it, so i can implement it from scratch with a code editor like sublime text in C

That is very simple:

<Read - Learn - Make >

and start again

Do you have any resources??, can you explain how interrupts and Timer works and how i can use it to implement either of these, a blinking led at 1sec or a frequency counter ?

Any resources to read, any tutorial or project to make?

Take here a view

read starting from section "Using interrupts"

To learn about timers and interrupts in Arduino, you will need to understand the following parameters:

  1. Prescaler: This determines the frequency of the timer clock, which in turn affects the precision and duration of the timer.
  2. Timer mode: Arduino timers can operate in different modes, such as Normal, CTC (Clear Timer on Compare Match), and PWM (Pulse Width Modulation).
  3. Interrupt enable: You need to enable interrupts for the timer to trigger an interrupt service routine (ISR) when it reaches a certain count value.
  4. Timer compare value: This determines the count value at which the timer should trigger an interrupt or reset itself.
  5. Interrupt service routine (ISR): This is a function that gets called when the timer reaches the specified count value, allowing you to perform a task or execute code at a precise interval.
  6. Timer register: This is the hardware register that stores the current count value of the timer.

To implement a blinking LED at 1Hz using an interrupt in Arduino, you dont need any hard algorithm, you just need to follow some simple steps .You can follow these steps:

  1. Connect an LED to a digital pin on your Arduino board, such as pin 13.
  2. In the setup function, set the pin as an output using the pinMode() function.
  3. Set up a timer interrupt to trigger every 0.5 seconds (or 500 milliseconds). You can use the Timer1 library to set up the interrupt, which provides a convenient set of functions for working with timers in Arduino.
  4. In the interrupt service routine (ISR) for the timer, toggle the state of the LED by using the digitalWrite() function to set the pin to HIGH or LOW.

To create a frequency counter using an interrupt in Arduino, you can use a hardware interrupt to detect a rising edge on an input pin, then use a timer interrupt to measure the time between consecutive rising edges. The frequency of the input signal can then be calculated by taking the reciprocal of the time between rising edges.

1 Like

Assuming that you have the Arduino Uno, you will need to read the ATmega328p data sheet if you want to use timers. There is a specific formula used to calculate the timer frequency. You can think of timers and interrupts as an 'extra' loop function since they both run in parallel with the loop function. The sample code below is for timers. It uses AVR registers.

// Calculations for timer 1 with period 2000ms
// 2000ms corresponds to a CTC frequency of 0.5 Hz
// For prescaler 1024, OCR1A = 16000000/(2*1024*0.5) - 1 = 15624

bool led_state = false;
void setup() {
  pinMode(12, OUTPUT);  // Set pin 12 (LED) as output
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B = bit(CS12) | bit(CS10);             // Set prescalar of timer 1 to 1024  
  TIMSK1 = bit(OCIE1A);                       // Enable compare match A on timer 1
  OCR1A = 15624;
  sei();  // Enable back the interrupts
}

void loop() {
  // Do nothing in the loop function
}

// With the settings above, this ISR will trigger each 1000ms.
ISR(TIMER1_COMPA_vect) {
  TCNT1 = 0;  // Reset the counter
  led_state = !led_state;
  digitalWrite(12,led_state); // Write new state to the LED on pin 12
}
1 Like

The attached file may worth reading/practicing. You are welcome to make any question/query referring to the contents of the attached file.
Ch-10TCLec (1).pdf (443.1 KB)

2 Likes

In fact, when an interrupt occurs and control goes to the ISR() function, the loop() function of the mainline program remains under suspension.

1 Like

Here is a good resource:
https://gammon.com.au/timers

2 Likes

what order should i learn about timers and interrupts, what the prerequisites??

1. Make a print of the attached file of post #11.
2. Look into TC1/TCNT1 (Timer/Counter 1) of Fig-10.1.
3. Check that TC1's size is 16-bit and is composed of two halves.
4. Observe that TC1 can receive its clocking pulse either from internal oscillator or from external source.

5. And so on...

  1. Read the processor data sheet.

Tutorial linked in post #13. Timers first.

Learn about timers before interrupts, in case you run out of time and get interrupted. :slight_smile:

Seriously, it makes no difference. Start reading and see what you can absorb.

1 Like

@GolamMostafa @anon57585045 i want to implemented a frequency counter using Atmega32. i intend to used the timer 0 to count the rising edge of the pulse (i also enabled the overflow interrupt to keep track of the counts) on the T0 pin.

i intend to used Timer 2 to create the timing interval to evaluate the counts of the Timer 0. i know that i'm suppose to configure Timer 2 in CTC mode and set a delay value for it in the 0CR2 register to create a timing interval.
i'm confused on how to set the 0CR2 to get a timing interval of 1000ms and how to use prescalers to get precise timing interval.

What is the usefulness of prescalers with respect to timers and counters . it is used to scale down the frequency of the clock. but why??
why would you want to count(increment) on a longer time period