substract 32 from uint8 temperature_sensor_read()

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

substract 32 from uint8 temperature_sensor_read()

Postby rudi ;-) » Sat Dec 26, 2015 3:14 pm

hi
think there is a little bug in read function.
we must subtract 32 from reading result to get a valid temperature.
think there is a mistake in base formula in read function.

right working code, this code
adjusted by own subtract 32 from the bug [***]

Code: Select all


// simple code for the measurement of OnBoard sensor
// rudi 26 Dez 2015
// base is RTOS SDK 1.1.0
// ESP31B (1. Revision "false silkscreen")
 
uint8 temper;  // result in fahrenheit
float celsius;    // conversion in Celsius later
uint8 bugCorrection = 32 ; // bug correction value   [***]

// which can differ locally, fine tuning is always made
// by each itself, for me it was 0.6 degrees Celsius,
// that are 1,08 Fahrenheit ,  i round down to 1 here:
uint8 calibrateCorrection = 1 ; // calibrate with an mercury thermometer
 
// read now from SoC
temper= temperature_sensor_read();

// [***]
// bugCorrecture - 32
temper= temper - bugCorrection;

// calibrate with an mercury thermometer - 1 Fahrenheit
temper= temper - calibrateCorrection;

// conversion fahrenheit in celsius
celsius = ( temper - 32 ) / 1.8;

// output fahrenheit and celsius result
printf("Sensor onBoard is F: %i    C: %.2f\n", temper, clesius);



can u please check, this is my final version
posted in SDK http://esp32.com/viewtopic.php?f=13&p=321#p321

best wishes
rudi ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

DarkByte
Posts: 4
Joined: Fri Jan 08, 2016 3:00 pm

Re: substract 32 from uint8 temperature_sensor_read()

Postby DarkByte » Sat Jan 09, 2016 7:17 pm

According to specifications, temperature sensor work from -40 to +125.
Return value of temperature_sensor_read function is unsigned.
So you need substract 40 from returned value to get valid results ;)

Also, to convert from Fahrenheit to Celsius, you need substract 32 and multiply by 5/9.

Code: Select all

		uint8 tRaw = temperature_sensor_read();
		int8 tFar = tRaw - 40;
		int8 tCel = (tFar - 32) * 5/9;
		printf("Raw: %d Far: %d Cel: %d\n", tRaw, tFar, tCel);

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: substract 32 from uint8 temperature_sensor_read()

Postby rudi ;-) » Sat Jan 09, 2016 10:33 pm

DarkByte wrote:According to specifications, temperature sensor work from -40 to +125.
Return value of temperature_sensor_read function is unsigned.
So you need substract 40 from returned value to get valid results ;)

Also, to convert from Fahrenheit to Celsius, you need substract 32 and multiply by 5/9.

Code: Select all

		uint8 tRaw = temperature_sensor_read();
		int8 tFar = tRaw - 40;
		int8 tCel = (tFar - 32) * 5/9;
		printf("Raw: %d Far: %d Cel: %d\n", tRaw, tFar, tCel);

Thank you!
i confirm with you, near ( uint8 *) 40 total substract from (uint8 *) tRaw read is used here, too.( in tests by steps 38-42 );
also i confirm with you to multiply by 5/9 what the same is like 1.8 ( 9 / 5 = 1.8 ) 8-)
i take 1.8 because wiki and other info sites ment. to 1.8 ( other ment. to 5/9 )

but i can not confirm with you (int8 *) tFar = (uint8 *) tRaw - 40;
because we can have tFar > 127 F ( > 52.778 °C) so we need here (uint8 *) tFar
info: tFar Range: 0..255 F need uint8 ,
what are tCel Range : -40 °C to +127 °C need int8 only in tCel.

because in testing want one decimal in tCel used here (float *) for tCel, but like you ment. (int8 *) tCel is ok too and sufficient.
and because we have (uint8 *) for tRaw it would be nonsense to make it float.
so thank you for the hint. i have now int8 not float for tCel too.

btw "bug"
i assumed times that are 32 (out of thin air without any justification why 32 and not 33..40)
after tests, i calibrate to ambient.
and think, we have 2 calibrates, because if you have measurements without wifi in a while(),
there are other values if you use wifi and makes measurements. test it.

you founded "40" for a conversion or it arises in you from the measurements?
so think, the "bug" is no bug, we can named it to "calibrate total" ? what are you mean?
and what we do in wifi? then a small further deviation.

calc in detail:
( casting only for info )

Code: Select all

int8 bug = 32; 
int8 calibrate = 8;

(uint8 *) tRaw      =   temperature_sensor_read();
(uint8 *) tFar      =  ( uint8 *) tRaw - (int8 *) bug;
(uint8 *) tFar      =  ( uint8 *) tRaw - (int8 *) calibrate;
(float *) tCel_f    =  ((uint8 *) tFar - 32) * 1.8;
//(float *) tCel_f  =  ((uint8 *) tFar - 32) * 5/9;

(int8 *) tCel_i     =  ((uint8 *) tFar - 32) * 5/9;
//(float *) tCel_f  =  ((uint8 *) tFar - 32) * 1.8;

 printf("Raw: %d Far: %d Cel: %d\n",   (uint8 *) tRaw, (uint8 *) tFar, (int8 *)  tCel_i);
 printf("Raw: %d Far: %d Cel: %.1f\n", (uint8 *) tRaw, (uint8 *) tFar, (float *) tCel_f); 

best wishes
rudi ;-)


btw, sensor.h
SRC RTOS SDK

Code: Select all


/**
  * @brief  Read value from temperature sensor.
  *
  * @param  null
  *
  * @return range [0, 255]
  */
uint8 temperature_sensor_read(void);


-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 6 guests