# Raspberry Pi Pico: Read the Internal Temperature Sensor (MicroPython)

The Raspberry Pi Pico comes with a built-in temperature sensor connected to ADC4. In this quick guide, youâ€™ll learn how to get temperature data from that sensor. We’ll show you two different methods to get the temperature using: analog read and picozero module.

New to the Raspberry Pi Pico? Read the following guide: Getting Started with Raspberry Pi Pico (and Pico W).

Throughout this tutorial, weâ€™ll cover the following contents:

## MicroPython Firmware

To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.

The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.

Alternatively, if you like programming using VS Code, you can start with the following tutorial:

# The Internal Temperature Sensor

The Raspberry Pi Pico internal temperature sensor works by checking the voltage of a special diode connected to ADC4 on the Raspberry Pi Pico.

According to the datasheet, when it’s 27 degrees Celsius, the voltage is around 0.706V, and for every degree the temperature changes, the voltage goes down by about 1.721mV.

To find the temperature in Celsius using the voltage, you can use this formula:

``T = 27 - (ADC_voltage - 0.706)/0.001721``

Accurate temperature measurements with this sensor can be challenging due to variations in voltage and slope between devices. Consider it more as a reference tool than a precise measurement device. You may need to make adjustments for accuracy.

To get the temperature from the internal temperature sensor, you can use the following code.

``````# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-internal-temperature-micropython/

# Internal temperature sensor is connected to ADC channel 4

# Convert ADC value to voltage
voltage = adc_value * (3.3 / 65535.0)

# Temperature calculation based on sensor characteristics
temperature_celsius = 27 - (voltage - 0.706) / 0.001721

return temperature_celsius

def celsius_to_fahrenheit(temp_celsius):
temp_fahrenheit = temp_celsius * (9/5) + 32
return temp_fahrenheit

# Reading and printing the internal temperature
temperatureF = celsius_to_fahrenheit(temperatureC)
print("Internal Temperature:", temperatureC, "Â°C")
print("Internal Temperature:", temperatureF, "Â°F")
``````

View raw code

### How the Code Works

Letâ€™s take a quick look at how the code works.

You start by importing the ADC class from the machine module to read analog signals.

``from machine import ADC``

The internal temperature sensor is connected to ADC4. So, we create an ADC object on that pin called temp_sensor.

``temp_sensor = ADC(4)``

#### Reading the Internal Temperature Sensor

Then, we create a function to read the internal temperature sensor called read_internal_temperature().

``def read_internal_temperature():``

We get the ADC reading using the read_u16() function. Learn how to read analog signals with the Raspberry Pi Pico with this guide.

``adc_value = temp_sensor.read_u16()``

Then, we need to convert that number to a voltage value. We know that 3.3V corresponds to 65535, so, we can calculate it as follows.

``voltage = adc_value * (3.3 / 65535.0)``

Finally, we can use the formula provided by the datasheet to get the temperature in Celsius.

``temperature_celsius = 27 - (voltage - 0.706) / 0.001721``

The function returns the temperature in Celsius.

``return temperature_celsius``

#### Converting to Fahrenheit

We also included the celsius_to_fahrenheit() function to convert the temperature in Celsius to Fahrenheit.

``````def celsius_to_fahrenheit(temp_celsius):
temp_fahrenheit = temp_celsius * (9/5) + 32
return temp_fahrenheit``````

#### Getting and Printing the Temperature Readings

We call the read_internal_temperature() function to get the temperature in Celsius and save it to the temperatureC variable.

``temperatureC = read_internal_temperature()``

Then, we call the celsius_to_fahrenheit() function and pass as an argument the temperatureC to get the temperature in Fahrenheit.

``temperatureF = celsius_to_fahrenheit(temperatureC)``

Finally, we print the readings in the shell.

``````print("Internal Temperature:", temperatureC, "Â°C")
print("Internal Temperature:", temperatureF, "Â°F")``````

## Testing the Code

Copy the code provided to Thonny IDE. Click on the green Run button.

The temperature of the sensor will be printed in the Shell both in Celsius and Fahrenheit degrees.

## Reading the Temperature using picozero

Thereâ€™s an easier way to get a reading from the internal temperature sensor using the picozero module. The picozero module is a beginner-friendly library with easy â€śrecipesâ€ť to use the most common sensors. If you want to learn more about the picozero library, you can check the picozero documentation.

The picozero library comes with a class called pico_temp_sensor that allows you to easily get the temperature without the need for any calculations (everything is done in the background).

### Installing the picozero Package

You need to install the picozero package before proceeding.

1. Go to Tools > Manage Packages.
2. Search for picozero.
3. Click on the first picozero @ PyPi option.
4. Finally, click Install.

After a few seconds, the package will be installed.

## Code

The following code does the same as the previous example, but it uses the picozero library instead.

``````# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-internal-temperature-micropython/

from picozero import pico_temp_sensor

# Convert from celsius to fahrenheit
def celsius_to_fahrenheit(temp_celsius):
temp_fahrenheit = temp_celsius * (9/5) + 32
return temp_fahrenheit

# Reading and printing the internal temperature
temperatureC = pico_temp_sensor.temp
temperatureF = celsius_to_fahrenheit(temperatureC)

print("Internal Temperature:", temperatureC, "Â°C")
print("Internal Temperature:", temperatureF, "Â°F")
``````

View raw code

We first, need to import the pico_temp_sensor class from the picozero module.

``from picozero import pico_temp_sensor``

Then, we get the temperature using pico_temp_sensor.temp. We save the temperature in Celsius degrees on the temperatureC variable.

``temperatureC = pico_temp_sensor.temp``

If you run this code on your Raspberry Pi Pico, youâ€™ll get the same result as the previous example.

If you want to continuously get new temperature readings, you can read and print the readings inside a while loop. In that case, the code will look as follows:

``````# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-internal-temperature-micropython/

from time import sleep
from picozero import pico_temp_sensor

# Convert from celsius to fahrenheit
def celsius_to_fahrenheit(temp_celsius):
temp_fahrenheit = temp_celsius * (9/5) + 32
return temp_fahrenheit

while True:
# Reading and printing the internal temperature
temperatureC = pico_temp_sensor.temp
temperatureF = celsius_to_fahrenheit(temperatureC)

print("Internal Temperature:", temperatureC, "Â°C")
print("Internal Temperature:", temperatureF, "Â°F")

# Wait one second between each reading
sleep(1)
``````

View raw code

With this example, you’ll get new temperature readings printed on the MicroPython shell every second until you stop the program.

## Wrapping Up

The Raspberry Pi Pico comes with an internal temperature sensor to give you a general idea of the Pico’s internal temperature.

Reading the temperature is as easy as reading the voltage on ADC4 and then making the necessary calculations. Alternatively, you can also use the picozero package.

We hope you’ve found this tutorial useful. If you’re just getting started with the Raspberry Pi Pico, you might also like the following tutorials:

#### SMART HOME with Raspberry Pi, ESP32, ESP8266 [eBook]

Learn how to build a home automation system and weâ€™ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB databaseÂ DOWNLOAD Â»
Learn how to build a home automation system and weâ€™ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB databaseÂ DOWNLOAD Â»

### Recommended Resources

Build a Home Automation System from Scratch Â»Â With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course Â»Â Build IoT and home automation projects.

Arduino Step-by-Step Projects Â» Build 25 Arduino projects with our course, even with no prior experience!

## 3 thoughts on “Raspberry Pi Pico: Read the Internal Temperature Sensor (MicroPython)”

1. Hi Sara, thanks for this tutorial. Installation of fw, download of pico zero lib, 1st sketch to read out temp – all run perfectly. At the 2nd sketch using the picozero lib I have problems. Debug message was ‘ no module named picozero’. I run Thonny 4.1.4 and pico fw Pico W/WH V1.22.2., picozero 0.42 lib. I can see the lib in the lib list and on my pc. Do you have any idea how to solve ?
Thanks for your support ! Dieter