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 Answers1

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

Milliways
  • 62,573
  • 32
  • 113
  • 225