# Accurate and efficient time keeping

I have a breadboard Arduino that I work it with 8MHz external crystal. I need it to go to sleep and wake up every 30 minutes or so, measure the temperature with the on board temperature sensor and go back to sleep. I’m doing this project for school’s garden where we want to monitor the soil temperature.

The question here is how to make it go to sleep and wake up every 30 minutes. I’ve checked the WDT which I don’t think is a good fit for my purpose because they say that it is inaccurate. And since the max it could go is 8 seconds, I need to loop it to achieve 30 mins which also seems like a power waste somehow if I’m not mistaken. And by the way I need this system to be low-power.

I’ve checked the Timer/Counter 0,1 and 2. Timer 1 which is 16-bit can also only count up to approximately 8.3 seconds with the highest prescalar. Please do correct me if my math is wrong below.

((X seconds) x 8MHz) /1024 = 65.535

x= 8.3 secs

Also I’ve found a comment in this post (https://forum.arduino.cc/index.php?topic=425483.msg2931650#quickreply),

1. It most likely uses the watchdog timer, which sucks for accuracy. If you want good timekeeping, you need to clock off a crystal. The Timer2 peripheral can have a separate crystal attached to drive it. It’s designed to use a 32.768 kHz crystal, which is the standard value used by real-time clocks and will keep very good time (0.001% accurate instead of 10%). Timer2 is left on if you use Power-save mode instead of Power-down.]

From this post I understand that Timer1 could be used with a 32kHz external clock. But can that count up to 30 minutes accurately and efficiently?

Or should I just use a RTC chip like DS1302? Would that be low-power?

So could you please guide this not very experienced avr programmer to find a suitable low-power way to make Atmega328p go to sleep and wake up every 30 mins or so.

I have no problems putting it to sleep and turning off the unnecessary peripherals thanks to Nick Gammon’s blog post. All I need is to find a suitable way to count and wake the MCU up in determined long periods like 30 mins or more and do this with as little power as possible.

How accurate does it need to be? Is +/- a few seconds going to ruin the data?

I have a solar powered garden monitor running on a Wemos D1 Mini. It wakes every hour from deep-sleep and checks the soil moisture. It sends an MQTT message to my Home Assistant computer (An Intel NUC, but a Raspberry Pi would work as well), then goes back to sleep.

I've never tried sleep on an Arduino.

Way too complex proposed solutions and just not necessary.

First, forget the obsession with timer accuracy, does it really make any practical difference whatsoever if instead of getting the temperature report every 30 minutes its every 28 or 32 minutes ?

Take a look at this practical example;

https://www.loratracker.uk/just-how-long-can-a-lora-sensor-battery-last/

Looks like a years operation based on a 15 minute reporting interval with a 155mahr battery. So a set of AA batteries ought to last 18 years or so. No RTC. What have I got wrong here ??????

If you use an RTC, get a DS3231, it is more accurate than the DS1302 and not much more expensive. The RTC can run off its own battery except when the arduino needs to access its internal registers, at which time it can be powered from an output port. The RTC battery is generally good for a minimum of 4 to 5 years.

You didn’t mention, how are you going to be storing the temperature readings? Many DS3231 clock boards include a 24c32 EEPROM which gives you 4K bytes of non-volatile storage.

I guess it would be okay if each of the intervals were +- 2 mins off. But my concern is, it needs to somehow wake up for a very brief time period to put itself back to sleep at the end of that 8 seconds. Wouldn't that be a power waste? But I'll definitely write a piece of code for WDT wake-up and see it for myself if it might work for my purpose. But I'm still looking for other solutions tho, because I've been reading that writing reliable software around watchdog timers is not so easy.

What do you all think about this idea tho? I understand its accuracy feature, but power-wise, would that make any difference?

It most likely uses the watchdog timer, which sucks for accuracy. If you want good timekeeping, you need to clock off a crystal. The Timer2 peripheral can have a separate crystal attached to drive it. It's designed to use a 32.768 kHz crystal, which is the standard value used by real-time clocks and will keep very good time (0.001% accurate instead of 10%). Timer2 is left on if you use Power-save mode instead of Power-down.

Yes, I'm going to add an EEPROM chip to store data. And yes, I'm aware of those modules with EEPROM on board. But I'm still not sure about RTCs' power consumption. It would be awesome to run the same battery pack close to a year time. So it's great that almost all of the RTC modules have a separate battery but I've been reading that the battery on the RTC module sometimes doesn't last more than couple of weeks.

Any more advice or suggestions on these matters?

cglkr97:
I guess it would be okay if each of the intervals were +- 2 mins off. But my concern is, it needs to somehow wake up for a very brief time period to put itself back to sleep at the end of that 8 seconds. Wouldn't that be a power waste?

The 8 second wakeup does 'waste' an extremely low amount of battery power yes, and such an extremely low amount of battery power as to be insignificant, as noted in the link I gave earlier.

If the RTC has a seperate coin battery, as apposed to a small PCB mounted one, then they will last a very long time, the PCB ones wont last long at all.

Be sure to study this excellent tutorial on sleep and power saving techniques with AVR processors.

There is no need for the measurement to be made at accurate intervals. You can instead timestamp the data using an accurate clock, and interpolate if needed.

The watchdog timer is fine for creating approximate 30-minute intervals. Wake up, count “8 second” periods, and when 225 have elapsed, make the measurement. Otherwise go back to sleep.

cglkr97:
So it's great that almost all of the RTC modules have a separate battery but I've been reading that the battery on the RTC module sometimes doesn't last more than couple of weeks.

Some of the RTC modules have a supercap (high capacity capacitor) or rechargeable battery instead of a lithium coin-cell battery, those do not last very long, and are better suited for use on a device that only occasionally loses power.

Unfortunately a lot of the cheap RTC boards leave the charge circuitry for the above on the board even though they come with a coin-cell battery installed, which can shorten the battery life by trying to charge it. Simple to remove, and there are other modifications needed for extremely low power operation, but probably not necessary in your situation.

Without an RTC, you can still get a fairly accurate time for the data if you note the time the arduino starts logging data, then the time you shut it down, and space the measurement times evenly over that time period.