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).
Table of Contents:
Throughout this tutorial, we’ll cover the following contents:
- The Raspberry Pi Pico’s Internal Temperature Sensor
- Reading the Temperature using ADC
- Reading the Temperature using picozero
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.
Reading the Temperature using ADC
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/
from machine import ADC
# Internal temperature sensor is connected to ADC channel 4
temp_sensor = ADC(4)
def read_internal_temperature():
# Read the raw ADC value
adc_value = temp_sensor.read_u16()
# 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
temperatureC = read_internal_temperature()
temperatureF = celsius_to_fahrenheit(temperatureC)
print("Internal Temperature:", temperatureC, "°C")
print("Internal Temperature:", temperatureF, "°F")
Recommended reading: Raspberry Pi Pico: Read Analog Inputs (MicroPython).
How the Code Works
Let’s take a quick look at how the code works.
Creating an ADC Object
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.
- Go to Tools > Manage Packages.
- Search for picozero.
- Click on the first picozero @ PyPi option.
- 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")
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.
Continuous Temperature Readings
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)
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:
- Raspberry Pi Pico: Read Analog Inputs (MicroPython)
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)
- Raspberry Pi Pico: PWM Fading an LED (MicroPython)
Learn more about the Raspberry Pi Pico with our resources:
Thanks for reading.
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
Hi.
Try to re-install the picozero package and test the code again.
Regards,
Sara
Hi Sara, I did the re-installation 3x without any progress. Then I copied the lib directly to the device and the sketch worked. Regards Dieter
Hi, unable to install picozero in thonny.
Hi.
What error do you get?
Can you provide more details?
Regards,
Sara
Hi, i got this ‘–disable-pip-version-check’, ‘–trusted-host’, ‘127.0.0.1’, ‘install’, ‘–no-compile’, ‘–use-pep517’, ‘–upgrade-strategy’, ‘only-if-needed’, ‘picozero’, ‘–index-url’, ‘http://127.0.0.1:36628′]’ returned non-zero exit status 2.
i am on linux pc.
Hi.
Double-check the firmware on your Raspberry Pi Pico board and load it again.
Then, try again.
Regards,
Sara