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 using Arduino IDE.
New to the Raspberry Pi Pico? Read the following guide: Getting Started with Raspberry Pi Pico (and Pico W).
Prerequisites
You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:
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.
Accurate temperature measurements with this sensor can be challenging. Consider it more as a reference tool than a precise measurement device.
Reading the Temperature using ADC
To read the Raspberry Pi Pico internal temperature sensor using Arduino IDE, you can use the analogReadTemp() function that returns the temperature in Celsius degrees.
You can use the following code to test it. This code prints the temperature in Celsius and Fahrenheit degrees to the Serial Monitor.
/*********
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-internal-temperature-arduino/
*********/
float tempC;
float tempF;
void setup() {
Serial.begin(115200);
}
void loop() {
tempC = analogReadTemp(); // Get internal temperature
tempF = tempC * 9.0 / 5.0 + 32.0; // Fahrenheit conversion
Serial.print("Temperature Celsius (ºC): ");
Serial.println(tempC);
Serial.print("Temperature Fahrenheit (ºF): ");
Serial.println(tempF);
delay(1000);
}
How the Code Works
Let’s take a quick look at how the code works.
Recommended reading: Raspberry Pi Pico: Read Analog Inputs (Arduino IDE)
Initializing variables
Start by creating two variables tempC, and tempF to hold the temperature values in Celsius and Fahrenheit degrees.
float tempC;
float tempF;
Initialize the Serial Monitor
In the setup(), initialize the Serial Monitor at a baud rate of 115200
void setup() {
Serial.begin(115200);
}
Get and Print the Temperature
We get the Raspberry Pi Pico internal temperature sensor in Celsius degrees by using the analogReadTemp() function.
tempC = analogReadTemp(); // Get internal temperature
Then, we convert the temperature in Celsius to Fahrenheit degrees.
tempF = tempC * 9.0 / 5.0 + 32.0; // Fahrenheit conversion
Finally, we print the readings in the Serial Monitor.
Serial.print("Temperature Celsius (ºC): ");
Serial.println(tempC);
Serial.print("Temperature Fahrenheit (ºF): ");
Serial.println(tempF);
The readings are printed every second (1000 milliseconds).
delay(1000);
Testing the Code
Upload the code to the Raspberry Pi Pico. Don’t know how to upload code? Check this tutorial: Programming Raspberry Pi Pico with Arduino IDE.
Open the Serial Monitor at a baud rate of 115200.
New temperature readings in Celsius and Fahrenheit degrees will be printed in the Serial Monitor every second.
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 Pico’s temperature using Arduino IDE is as easy as using the analogReadTemp() function. You may take this project further and display the temperature on an OLED display: Raspberry Pi Pico: SSD1306 OLED Display (Arduino IDE).
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 (Arduino IDE)
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (Arduino IDE)
- Raspberry Pi Pico: Fading an LED using PWM (Arduino IDE)
Learn more about the Raspberry Pi Pico with our resources:
Thanks for reading.
I Have this error
C:\Users\dell\Documents\Arduino\sketch_apr08c\sketch_apr08c.ino: In function ‘void loop()’:
sketch_apr08c:14:11: error: ‘analogReadTemp’ was not declared in this scope
tempC = analogReadTemp(); // Get internal temperature
^~~~~~~~~~~~~~
C:\Users\dell\Documents\Arduino\sketch_apr08c\sketch_apr08c.ino:14:11: note: suggested alternative: ‘analogRead’
tempC = analogReadTemp(); // Get internal temperature
^~~~~~~~~~~~~~
analogRead
exit status 1
‘analogReadTemp’ was not declared in this scope
Hi.
Do you have a Raspberry Pi Pico board selected in Tools > Board?
Do you have the Raspberry Pi Pico boards installed in Arduino IDE?
Regards,
Sara
Yes, I did that, according to your examples. The sketch worked fine at first, but now it gives this error message
What error message?
Hello, I am using Raspberry Pi Pico 2, and it gives me this error.
Are the two boards compatible?
Temp._Pico:14:11: error: ‘analogReadTemp’ was not declared in this scope
tempC = analogReadTemp(); // Get internal temperature
suggested alternative: ‘analogRead’.
Thank you
Best Regards
Hi.
Do you have a Raspberry Pi Pico board selected in Tools > Board?
Regards,
Sara
Yes, I have