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.

445 questions
3
votes
1 answer

Timer Accuracy Always off by 1uS

I am using the ATMega32u4 to generate a pulse at 1 kHz. I am using Timer 3, which is a 16-bit timer and using a prescaler of 1, so I have a 0.0625uS resolution. The good news is that this is working, I can control the frequency and pulse length of…
M.Schindler
  • 221
  • 1
  • 5
3
votes
1 answer

Why is no PWM signal generated using Timer 2?

I managed to work with timer 0 and 1 but am unable to generate a PWM with timer 2 in my atmega328p. This is the code: #include int main(void) { /* PD6 output */ DDRD |= (1 << DDD6); /* 50% duty cycle */ OCR2A = 128; …
traducerad
  • 143
  • 3
3
votes
4 answers

Arduino timers. How they work

Explain this line in simple words. TCCR0B=TCCR0B&0b11111000|0x01; As the variuos arduinos have different chips. They also have different timers and different pins which use them. Lately i want to rewrite a code(Arduino Uno) to make it work on a…
cocco
  • 431
  • 5
  • 7
  • 15
2
votes
1 answer

Arduino Timer Precision

I am using the ATMega32u4 to generate multiple pulses at 2.5 kHz. I am using Timer 3, which is a 16-bit timer and using a prescaler of 1, so I have a 0.0625uS resolution. I want to generate pulses with pretty high accuracy and consistency. I have my…
M.Schindler
  • 221
  • 1
  • 5
2
votes
2 answers

Button for Delay Select

My target is to make an Arduino system that is able to change modes with different delays by pushing a button. For example, if I pressed the button, it will go into mode 1 with loop 1 that has a 30s delay. If I pressed it one more time, it will go…
Ibrahim.M
  • 23
  • 4
2
votes
2 answers

Please Correct my Delay Code without using delay();

This is my loop()function: if(Serial.read() == 't') { tarestate = true; } while(tarestate == true) { timeNowTare = mills(); if(mills() - timeNowTare >= 3000) { Serial.println("Tare"); } tareState = false; } Where tareStare is a…
Tim
  • 21
  • 3
2
votes
5 answers

Would this substitute for an Interrupt timer?

class Pseudo_Interrupt { //only cummulative time is important, exact interval is not // Class Member Variables // These are initialized at startup long OffTime = 1000; // milliseconds of off-time // These maintain the current state …
2
votes
1 answer

Generating a short pulse after a delay

I am trying to use one of the hardware timers on the arduino uno to generate a short pulse some number of microseconds after receiving a pulse on an input. Currently my code looks like this: uint16_t pulse_delay = 12000; //half-microseconds uint16_t…
AJMansfield
  • 225
  • 1
  • 11
2
votes
2 answers

Replacing delay() with millis()

I'm currently attempting to replace delay() in my project so that i may perform other tasks while waiting on a particular event to fire. The issue I'm having, is every example i've found using millis / elapsedmillis has everything inside the loop,…
ArcAiN6
  • 147
  • 1
  • 12
2
votes
1 answer

Calibrate Watchdog Timer

Is there any good way to calibrate the watchdog timer? I'm trying to use it as a time source, even when asleep, but it's woefully inaccurate. Some simple testing showed it loosing 2 minutes every hour!
Adam Haile
  • 599
  • 2
  • 7
  • 14
1
vote
2 answers

Turn off Timer1 / Timer1 One-shot

A zero detector crossover sends a pulse to the Arduino, it must start counting x time and then a pulse is sent from the Arduino, once the timer reaches time x, it must stop counting and only start counting again when it receives the new pulse of the…
1
vote
1 answer

Unable to compile SimpleTimer code - Exit status 1

I downloaded the SimpleTimer library from the Arduino's library manager and navigated to Arduino Playground website for some sample code for testing - SimpleTimer - Arduino Playground. After copying the following code into the Arduino IDE: #include…
user8396171
  • 11
  • 1
  • 2
1
vote
2 answers

Start / end events using milis

I want to start a relay, have it run for 3 seconds, stop for 10 seconds, repeat. I can't use delay, because I have a motor driver starting / stopping every 100 milis. So I put together this code, but it does not work. Any help appreciated. There's a…
R0b0tn1k
  • 187
  • 1
  • 2
  • 7
1
vote
0 answers

Due timer update

I want to test a BPM counter as a part of larger project rewriting code for a groovesizer synth. In essence i want to read a pot for updating BPM tempo, and then have a LED blink that tempo, displaying BPM value on an LCD screen. I am using the…
Erik
  • 261
  • 1
  • 12
1
vote
1 answer

Timer1 firing immediately

I'm trying to use Timer1 to get fired once a certain event occurs (about 1ms after the event). Using the timer to get a periodic interrupt works fine, but the very first interrupt fires immediately, rather than after the time I want it. #include…
qwerty_so
  • 113
  • 5
1
2