This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can i convert the temperature using sd_temp_get() to an uint8_t?

Hello

I am using the experimental_ble_app_uart for s110. I want to use the sd_temp_get() and convert the temperature value to a uint8_t, but if understand correctly when i do this i only get information from the first bytes of the int32_t and these are not the actual temperature value. I was wondering if someone could help me with this problem. This is part of the code i am using at the moment.

    static int32_t temp;
	uint32_t err_code;
	
	err_code = sd_temp_get(&temp);		
	APP_ERROR_CHECK(err_code);
	
	uint8_t temp1 = temp;
Parents
  • The value from sd_temp_get() is in units of 0.25 degrees Celsius (source). This means a value of 4 is 1 degree Celsius, a value of 8 is 2 degrees Celsius, a value of 12 is 3 degrees Celsius, etc.

    Assuming you wish to convert the result into a whole number, you would simply divide it by four. I wouldn't worry about any integer overflow as a result of going from a 32 bit to an 8 bit number since the operating range of the nrf514122 is between -25 and 75 degrees C. However, since the temperature can drop below zero, you should use an int8_t instead of a uint8_t.

    So your code would look like this:

    int32_t raw_temp;
    sd_temp_get(&raw_temp);
    int8_t temp = raw_temp >> 2;  // divide by 4
    
  • Hi

    In addition to Nick's answer, you may need to calibrate the output of the TEMP peripheral. The potential error read on the TEMP peripheral should be pure offset error. You can calibrate the temperature value in software, i.e. if you know the temperature in your room, just add or subtract from the measured value to get the correct value.

Reply Children
No Data
Related