2 Replies Latest reply on Aug 4, 2016 11:55 PM by Pawek

    Convert temeprature to *C

    Pawek

      Hi,

      What am I doing wrong. I believe i properly converted the code (Arduino -> C) from that example:

      Grove - Temperature Sensor V1.2 - Wiki

      or even (Python -> C)

      Grove_Starter_Kit_for_BBG/grove_temperature_sensor.py at master · Seeed-Studio/Grove_Starter_Kit_for_BBG · GitHub

       

      but I think that values are wrong.

       

      Here my code:

       

      int get_temp_in_celcius(void)
      {
        const int B=4255;
        int a;
      
          a = mraa_aio_read(temp);
          float R = 1023.0/((float)a)-1.0;
          R = 100000.0*R;
          float temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15; // looks ok: http://www.seeedstudio.com/wiki/images/a/a1/NCP18WF104F03RC.pdf
         printf("temp: %f\n",temperature);
      
         return 20; /*I know*/
      }
      

       

      It prints about 15*C  but I know here is much hotter.

       

      I would appreciate any support.

       

      Paweł

        • 1. Re: Convert temeprature to *C
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Pawek,

          I believe the code translated to C should look like the following:

          #include <stdio.h>
          #include <unistd.h>
          #include <math.h>
          #include "mraa/aio.h"

          int main()  
          {
           mraa_aio_context adc_a0;
           float adc_value = 0.0;
           const int B=4255;

           adc_a0 = mraa_aio_init(0);
              if (adc_a0 == NULL) 
              {
                  return 1;
              }

              for (;;) 
              {
            adc_value = mraa_aio_read_float(adc_a0);

            float R = 1023.0/(adc_value)-1.0;
            R = 100000.0*R;
            float temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15;
            printf("It is : %f\n degrees",temperature);
            sleep(1);
           }

           mraa_aio_close(adc_a0);
           return MRAA_SUCCESS;  
          }

          Nevertheless, I don't have access to that sensor so I didn't try it. I did compile it and run it, but as expected the values were negative (~-90) because there was nothing connected. The ADC should be working but I'm not sure about the math, I just copied and pasted that part.

          I hope this helps.
          -Peter.

          • 2. Re: Convert temeprature to *C
            Pawek

            Hi Peter,

             

            I think our codes are almost exactly the same.

            Except that you use mraa_aio_read_float(adc_a0); and I mraa_aio_read(temp) so I compared it:

            112164.PNG

            As you can see my temperature readings are also wrong even thought I have plugged temp sensor in.

            My aios initialization are in separated function, nevertheless it is called before get_temp_in_celcis().

             

            Thanks for support,

            Paweł

             

             

             

             

            ======================================EDIT========================================================

            Ok, I changed VCC to 5 V. Now my algorithm (with reading to int) seems to return proper values.

            23132.PNG