Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Community Manager
996 Views

Convert temeprature to *C

Hi,

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

http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor_V1.2# Download Grove - Temperature Sensor V1.2 - Wiki

or even (Python -> C)

https://github.com/Seeed-Studio/Grove_Starter_Kit_for_BBG/blob/master/grove_temperature_sensor.py 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 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ł

0 Kudos
2 Replies
Highlighted
Community Manager
1 View

Hi Pawek,

 

 

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

 

 

# include

 

# include

 

# include

 

# 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.
0 Kudos
Highlighted
Community Manager
1 View

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:

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.

0 Kudos