0

I'm working on a Raspberry Pi pico and I'm using the C pico-sdk. I want to get the temperature of my package so I followed the datasheet of the rp2040 (page 565) and I've used the following code :

void init_ADC() {
    adc_gpio_init(GPIO_ADC); //26
    adc_gpio_init(GPIO_BATTERY_LEVEL); //29
    adc_init();
    adc_set_round_robin(0);
    adc_set_temp_sensor_enabled(1);
}

uint16_t get_cpu_temp() {
    adc_select_input(ADC_TEMP_SENSOR); // 4
    uint16_t value = adc_read();
    return value;
}

So according to the datasheet, my function get_cpu_temp is supposed to get the value of the ADC that will be between 0 and 65535 since it's an uint16. After that I use the following formula :

uint16_t res = get_cpu_temp();
float temp = 27.0f - ((res *3.3f/65535.0f)-0.706f)/0.001721.0f;

The resulting value of temp is around 413°C which is obviously wrong, I'm expecting a value around 40°C. I looked for the value of res and it's around 810.

Does anyone sees what I'm doing wrong ? Thanks.

1 Answer 1

1

I think your mistake is to use "65535". Here you suppose that get_cpu_temp() will return a value between 0 and 65535 but the ADC detailed in datasheet RP2040 is a 12 bit ADC so the result will vary between 0 and 4095. Maybe you get a better explanation here : https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/#Temperature_Sensor_of_Raspberry_Pi_Pico

They say :

the MicroPython code can scale the ADC values to a 16-bit range.

So their Python code uses a value between 0 and 65535 but maybe your C code should use a maximum of 4095.

1
  • Thanks for your answer ! seems like it's working, I'm getting 45°C :)
    – Bibibou
    Aug 10, 2023 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.