Sticky wake up problem

Hi everyone,

Firstly, thanks for a great forum! I’ve used Arduinos over the years for lots of little projects and this forum has regularly been a go-to for me! I really appreciate all the guidance and examples given by everyone on here.

Whilst being stuck at home, I’m trying to build a device to monitor 3 water butts that I have in the garden using and Arduino (currently Uno, probably later a Mini). I would like to know the water level in the butt and the temperature and humidity.

I thought an easy way to do this would be with a Dragino Lora shield, a matched gateway, a DHT11 module, a one-wire temp device, an RTC3231 and maybe a 4…20mA to 0…5V board for a pressure transducer. As I wanted the thing to last “forever” in the garden, I figured an appropriately IP rated enclosure, a small solar panel, a deep cycle battery and charge regulator might be useful too! So I ordered a host of bits and started tinkering!

My first few rounds of tests went well and I got to the point that I could receive data every minute through the Lora system. The data showed me external temperature, internal temperature and humidity, and depth as expected.

At that point I was just sleeping for 60 seconds which of course meant I would have been gobbling through batteries (I was still running on a PSU at the time). I next implemented the RTC using an interrupt to wake up every minute and do the same thing. Everything seemed to work! I was delighted! I popped a 6V sealed lead acid battery into the enclosure and stuck it outside to do some more tests! Again, everything worked! The thing woke up every minute and sent a load of data before sleeping again.

Then I noticed some quirky things had started happening. Sometimes, it sent data ever minute for a few hours, then stopped for a few mins, then started again. Sometimes it sent 3 messages then didn’t, then came back for a few hours, then went again. At first I thought it was a power issue - I appreciate I’m using a 6V battery and so perhaps I was suffering voltage drop that was causing instability, but even when running on the power supply (or just the power from the USB), the same problem occurred. I was using a 220R resister to measure my 4…20mA device at that point, so I disconnected that too - but still the same strange intermittent operation. Eventually, I tried substituting the line of code that put it to sleep, and instead just returned to sleep(60000) and it happily ran for hours, no problems.

So I can only assume I’m doing something wrong within my Going_To_Sleep code - but I can’t see what it is.

I’ve attached my code and hope it’s sufficiently documented to make some sense!

Any help would be very much appreciated!

Thank you all!

LoRa_Simple_Client_Arduino-V10a.ino (14.1 KB)

Could it be wiring? Maybe the wakeup interrupt is firing inappropriately or not at all.

I don't see anything in the code, but sleep functions are an unfamiliar area for me. I assume you've seen Nick Gammon's work on this stuff.

My initial thought was that you've miscounted the number of characters you need in one of your little buffers, but the behavior you see pretty much rules out that theory.

Thanks for the reply WildBill,

It could be, but I've tried to get it right and it does work for extended periods (sometimes hundreds of minutes, i.e. >100 sleep wake cycles.) I followed this as an example:

I don't have the Pro Mini, and I'm connecting to Pin 3 (interrupt 2 as I understand it) instead of Pin 2 as Pin 2 is in use by the Dragino Shield, but I think that's all fine.

Yes, I have had a look at Nick Gammon's pages. It all looks good and I may go the bare bones route and maximum power saving in the end, but to get some trials going I would be happy with this working! I'm sure I'm just missing something obvious... but having spend a fair few days digging, I haven't worked out what that is yet!

Thanks for your suggestions!

A lot of complexity there, if you just monitoring stuff in the garden, the Arduino can manage well enough without the RTC, just use the watchdog time to sleep at 8 seconds at a time.

I have had a sensor running in my garden, it sends temperature, humidity and pressure every 15 minutes. Uses a bare bones style Arduino (in the form of a Pro Mini).

Its running off a 155mAhr battery, which has dropped from 4218mV to 3978mv in 4 months. Never been touched in 4 months either.

Thanks for the reply Srnet!

That’s a nice project you’ve got there!

I wanted to use the the RTC so that I could maximise the life expectancy of the battery. I am not yet sure how frequently I really want to sample and there’s (obviously) a trade off between battery life and sampling rate. I am planning to add a solar panel and charge system to the arrangement later so that I can effectively fit and forget and although I appreciate I can keep sleeping for multiple iterations of 8 seconds, this seemed rather inelegant when the RTC board existed and was acclaimed… but given the problem I’m having at the moment, I’m seriously thinking doing just what you’re suggesting!

Really interesting project though, thanks for drawing my attention to it! I’ll be really interested to see how long it lasts on that battery! It’s doing great!

Thanks again

Really interesting project though, thanks for drawing my attention to it! I'll be really interested to see how long it lasts on that battery! It's doing great!

Thank you, I started it especially to show how long a standard ATmega328 could last in a low power project, with no other components such as a RTC, TPL5110 etc.

The 155mAhr battery looks like it might last a year, which would suggest that a set of AA Lithiums (2800mAhr) would last around 18 years ...................... no need to change a RTC battery on a regular basis either ...........


Thanks for the comments and help.

I have found the problem and corrected it! I didn't spot before, but in the example I was following, the way the alarm is set after an initial wake-up is to simply increment the minute value. This works fine, except when the minute value passes 59! There will never be a match for 10:61:0 for example!

I added the following to my code and it works wonderfully now!

int newm = minute(t)+time_interval;
if (newm>=60) {
newm = newm-60;

and of course changed the RTC setAlarm statement to:

RTC.setAlarm(ALM1_MATCH_MINUTES , 0, newm, 0, 0);

I should probably get back to the author of the code I was working from (here: Project: Example using a RTC to wake-up an Arduino Data Logger — The Arduino Maker Man) and let them know about the problem.

I'm still really interested in your device though Srnet, so I will be in touch if that's ok?!

Many thanks again all.

Glad you got it squared away. I rather suspected at the outset that you would have to solve it yourself :slight_smile:

I'm still really interested in your device though Srnet, so I will be in touch if that's ok?!

Go ahead, contact me via the Website.

The sensor board shown in that example uses a Pro Mini of my own contstruction. This has a MCP1700 regulator so the sleep current, watchdog timer active, is about 6.5uA. Easy enough to build if your OK with SMT stuff. If you use a Pro Mini off eBay then sleep current is in the 50uA region, not so good.

There is however a version of the sensor board that uses a DIP Atmega328 in a bare bones circuit, that does have the same 6.5uA sleep current. No surface mount parts need be used. Same size as the Pro Mini board.