HELLLLLPPPPPP - Need help with calculation of temp sensor and want to add extra sensors



  • Hi Everyone,

    I managed to get this to compile and send data to Vera, however the results that worked in the old version doesn't calculate the correct temp. I am missing something but can figure it out. ALL HELP WOULD BE APPRECIATED.

    Thanks

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define THERMISTORPIN A0 // Same
    #define THERMISTORNOMINAL 10000 // Same
    #define TEMPERATURENOMINAL 25
    #define NUMSAMPLES 5
    #define BCOEFFICIENT 3950
    #define SERIESRESISTOR 10000
    #define CHILD_ID 98   // Id of the sensor child
    #include <MyConfig.h>
    #include <MySensors.h>
    
    #include <SPI.h>
    
    const unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MyMessage msg(CHILD_ID, V_TEMP);
    
    int samples[NUMSAMPLES];
    
    void setup(void) {
    
      pinMode(THERMISTORPIN, INPUT);
      analogReference(EXTERNAL);
    
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("PT Temperature Sensor", "1.1");
    
      // Present all sensors to controller
    present(0, S_TEMP);
    }
    
    void loop(void) {
     
      float average;
    
      // take N samples in a row, with a slight delay
      for (uint8_t i = 0; i < NUMSAMPLES; i++) {
        samples[i] = analogRead(THERMISTORPIN);
        delay(12);
      }
      // average all the samples out
      average = 0;
      for (uint8_t i = 0; i < NUMSAMPLES; i++) {
        average += samples[i];
      }
     average /= NUMSAMPLES;
    
      // convert the value to resistance
      average = 1023 / average - 1;
      average = SERIESRESISTOR / average;
    
      float temperature;
      temperature = average / THERMISTORNOMINAL; // (R/Ro)
      temperature = log(temperature); // ln(R/Ro)
      temperature /= BCOEFFICIENT; // 1/B * ln(R/Ro)
      temperature += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
      temperature = 1.0 / temperature; // Invert
      temperature -= 258.95; // convert to C
    
      Serial.print("Temperature ");
      Serial.print(temperature);
      Serial.println(" *C");
    Serial.print("Average analog reading ");
    Serial.println(average);
    Serial.print("analog reading ");
    Serial.println(THERMISTORPIN);
    Serial.print("analog reading samples ");
    Serial.println(samples[1]);
    Serial.print("analog reading samples ");
    Serial.println(samples[2]);
    Serial.print("analog reading samples ");
    Serial.println(samples[3]);
    Serial.print("analog reading samples ");
    Serial.println(samples[4]);
    Serial.print("analog reading samples ");
    Serial.println(samples[5]);
      // Send in the new temperature
      send(msg.setSensor(0).set(temperature,1));
      // delay(1000);
      sleep(SLEEP_TIME);
    }
    

  • Mod

    I have the function that returns correct value from thermistor, but I'm not at home now. I found it on Google looking for a logarithmic conversion formula for thermistor if you want to search it meanwhile



  • thanks, looking forward to it


  • Mod



  • Hi

    Thank you. I had the sketch calculating correctly by I couldn't get the sketch to work with the mysensors side. Both sketches worked but when I combined them the radio worked but the temp sensors didn't. This is what I have working, this calculates the temp correctly and transmits the correct reading.

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define THERMISTORPIN A0 // Same
    #define THERMISTORNOMINAL 10000 // Same
    #define TEMPERATURENOMINAL 25
    #define NUMSAMPLES 5
    #define BCOEFFICIENT 3950
    #define SERIESRESISTOR 10000
    #define CHILD_ID 98   // Id of the sensor child
    #include <MyConfig.h>
    #include <MySensors.h>
    #include <math.h>
    #include <SPI.h>
    
    const unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    MyMessage msg(CHILD_ID, V_TEMP);
    
    int samples[NUMSAMPLES];
    
    void setup(void) {
    
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("PT Temperature Sensor", "1.1");
      // Present all sensors to controller
    present(0, S_TEMP);
    }
    
    void loop(void) {
      float average;
      // take N samples in a row, with a slight delay
      average = 0;
      for (uint8_t i=0; i < NUMSAMPLES; i++) {
        samples[i] = analogRead(THERMISTORPIN);
        average = samples[1] + samples[2] + samples[3] + samples[4] +samples[5];
        delay(10);
      }
      average = average / NUMSAMPLES;
      average = SERIESRESISTOR * ((1823.0 / average) -1);
    
      float temperature;
     temperature = log(average); // ln(R/Ro)
     temperature = 1 / (0.001129148 + (0.000234125 * temperature) + (0.0000000876741 * temperature * temperature * temperature));
     temperature = temperature - 273.15;  // Convert Kelvin to Celsius 
    
      Serial.print("Temperature ");
      Serial.print(temperature);
      Serial.println(" *C");
      // Send in the new temperature
      send(msg.setSensor(0).set(temperature,1));
      sleep(SLEEP_TIME);
    }
    

    Ncie for the extra in floor heating sensors you have when building.


  • Mod

    What is the actual problem you are having? Do you have a serial debug?



  • The problem I had was it didn't actually read the temp when I combined the sensor with the radio. The radio worked and could send data back to Vera but is was wrong. I removed the reference to the pin and it reads and sends the correct data, I would like to add more than one temp sensor. I want to check my fridge temps similar to the project pet and bulldog complete, with one exception. I want to repurpose my extra floor sensors they only have two wires vs. a Dallas temp that has 3.

    If you have an idea on how to add the extra sensor that would be great.


  • Mod

    If you want to add it in software, you need to add another mymessage object, add a different child ID, present an additional sensor and add the extra sensor code in the sketch (it's pretty much copy and paste). If you don't know how to add it in hardware, it depends on the sensors you have available, wires needed and so on.



  • @Newzwaver

    although I always attach 3 wires to the Dallas DS18B20 sensors (guess you'll be using these...) I think the dallas temp sensor can be run using 2 wires.
    My 'normal' hardware setup is that I connect the middle leg of the DS18B20 to pin 3 of the 'arduino' (could be any other DI pin of course) from 5V a 4k7 resistor on the same pin 3 and both outer legs of the DS18B20 on GND.
    works great for me.

    BR

    Boozz


Log in to reply
 

Suggested Topics

  • 3
  • 1
  • 4
  • 2
  • 3
  • 24

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts