Raspberry Pi Pico temperature sensor

Posted by

Let’s get to know the Raspberry Pi Pico temperature sensor, an internal sensor that can be easily read using microPython. It is a sensor that is inside the RP2040 chip.

According to the Pi Pico datasheet, the sensor is a polarized diode using an internal voltage reference. This voltage is not very accurate, so even small errors can turn into large variations in temperature readings.

Pi Pico temperature sensor
Raspberry Pi Pico internal temperature sensor

The equation for obtaining temperature from voltage is below.

volt = (3.3/65535) * adc_value
temperature = 27 - (volt - 0.706)/0.001721

First, “volt” is calculated, which is the raw voltage coming from the analog-to-digital converter. This is done using the board’s 3.3V supply. Then the temperature is calculated taking into account the sensor voltage at 27 degrees (0.706V) and its slope of -1.721mV/ºC. A temperature is then obtained in degrees Celsius.

The complete microPython code is seen below. The reference I used was this blog. It depends on the “machine” library to handle IO (in this case, analog) and “time” to handle delay.

I used the Thonny IDE software to program the Raspberry Pi Pico, as already seen here.

import machine
import time

adcpin = 4
sensor = machine.ADC(adcpin)
  
def ReadTemperature():
    adc_value = sensor.read_u16()
    volt = (3.3/65535) * adc_value
    temperature = 27 - (volt - 0.706)/0.001721
    return round(temperature, 1)
  
while True:
    temperature = ReadTemperature()
    print(temperature)
    time.sleep(5)

Note that “adcpin = 4” means that the sensor is internally connected to pin 4. The “round(temperature, 1)” function says that the temperature will be shown with one decimal place. Below is an image of the Thonny IDE console, showing the temperatures read every 5 seconds.

Raspberry Pi Pico temperature readings
Raspberry Pi Pico temperature readings

In the near future we will talk more about the Raspberry Pi Pico, stay tuned.

Leave a Reply

Your email address will not be published. Required fields are marked *