3

I have been playing with my newly acquired Raspberry Pi Pico W and reading the temperature.

How much variation between the internal temperature sensor and an external TMP36 wired up to ADC2 should I be expecting?

The value from the internal sensor seem to be lower than the external one, where I would expect it to be the other way around.

Code for reading internal sensor

import machine
import utime


class GetTemp:
    """Read the internal temp sensor"""

    def __init__(self):
        """Init"""
        self.sensor_temp = machine.ADC(4)
        self.convert = 3.3 / 65535
        self.temperature = 0

    def get_temp(self):
        """Read the Internal Temp Sensor"""
        reading = self.sensor_temp.read_u16() * self.convert
        self.temperature = 27 - (reading - 0.706) / 0.001721
        return self.temperature

    def get_temp_f(self):
        """Get Temp in Fahrenheit"""
        return (self.get_temp() * 1.8) + 32


if __name__ == "__main__":
    temp = GetTemp()
    while True:
        print("%.4f C, %.4f F" % (temp.get_temp(), temp.get_temp_f()))
        utime.sleep(1)

Code for reading the TMP36 sensor

import machine
import utime

class GetTemp:
    """Read the temp from a TMP36 sensor"""

    def __init__(self, adc, ref_voltage=3.3):
        """Init"""
        self.sensor_temp = machine.ADC(adc)
        self.convert = ref_voltage * 1000 / 65535
        self.temperature = 0

    def get_temp(self):
        """Read the Internal Temp Sensor"""
        millivolts = self.get_raw() * self.convert
        self.temperature = (millivolts - 550) / 10
        return self.temperature

    def get_temp_f(self):
        """Get Temp in Fahrenheit"""
        return (self.get_temp() * 1.8) + 32
    
    def get_raw(self):
        return self.sensor_temp.read_u16()


if __name__ == "__main__":
    temp = GetTemp(2)
    while True:
        print("%.4f C, %.4f F" % (temp.get_temp(), temp.get_temp_f()))
        utime.sleep(1)

Example Output

TMP36 = 15.6729 C, Internal = 16.2771 C
TMP36 = 15.6729 C, Internal = 16.2771 C
TMP36 = 15.5924 C, Internal = 15.8089 C
TMP36 = 15.5924 C, Internal = 15.8089 C
TMP36 = 15.7535 C, Internal = 15.8089 C
TMP36 = 15.6729 C, Internal = 16.2771 C
TMP36 = 15.6729 C, Internal = 15.8089 C
TMP36 = 15.7535 C, Internal = 16.2771 C
TMP36 = 15.5924 C, Internal = 16.2771 C
TMP36 = 15.6729 C, Internal = 16.2771 C

Is there anything else I can check or troubleshoot with the TMP36 sensor to make it more accurate?

1 Answer 1

1

You don't give any output, but I notice your code (which I haven't analysed in detail) gives slightly different results to mine (on Pico).

23.29925

24.2355 C, 74.7813 F
from machine import ADC
import utime

sensor_temp = ADC(ADC.CORE_TEMP)
conversion_factor = 3.3 / (65535)

while True:
    reading = sensor_temp.read_u16() * conversion_factor
    
    temperature = 27 - (reading - 0.706)/0.001721
    print(temperature)
    utime.sleep(2)

I believe this may be because ADC is expecting a pin not an integer. I had encountered a similar issue reading VSYS. See https://forums.raspberrypi.com/viewtopic.php?p=1809725#p1809725

4
  • Thank you, that's interesting, I will do some tests to see if that makes a difference. Jan 11 at 8:56
  • Although, looking at the type of ADC.CORE_TEMP it is a <class 'int'>, confused. Jan 11 at 9:10
  • @AlastairMontgomery I did similar and agree but there is no doubt from machine import ADC print(ADC(4).read_u16()) print(ADC(ADC.CORE_TEMP).read_u16()) produces different result (on initial run). Subsequent runs are similar. I guess some undocumented initialisation issue.
    – Milliways
    Jan 11 at 10:24
  • True, I've updated my class to use ADC.CORE_TEMP instead of 4 and the readings are now more consistent. Thanks again. Jan 11 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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