DallasTemperature library and ArduinoLowPower library not compatible?

Hello everybody,

I have the following problem: Basically, I use the TimedWakeUp example from the ArduinoLowPower library. When I don’t start up the library with sensors.begin() in the void setup(), the sketch works as intended. My MKR WAN 1300 goes to sleep for 5 seconds after the board led blinks in a loop. When I uncomment sensors.begin() and start up the DallasTemperature library, the whole sketch get’s blocked it seems. There is just no blinking of the board led followed by a sleep mode. It seems like starting up the DallasTemperature library blocks the rest of the sketch and I dont know why. Did anybody else encounter something like this? Maybe somebody can confirm this behaviour, when they own the same board? I dont know, if there is a compatibility issue between both libraries and I can’t find anything on the internet about it after researching the issue for a few days.

Should the library start up be defined somewhere else in the code?

Thanks for any advise or hints!

/*
  TimedWakeup
  This sketch demonstrates the usage of Internal Interrupts to wakeup a chip in sleep mode.
  Sleep modes allow a significant drop in the power usage of a board while it does nothing waiting for an event to happen. Battery powered application can take advantage of these modes to enhance battery life significantly.
  In this sketch, the internal RTC will wake up the processor every 2 seconds.
  Please note that, if the processor is sleeping, a new sketch can't be uploaded. To overcome this, manually reset the board (usually with a single or double tap to the RESET button)
  This example code is in the public domain.
*/


#define ONE_WIRE_BUS 6

#include <ArduinoLowPower.h>
#include <OneWire.h> 
#include <DallasTemperature.h>
 
OneWire oneWire(ONE_WIRE_BUS);  
DallasTemperature sensors(&oneWire);

int alarm_source = 0;

void setup() {
  delay(10000);
  pinMode(LED_BUILTIN, OUTPUT);
  // start serial port 
  Serial.begin(9600); 
  // Start up the library 
  //sensors.begin(); // uncomment this and the Sketch gets blocked and never goes into sleep mode
  
  // Uncomment this function if you wish to attach function dummy when RTC wakes up the chip
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE); // Zum Aufwecken
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  LowPower.sleep(5000);
}

void alarmEvent0() {
  alarm_source = 0; 
}

Does the DallasTemperature library work by itself without any code attempting to sleep? Have you tried the examples that come with the library? Do they work?

gfvalvo:
Does the DallasTemperature library work by itself without any code attempting to sleep? Have you tried the examples that come with the library? Do they work?

Hey gfvalvo,

thx for trying to help me.

The library actually does work, when I use it for temperature measurements only and not attempt to make the Board sleep. I’ve used the DallasTemperature library to measure temperatures with the DS18B20 connected to the MKR WAN 1300.

Here the code I’ve used for temperature readings:

/********************************************************************/
// libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 6 on the Arduino 
#define ONE_WIRE_BUS 6
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
/********************************************************************/ 
void setup(void) 
{ 
 // start serial port 
 Serial.begin(9600); 
 Serial.println("Dallas Temperature IC Control Library Demo"); 
 // Start up the library 
 sensors.begin(); 
} 

void loop(void) 
{ 
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
}

Put a Serial debug print before and after the calls to 'sensors.begin()' and 'LowPower.attachInterruptWakeup()'. Put one on entry to 'loop()' also. Hopefully, you'll see which function is hanging. Then you'll need to dig into the library code to see what is interfering with what.

gfvalvo:
Put a Serial debug print before and after the calls to ‘sensors.begin()’ and ‘LowPower.attachInterruptWakeup()’. Put one on entry to ‘loop()’ also. Hopefully, you’ll see which function is hanging. Then you’ll need to dig into the library code to see what is interfering with what.

My sketch with serial prints for debugging:

/* how to debug https://forum.arduino.cc/index.php?topic=215334.0 */

/* #define DEBUG 1 */ /* Comment #define DEBUG 1 out to make all prints disappear*/
#define DEBUG 1
#define ONE_WIRE_BUS 6

#include <ArduinoLowPower.h>
#include <OneWire.h>
#include <DallasTemperature.h>
 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);

int alarm_source = 0;

void setup() {
  delay(10000);
  pinMode(LED_BUILTIN, OUTPUT);
  // start serial port
  #ifdef DEBUG
  Serial.begin(9600);
  #endif  

  #ifdef DEBUG
  Serial.println(F("Load library"));
  // Start up the library
  sensors.begin(); // uncomment this and the Sketch gets blocked and never goes into sleep mode
  Serial.println(F("After library loading"));
  #endif
 
  #ifdef DEBUG
  Serial.println(F("Top of interrupt"));
  // Uncomment this function if you wish to attach function dummy when RTC wakes up the chip
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE); // Zum Aufwecken
  Serial.println(F("Below interrupt"));
  #endif
}


void loop() {

  #ifdef DEBUG
  Serial.println(F("Top of loop"));
  #endif DEBUG

  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
    
  #ifdef DEBUG
  Serial.println(F("Go sleep!"));
  #endif DEBUG
  LowPower.sleep(5000);
}

void alarmEvent0() {
  alarm_source = 0;
}

If sensors.begin appears as a comment in the sketch, I get the following DEBUG output

Load library
15:04:42.651 -> After library loading
15:04:42.651 -> Top of interrupt
Below interrupt
15:04:42.698 -> Top of loop
Go sleep!

I have a question regarding that. In a loop, shouldnt the Top of loop and Go sleep! message appear over and over again? The board blinks as expected every 5 seconds, so it should be in a loop state.

After commenting sensors.begin(), I get the same messages though:

Load library
After library loading
15:08:18.973 -> Top of interrupt
Below interrupt
15:08:19.020 -> Top of loop
Go sleep!

But the Board LED never blinks, so the same problem still persists (Blocking of the loop), while the Debugging information gives me no hint for the cause of that behavior. That’s just irritating.

The ArduinoLowPower library seems to depend on the RTCZero library. I just installed the IDE on another Computer. Next to the mentioned libraries in the code, I also need this one for the ArduinoLowPower library it seems, because:

In file included from C:\Users\anon\Documents\Arduino\sketch_dec20a\sketch_dec20a.ino:7:0:

C:\Users\anon\Documents\Arduino\libraries\Arduino_Low_Power-1.2.0\src/ArduinoLowPower.h:11:21: fatal error: RTCZero.h: No such file or directory

 #include "RTCZero.h"

                     ^

compilation terminated.

exit status 1
Fehler beim Kompilieren für das Board Arduino MKR WAN 1300.

It seems that there is an issue between the latest DallasTemperature library and latest RTCZero library I guess. Must be clock related then. At least I got this issue for my MKR WAN 1300....

gaebel90:
It seems that there is an issue between the latest DallasTemperature library and latest RTCZero library I guess.

Guess you're going to have to dig through the libraries' source code to find out. Look for common processor resources they may use in conflicting ways.

Before you do that though, where do you set up the RTC telling it how often to wake up?

gfvalvo:
Guess you’re going to have to dig through the libraries’ source code to find out. Look for common processor resources they may use in conflicting ways.

Before you do that though, where do you set up the RTC telling it how often to wake up?

Thank you for the advise. In the actual example in the ArduinoLowPower library for a timed wakeup, which is also called TimedWakeup, it seems like you dont need to call a RTC object. That means you can use LowPower.sleep(SleepDuration) straight to tell your Microcontroller how long the sleeping intervalls have to be.

I’ve written another solution for Deep Sleep, which directly uses a RTC object, where I set alarms and wake up intervalls through the help of the RTCZero library and the specifics of SAMD21 boards. The deep sleep works too, but if I call sensors.begin() in that script, it also blocks the loop and my board LED never blinks…