Read every hour - Interrupts


I need some help. I have an Arduino Mega and I need this micro to be constantly reading from an ultrasound sensor. The problem is that I want to read from a temperature sensor once per hour. I have done some numbers with the TIMER1, but I am only alowed to throw an interrupt if it is less than 4,19 secons with a 1024 prescaler, because the timer1 has only 16 bits. Therefore, I think using the interrupts are not the best way to do this. Any solution?

Thank you.

Easy way, use an off-board RTC and set the alarm for once per hour... reset for +1 hour each alarm.


You have to measure the time, somehow. There is millis() function so it should not be a problem measure time difference 1 hour. This function depends on system clock and the resonator which is not precise. If you need higher precision then some RTC circuit would be a solution.

Maybe this could help:

Set your interrupts to say 3 seconds and count them, every 1200 read the sensor.

Or count milliseconds with millis().

I doubt there is a need for interrupts. Read your ultrasound sensor in loop() and use a millis() based timing to read the temperature sensor.

unsigned long currentTime;
void loop()
  // start time of wait period
  static unsigned long startTime = 0;

  currentTime = millis();

  if (startTime == 0)
    startTime = currentTime;

  if (currentTime - startTime >= 3600000UL)
    // reste start time; next iteration of loop a temperature reading will be done because startTime equals 0
    startTime = 0;


There are slightly different approaches, but it's the basics.

You got the same answers in your post in the Sensors section. Please don’t post the same question in more than one section (aka cross posting).