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?