Questions tagged [timers]

Timers are the hardware in the processor used to count and time events. Use this tag for questions about the hardware timers.

The Arduino processor chips have a number of timers/counters. Basically they are hardware that count things, however if they count pulses from the system clock (which runs at a known speed) they can also be used to time things.

Timers have an input which can be an external signal, or the system clock. If counting an external signal they can be used to count something like how many pulses are detected on a pin. If counting the system clock they can be used as an elapsed-time timer.

The input can go through a prescaler which divides down the input (eg. by 64). This lets you have the counter "tick over" more slowly than it otherwise might.

Timers are usually described as 8-bit, 10-bit, 16-bit and so on. The number of bits indicates the maximum the counter/timer can count to before overflowing. For example, an 8-bit counter can count to 256.

Timers can be configured to raise interrupts on various events, for example:

  • Timer overflow
  • A certain count has been reached

The timers can also be used to generate pulses. PWM (pulse-width-modulation) can be configured to have a certain frequency, and a certain duty cycle, depending on the way the timer registers are set.

451 questions
14
votes
4 answers

Using millis() and micros() inside an interrupt routine

The documentation for attachInterrupt() says: ... millis() relies on interrupts to count, so it will never increment inside an ISR. Since delay() requires interrupts to work, it will not work if called inside an ISR. micros() works initially, but…
Petr
  • 253
  • 1
  • 2
  • 12
11
votes
3 answers

Could someone explain this weird looking code, used to setup timers?

While looking at sketches other people have written, I occasionally come across code that looks somewhat like this: TCCR1A = 0; TCCR1B = 0; TCNT1 = 34286; TCCR1B |= (1 << CS12); TIMSK1 |= (1 << TOIE1); All I know is that is has something to with…
The Guy with The Hat
  • 5,292
  • 7
  • 30
  • 51
11
votes
2 answers

Are function pointer assignments atomic in Arduino?

The following snippets are from TimerOne library source code: // TimerOne.h: void (*isrCallback)(); // TimerOne.cpp: ISR(TIMER1_OVF_vect) // interrupt service routine that wraps a user defined function supplied by attachInterrupt { …
Joonas Pulakka
  • 350
  • 2
  • 11
10
votes
1 answer

Setting timer3 in CTC mode - conflict with servo library

I would like to set up a timer in order to call a function 800 times per second. I'm using the Arduino Mega and Timer3 with a prescaler of 1024. To choose the prescaler factor I've considered the following steps: CPU freq: 16MHz Timer resolution:…
UserK
  • 559
  • 1
  • 11
  • 24
10
votes
5 answers

Can I make delayMicroseconds more accurate?

I'm trying to bit bang DMX data and that requires 4us pulses. Not having much luck with the results I'm checking to see how good the Arduino is at delaying... Seems to be pretty terrible at it. Here's a quick little test I did: unsigned long…
bwoogie
  • 203
  • 2
  • 9
9
votes
2 answers

How to update a variable in an ISR using Timers

I'm trying to check the frequency of Timer3 using a counter. The value of the counter, declared as volatile, is incremented in the ISR and every second the sum is shown in the main loop and the value reset to zero. The timer has been set up…
UserK
  • 559
  • 1
  • 11
  • 24
9
votes
5 answers

How can an Arduino output a specific (i.e. 56 kHz) carrier frequency?

I'm working on a free space optics project to send data wirelessly between two points. To accomplish this I'm using an IR LED connected to an Arduino Uno that pulses with a 56 kHz carrier frequency for the transmitter and a second Arduino with a…
jlbnjmn
  • 978
  • 2
  • 9
  • 15
9
votes
6 answers

Very long delay() possible?

I'm trying to make an opening and closing little door that should open or close every 12 hours. I was wondering if I could just make a small looping script with a delay() for 12 hours, delay(43 200 000 000); I guess? However, I have no idea if…
Fred Pannekoek
  • 93
  • 1
  • 1
  • 5
9
votes
4 answers

Turn Arduino on with timer every 24h

I'm currently working on an Arduino project that sends the charge of my car battery once a day via an ESP8266 to me. During the winter I don't drive my car, and I would like to get informed when the battery drops beyond a critical value, so I can…
Dirk Pitt
  • 133
  • 1
  • 5
9
votes
1 answer

Is volatile needed when variable is accessed from > 1 ISRs, but not shared outside ISRs?

It's clearly documented that when global data is shared with an ISR and the main program, the data needs to be declared volatile in order to guarantee memory visibility (and that only suffices for 1-byte data; anything bigger needs special…
Joonas Pulakka
  • 350
  • 2
  • 11
9
votes
2 answers

Does millis() conflict with the PWM pins associated with timer 0?

I've read that the millis() function uses the same timer as a couple of PWM pins. If you're using those PWM pins, will millis() still return the correct value?
Aurast
  • 295
  • 2
  • 7
8
votes
2 answers

When setting hardware timers as interrupts, should I prefer a lower prescaler value or a lower CTC?

I'm currently playing with Arduino's hardware timers, and a question came to my mind. Let me explain it a bit. Let's suppose I want a certain function to execute every 1024 clock ticks. AFAIK, I could achieve this in several ways, playing with the…
Nadia.moe
  • 203
  • 1
  • 5
8
votes
1 answer

The difference between "time_t" and "DateTime"

I've run into an issue trying to combine 2 different Arduino timer projects I've found online. For both projects I'm using a DS3231 RTC, but have been able use the DS1307 library just fine in my code. The first set of code I picked up started…
Vinterwoo
  • 259
  • 2
  • 4
  • 7
7
votes
6 answers

Is it possible to generate an exact 15 kHz clock pulse using an Arduino?

I want to generate a 15 kHz pulse with an Arduino using Timer1, but the problem is that if we want a 15000 Hz clock we need to initialize the timer with 1/15000 seconds or 66.66 microseconds, but we can only pass integers without any decimal…
astrick
  • 193
  • 1
  • 9
7
votes
3 answers

Write PWM with only 8 bit? (Timer2)

I have a problem with the Servo library. I need to read a PPM signal and create a PWM signal of each channel. The problem is that PPM has a higher resolution than PWM so I want to use the 16 bit Timer1 for reading PPM. However the Servo library…
betion
  • 115
  • 2
  • 6
1
2 3
30 31