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.

Filter by
Sorted by
Tagged with
14 votes
4 answers
53k views

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 ...
Petr's user avatar
  • 253
9 votes
2 answers
4k views

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 ...
UserK's user avatar
  • 559
5 votes
1 answer
13k views

Arduino Countdown without using delay()

I am working on a project which requires countdown from 30sec to 0sec. I can't use delay() function because I am taking input from keyboard during countdown. millis() function wont help because my ...
Tanmay Yerunkar's user avatar
5 votes
4 answers
10k views

Timer2 does not work as it should

I am currently playing with Arduino timers (on UNO currently) and I am building a library that has a function that must be called every millisecond. There is plenty of code samples on Internet so I ...
jfpoilpret's user avatar
  • 9,132
3 votes
3 answers
6k views

How to make 1 second delay more accurate?

I have written two (1 second) delay programs. The first program as follows: void setup() { Serial.begin(9600); TCCR1A = 0; TCCR1B |= (1<<CS12); TCCR1B &= ~(1<<CS11); TCCR1B &=...
Peouse Dutta's user avatar
3 votes
1 answer
2k views

Timer2 "Clear OC2B on Compare Match" not working as expected in CTC mode

This is a follow up to my previous question. I have Timer2 in CTC mode, with TOP=OCR2A set to 150 (with a prescaler of 8, giving a 75us timing interval). The datasheet states that setting COM2B1 to ...
Tom van der Zanden's user avatar
10 votes
1 answer
4k views

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 ...
UserK's user avatar
  • 559
9 votes
5 answers
6k views

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'...
bwoogie's user avatar
  • 193
5 votes
1 answer
650 views

Help managing the fallout from timer0 mode and prescaler change on ATMega2560 without modifying wiring.c

For a driving application I need the PWM mode to be phase correct and the prescaler to be 1. The issue is that the board I am working with has the load hooked up to a PWM pin reliant on timer0. The ...
Nerbsie's user avatar
  • 53
4 votes
2 answers
1k views

Can a timer be used in both CTC and (fast) PWM mode simultaneously?

I would like to generate a sequence of pulses of varying lengths in the range of 0-100us, so that from the start of one pulse to the start of the next there is a delay of 100us. I have set up timer0 ...
Tom van der Zanden's user avatar
4 votes
3 answers
14k views

Stable frequency generation

I'm trying to work out the best way to generate a stable frequency with my Uno R3 I'm using interrupts to generate an approximately 40KHz frequency which drives some ICs/MOSFETs to effectively ...
Ramrod's user avatar
  • 187
3 votes
1 answer
29k views

Getting BPM from the given code

I have found code that can blink an LED on Arduino according to Heart Beat. I am using AD8232 as a sensor for Heart Rate. Code for it is: // Variables int PulseSensorPurplePin = 0; // Pulse Sensor ...
Mudassir Hussain's user avatar
3 votes
1 answer
250 views

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, ...
M.Schindler's user avatar
3 votes
1 answer
1k views

Setting the frequency of a timer interrupt

The concept: Play a sine wave saved as a character array in PROGMEM through an 8-bit DAC connected to the PORTC pins on the Arduino Mega. This works: // File just defines an array and a size #include ...
Dave's user avatar
  • 133
2 votes
1 answer
1k views

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: ...
AJMansfield's user avatar
2 votes
1 answer
1k views

Why does timer1 not generate correct period on CTC mode with a resolution of 5us?

I have defined my code to generate an interrupt over each 5μs on CTC mode using timer1 but the output shows a time of ~15μs. Following the equation to set the counting number to achieve the ...
bpinhosilva's user avatar
2 votes
1 answer
608 views

Set PWM by period? And have it variable?

In this other stack QA I'm asking about generating a pulse signal with a period ranging from 63 ms to 6.5 ms. There is a popular PWM library however it doesn't compile and also since 63 ...
Krista K's user avatar
  • 526
2 votes
1 answer
565 views

Why does enabling TIMSK1 mess up serial?

I am intending to write a code where a user will send lines through serial to affect timer interrupts. I am noticing a problem with serial where as soon as I enable the timer interrupts, the serial ...
Sebastian Freeman's user avatar
2 votes
0 answers
1k views

Arduino Every 25k PWM signal

I have written a class for generating a 25khz pwm signal for the Arduino Nano. This works great (pin 9 and 10 only) on the Nano. I want to switch to Arduino Every; this code does no longer work (the ...
HixField's user avatar
  • 135
1 vote
2 answers
429 views

maximum interrupt-based PWM frequency at 500 Hz

I need to control a 24 kHz ultrasonic transducer and decided to use an arduino uno I have lying around. Using tone() or the atmega's hardware pwm is not very suitable because I need two 50/50 square ...
Sim Son's user avatar
  • 1,859
1 vote
1 answer
366 views

Issue using Arduino Mega Timer2 with PinChange Interrupt

I was working on a program to measure the pulse width , for that I thought of using Timer 2 in Arduino mega along with pin change interrupt, The program I have written is as follows volatile float ...
Lawliet's user avatar
  • 183
1 vote
0 answers
127 views

Arduino mega PinChangeInterrupt with 16 bit timer

With reference to this question ::I have written a program by using 16-bit timer, The program is as follows . volatile uint32_t ovfCount = 0; struct Pulse { uint32_t last_toggle; uint32_t width; ...
Lawliet's user avatar
  • 183
1 vote
1 answer
210 views

Input Capture with Sleep Enabled on ATM32u4 board only works every second reading in SLEEP mode

In short: I have an Input Capture code and a Power-Saving code combined into one program. Both are adapted from Nick Gammon's examples that work fine independently. When I combined them into one ...
TommyS's user avatar
  • 71
1 vote
1 answer
1k views

Timer2 Compare Interrupt not working as expected

To get this out of the way first: I have already looked at "Timer2 does not work as it should" and "Timer2 “Clear OC2B on Compare Match” not working as expected in CTC mode" but have not found the ...
Lithimlin's user avatar
  • 145
1 vote
2 answers
10k views

Arduino Uno vs Arduino Micro timers → pins

Where do I find the pins relative to timers of each individual Arduino microcontroller? As I like Arduino's I already have various samples of them. The one I mostly use is the Uno and Micro for now......
cocco's user avatar
  • 431
0 votes
1 answer
2k views

Seeeduino XIAO write and read PWM duration (period) using timers

I am trying to communicate between two Seeeduino XIAO (chip: ATSAMD21G18A-MU) by way of TCC capture using register timers. Square wave pulse duration varies from 200ns to 4us. I found these two code ...
MeSo2's user avatar
  • 177
0 votes
2 answers
4k views

32u4 timer 4 pwm frequency help

I have extracted the following code from the multiwii project. It generates a ~490-500hz pwm signal on pin 6 which is used to drive an esc. I want to change this frequency down to 50Hz to drive a ...
Hayden Thring's user avatar
0 votes
3 answers
1k views

Time.h rollover after 49.7 days [duplicate]

I'm developing a arduino based system, which includes a alarm system. Searching on the Internet, I found these lybraries "Time.h" and "TimeAlarms.h". In the requirements, it says: "Time does not ...
BrunoAraujo's user avatar
0 votes
2 answers
2k views

DHT11 and Pulse sensor incompatibility?

I am working on an IoT project to send data acquired from DHT11 and PulseSensor to a website. I can send the data properly when I use each of sensors alone but when I write a wrapper to use both ...
Puya's user avatar
  • 5
0 votes
3 answers
1k views

How can I set a timer?

I would like to set a stopwatch timer that will determine how long an input is in a certain state before changing. I want to set it so that, depending on the output my code executes one of 2 switch ...
Damon Swart's user avatar
-1 votes
2 answers
408 views

Detecting when a timer interrupt does not run

I am dealing with code thatt i received that relies heavily on timer interrupts. i am using arduino Mega (Funduino) More specifically, when a signal arrives in a pin, an interrupt is run that starts ...
user1584421's user avatar
  • 1,395