Problem with battery powered temperature sensor



  • I'm having an issue where a simple temperature sensor sketch works fine powered by the ftdi serial converter but when powered by 2xAA batteries something isn't working right.

    Log from when it's powered by 3.3v and working correctly:

    77 TSM:INIT
    77 TSF:WUR:MS=0
    86 TSM:INIT:TSP OK
    86 TSM:INIT:STATID=5
    90 TSF:SID:OK,ID=5
    92 TSM:FPAR
    126 TSF:MSG:SEND,5-5-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    905 TSF:MSG:READ,0-0-5,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    911 TSF:MSG:FPAR OK,ID=0,D=1
    2136 TSM:FPAR:OK
    2136 TSM:ID
    2138 TSM:ID:OK
    2140 TSM:UPL
    2146 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2152 TSF:MSG:READ,0-0-5,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2158 TSF:MSG:PONG RECV,HP=1
    2162 TSF:MSG:READ,0-0-5,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2168 !TSF:MSG:PONG RECV,INACTIVE
    2170 TSM:UPL:OK
    2172 TSM:READY:ID=5,PAR=0,DIS=1
    2179 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2187 TSF:MSG:READ,0-0-5,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2205 TSF:MSG:SEND,5-5-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
    2217 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    4227 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
    4243 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    4257 TSF:MSG:SEND,5-5-0-0,s=0,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK:
    4265 MCO:REG:REQ
    4268 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    4276 TSF:MSG:READ,0-0-5,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    4282 MCO:PIM:NODE REG=1
    4284 MCO:BGN:STP
    4290 TSF:MSG:SEND,5-5-0-0,s=1,c=0,t=30,pt=0,l=0,sg=0,ft=0,st=OK:
    4298 MCO:BGN:INIT OK,TSP=1
    4302 MCO:SLP:MS=750,SMS=0,I1=255,M1=255,I2=255,M2=255
    4308 TSF:TDI:TSL
    4311 MCO:SLP:WUP=-1
    4313 TSF:TRI:TSB
    4349 TSF:MSG:SEND,5-5-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:28.0
    4362 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:100
    4372 TSF:MSG:SEND,5-5-0-0,s=1,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.39
    4380 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    4386 TSF:TDI:TSL
    

    log from when it's not working:

    16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1
    26 MCO:BGN:BFR
    30 TSM:INIT
    30 TSF:WUR:MS=0
    38 TSM:INIT:TSP OK
    40 TSM:INIT:STATID=5
    43 TSF:SID:OK,ID=5
    45 TSM:FPAR
    79 TSF:MSG:SEND,5-5-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    1054 TSF:MSG:READ,0-0-5,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    1060 TSF:MSG:FPAR OK,ID=0,D=1
    2091 TSM:FPAR:OK
    2091 TSM:ID
    2093 TSM:ID:OK
    2095 TSM:UPL
    2099 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2107 TSF:MSG:READ,0-0-5,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2113 TSF:MSG:PONG RECV,HP=1
    2115 TSM:UPL:OK
    2117 TSM:READY:ID=5,PAR=0,DIS=1
    2121 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2129 TSF:MSG:READ,0-0-5,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2140 TSF:MSG:SEND,5-5-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
    2150 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    4161 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=11,pt=0,l=18,sg=0,ft=0,st=OK:Temperature Sensor
    4171 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1
    4179 MCO:REG:REQ
    4184 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    4190 TSF:MSG:READ,0-0-5,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    4196 MCO:PIM:NODE REG=1
    4200 MCO:BGN:STP
    4202 TSF:MSG:SEND,5-5-0-0,s=1,c=0,t=30,pt=0,l=0,sg=0,ft=0,st=OK:
    4210 MCO:BGN:INIT OK,TSP=1
    4214 MCO:SLP:MS=94,SMS=0,I1=255,M1=255,I2=255,M2=255
    4220 TSF:TDI:TSL
    4222 MCO:SLP:WUP=-1
    4225 TSF:TRI:TSB
    4231 MCO:SLP:MS=30000,SMS=0,I1=255,M1=255,I2=255,M2=255
    4237 TSF:TDI:TSL
    

    sketch:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
     * http://www.mysensors.org/build/temp
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 5
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define BATT_SENSOR
    
    #ifdef BATT_SENSOR
    #define REPORT_VOLTAGE
    #endif
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    
    #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected
    #define ONE_WIRE_GND 5
    #define ONE_WIRE_VCC 3
     
    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    const uint8_t TEMP_TIME = 12; //at least every nth time battery is reported
    const uint8_t BATT_TIME = 12; //when also BATT-LEVEL is reportet
    const float BATT_100 = 3; //3.3V for CR2032, 3V for 2xAA
    const float BATT_0 = 2.2;
    //float lastTemperature;
    uint8_t lastTempSent = 0;//, lastHumSent = 0;
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    bool metric = true;
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    #ifdef REPORT_VOLTAGE
    MyMessage msgBatt(1, V_VOLTAGE);
    #endif
    #ifdef BATT_SENSOR
    uint8_t battReport = BATT_TIME - 1; //First report at startup
    long oldvalue = 0;
    #endif
    
    void before()
    {
    
    #ifdef ONE_WIRE_VCC
      pinMode(ONE_WIRE_VCC, OUTPUT);
      digitalWrite(ONE_WIRE_VCC, HIGH);
    #endif
    
    #ifdef ONE_WIRE_GND
      pinMode(ONE_WIRE_GND, OUTPUT);
      digitalWrite(ONE_WIRE_GND, LOW);
    #endif
    
      #ifdef VCCGND_PINS
      pinMode(VCC, OUTPUT);
      digitalWrite(VCC, HIGH);
      pinMode(GND, OUTPUT);
      digitalWrite(GND, LOW);
      #endif
      
      // Startup up the OneWire library
      sensors.begin();
    }
    
    void setup()  
    { 
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      #ifdef REPORT_VOLTAGE
      present(1, S_MULTIMETER);
    #endif
    
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temperature Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         present(i, S_TEMP);
      }
    }
    
    void loop()     
    {     
       boolean tempsent = false;
       
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
      // query conversion time and sleep until conversion completed
      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
    
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
    
        // Only send data if temperature has changed and no error
        #if COMPARE_TEMP == 1
        if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
    
          // Send in the new temperature
          send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
          lastTempSent = 0;
          tempsent = true;
        }
      }
      #ifdef BATT_SENSOR
      if (++battReport >= BATT_TIME && tempsent) {
    
        //gw.sleep(10);
        long value = readVcc();
    
    
        if (value != oldvalue) {
          int percent = (( (float)(value) / 1000 ) - BATT_0) / (BATT_100 - BATT_0) * 100;
          percent = (percent > 100) ? 100 : percent;
          percent = (percent < 0) ? 0 : percent;
          sendBatteryLevel(percent);
    #ifdef REPORT_VOLTAGE
          send(msgBatt.set((float)(value) / 1000, 2));
    #endif
        }
        oldvalue = value;
        battReport = 0;
      }
    #endif
      sleep(SLEEP_TIME);
    }
    
    long readVcc() {
      // Read 1.1V reference against AVcc
      // set the reference to Vcc and the measurement to the internal 1.1V reference
    #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
      ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
      ADMUX = _BV(MUX5) | _BV(MUX0);
    #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
      ADMUX = _BV(MUX3) | _BV(MUX2);
    #else
      ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
    #endif
    
      delay(2); // Wait for Vref to settle
      ADCSRA |= _BV(ADSC); // Start conversion
      while (bit_is_set(ADCSRA, ADSC)); // measuring
    
      uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH
      uint8_t high = ADCH; // unlocks both
    
      long result = (high << 8) | low;
    
      result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
      return result; // Vcc in millivolts
    }
    

    i tried a 47uf cap that I had laying around on the AA setup but nothing changes...


  • Mod

    @waspie both log files look good. Could you describe the problem you are experiencing?



  • @mfalkvidd said in Problem with battery powered temperature sensor:

    Could you describe the problem you are experiencing?

    The temperature/voltage isn't being sent when battery powered.
    from the good log:

    4349 TSF:MSG:SEND,5-5-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:28.0  <---temperature
    4362 TSF:MSG:SEND,5-5-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:100 <---battery percentage
    4372 TSF:MSG:SEND,5-5-0-0,s=1,c=1,t=38,pt=7,l=5,sg=0,ft=0,st=OK:3.39 <---- vcc voltage
    

    but those items aren't sent when it is powered by 2xAA. Also, it doesn't seem to go to sleep or wakeup (one or the other).

    If I keep watching the serial log while battery powered it never does anything more. Not after the allotted 30 seconds or longer. As if it is stuck.


  • Mod

    Thanks for clarifying @waspie
    Could you post your sketch?
    EDIT: Sorry, you already did that. I'll take a look at it.


  • Mod

    I can't see anything that looks odd in the sketch. Easiest way to troubleshoot is probably to add Serial.println statements to see where the code execution stops.



  • i stripped out enough stuff to see it post 85 degrees the first time around. after that it is reporting the temperature consistently on battery (including going to sleep, waking up, reporting, and going back to sleep).

    Any thoughts on why it fails the first time through?

    My hack solution would be to remove the check and let it throw out 85 the first time through and discard that on the other send (openhab) but surely there's a better way?



  • This post is deleted!


  • Hi, @waspie !

    First of all i'd like to say about value "85" - according to DS18B20 datasheet page 6, note after table 4.1: *The power-on reset value of the temperature register is +85°C.

    Your code looks fine and similar to my mains-powered DS18B20 sensor (because we used same example).

    I have some thoughts:

    1. you may have power problems:
      a. from logs i see that you have 1 sensor, but you left defined #define MAX_ATTACHED_DS18B20 16 . If (suddenly) you would have 16 sensors - when you ask them for conversion they would consume 16* 1.5mA(max at 5v) = 24mA which is above recomended 20mA for atmega pin
      b. you are powering from battery: DS18B20 oficially requres minimum of 3 v (as for datasheet). People say that with lower voltage it may have strange readings or even do not work at all
      c. you are powering from battery through pin: is voltage raise fast enougth before start reading? is voltage drop low enought so voltage supplied to ds18b20 is >=3v?
      i'd suggest you to measure voltages (but multimeter won't show you short drop-outs when start conmsuming) and try to feed VCC of DS18b20 directly from power source. If not possible - at least measure voltage and try to add some wait() after powering up pin (or also before measure start or after sleep).

    2. When running from 3v, what frequency of atmega processor is? If it is 16mHz - that it is not in safe operating area for that voltage. you should run 8 mHz. (>=2.4v for 8mHz and >=3.78v for 16mHz)

    Also i suggest you try to change sleep(conversionTime); with wait(conversionTime); and check if it will change anything (especially when ds18b20 directly powered from source). Also try with fresh batteries.

    If nothing helped, then for "85" value simple solution will be

    if (temperature != 85)
        send();
    

    because it is very rarely that people need to measure 85 degrees of Celsius at home...

    ~~ ~~
    For advanced battery-powering i highly recommend to read this. Also you can find there about frequency vs voltage.

    And for internal battery voltage reading:
    i don't remember where i found it: here1 or here2 or somwhere else..
    but you may want to get more accurate readings by measuring and setting Internal Reference Voltage value and using it in the code, and give it some time to stabilize and/or use some type of averaging.


 

301
Online

8.7k
Users

9.5k
Topics

99.5k
Posts