Raspberry Pi Pico Onboard Temperature Sensor – MicroPython Guide

In this tutorial, we will learn how to read the in-built temperature sensor in Raspberry Pi Pico. First, we shall write a simple MicroPython script to display temperature values. We shall also learn to interface an OLED display and show the temperature values in it. This guide will work on all variants of Raspberry Pi Pico at the time of publishing this guide, including the Raspberry Pi Pico W.


The ADC in Raspberry Pi Pico & Pico W

Raspberry Pi Pico & Pico W features the RP2040 microcontroller. The RP2040 has a total of 5 ADC(Analog-to-Digital Converter) channels. ADC0, ADC1, ADC2, and ADC3 are connected to GP26, GP27, GP28, and GP29 respectively. These four are 12-bit SAR-based ADCs. The fifth ADC channel is connected to an internal temperature sensor. These ADCs measure analog voltage in the range of 0-3.3 Volts.

raspberry pi pico w pinout
Raspberry Pi Pico W Pinout. Image Source: Datasheet

The ADCs have a sampling rate of 500kS/s and use an independent 48Mhz clock for this purpose. Each sample takes 96 clock cycles. So the sampling time taken per sample is (96/48Mhz)=2μS.

The 12-bit ADC register will give us a value ranging from 0 to 4095 (2^12-1).  But specific functions in MicroPython can scale the ADC values to give us a 16-bit range. So we can get sensor readings ranging from 0 to 65535 (2^16-1). We shall see this shortly in the code below. In Arduino UNO, we get a 10-bit resolution ADC. The resolution of an ADC is the smallest analog voltage change for which there occurs a change in digital reading.

To know more about the ADC in Raspberry Pi Pico, read our dedicated article: Raspberry Pi Pico ADC Guide using MicroPython.

About The Raspberry Pi Pico Onboard Temperature Sensor

The onboard temperature sensor in Pi Pico uses a clever trick. According to the datasheet of RP20240, “The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel (AINSEL=4). Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV per degree”. Using this detail, we will now try to find a mathematical expression to derive temperature based on the Vbe voltage.

If we consider temperature along the X-axis, Voltage(Vbe) along the Y-axis and a gradient of -1.721mV/°C, we can plot a graph as shown below.

Vbe vs Temperature graph

The point-slope form of a linear equation is written as y-y1=m(x-x1). For our purposes, we can re-write this equation as V-V1=m(T-T1), where V is the Vbe voltage, m is the gradient and T is the temperature. Let us solve this equation and find T.

Equation to find the temperature

Read Raspberry Pi Pico Temperature Sensor Using MicroPython

We shall be using Thonny IDE to upload a MicroPython script to Pi Pico. If you are a beginner, please look at our article Getting Started With Raspberry Pi Pico on Windows to set up and program your Pico from a Windows computer. For macOS users, follow our guide to Program Raspberry Pi Pico On macOS Using Thonny IDE. Ensure that the UF2 MicroPython file is already flashed in your Pico before proceeding further with this guide.

Steps to upload code and read temperature sensor:

  • Connect Your Pico to your computer via a USB cable.
  • Open Thonny and ensure that the interpreter is set to MicroPython(Raspberry Pi Pico).
  • Enter the following example script to read the temperature:
# Source: Electrocredible.com, Language: MicroPython
from machine import ADC
import time
adc = machine.ADC(4) 
while True:
    ADC_voltage = adc.read_u16() * (3.3 / (65536))
    temperature_celcius = 27 - (ADC_voltage - 0.706)/0.001721
    print("Temperature: {}°C {}°F".format(temperature_celcius,temp_fahrenheit))
    time.sleep_ms(500)Code language: Python (python)
  • Run the code by clicking the Run icon or by pressing the F5 key.
  • Save your script to Raspberry Pi Pico when a prompt appears.
Thonny Save to
  • Give the script a file name with a ‘.py’ extension. I named the file ‘temp_sensor_onboard.py‘. If you want the script to run at startup, save it as “main.py”.
save as main.py in Thonny
  • The Thonny shell window will display the output when your script starts running, as shown in the screenshot below.

Also read: Raspberry Pi Pico BME280 Interfacing Guide Using MicroPython

Raspberry Pi Pico Temperature Sensor Code Explained

First, we have to import the ADC and time modules. The ADC module will help us to read the analog voltage values from pins and convert them to digital data. We shall use the time module to introduce delays in our script.

from machine import ADC
import timepyCode language: Python (python)

We create an adc instance to read the voltage from the fifth ADC channel(ADC4 refers to the fifth channel).

adc = machine.ADC(4) 

Now, we shall read the voltage in the ADC pin. adc.read_u16() will read 16 bits of digital data. As we discussed earlier under the heading The ADC in Raspberry Pi Pico, we will get a reading in the range of 0-65535 for a voltage range of 0V to 3.3V. So, based on the simple unitary method, we multiply the ADC reading with (3.3 / (65536)).

ADC_voltage = adc.read_u16() * (3.3 / (65536))

Based on the equation we solved above in this article, we calculate the value for temperature in Celcius and then convert it to Fahrenheit.

temperature_celcius = 27 - (ADC_voltage - 0.706)/0.001721
temp_fahrenheit=32+(1.8*temperature_celcius)Code language: Python (python)

We then print the value of temperature in Celcius and Fahrenheit and introduce a delay of 500 milliseconds. Without this delay, the results will be printed too fast.

print("Temperature: {}°C {}°F".format(temperature_celcius,temp_fahrenheit))
time.sleep_ms(500)Code language: Python (python)

Display Onboard Temperature Sensor Value On OLED Display

Let us now write a script to display the Raspberry Pi Pico internal temperature sensor data on a 128×64 SSD1306 OLED display. Proper modules must be installed to interface Pico with the OLED display. You can review our article Raspberry Pi Pico OLED Interfacing Tutorial to learn how to install the SSD1306 MicroPython library on Pico.

  • Connect your Pico to the OLED display as shown in the figure below.

Connection Details:

Raspberry Pi Pico PinOLED Display Pin
Pin 1 (GP0)4(SDA)
Pin 2 (GP1)3(SCL)
Pin 36(3.3 Volts Out)2(VDD)
Pin 38(GND)1(GND)

Installation Of OLED Module

With your Pico connected to the computer, Open Thonny IDE and perform the following steps to install the MicroPython library for SSD1306.

  • In Thonny IDE, go to Tools> Manage Packages.
  • Type “micropython-ssd1306” on the search bar and press Enter. From the search results, click on the library that you searched for.
  • In the next window, click on Install.

Code To Display Onboard Temperature On OLED Display

After ensuring that the OLED library has been properly installed, copy and paste the following script into a new window in Thonny IDE.

# Source: Electrocredible.com, Language: MicroPython
from machine import ADC, Pin, I2C 
from ssd1306 import SSD1306_I2C
import time
adc = machine.ADC(4)
WIDTH =128 
oled = SSD1306_I2C(WIDTH,HEIGHT,i2c) 
while True:
    ADC_voltage = adc.read_u16() * (3.3 / (65536))
    temperature_celcius = 27 - (ADC_voltage - 0.706)/0.001721
    oled.text(str(int(temperature_celcius)), 95, 10)
    time.sleep_ms(500)Code language: Python (python)

If you want to understand how this part of the script works, you can view the explanation here.

Run the script and save it on RPi Pico with a “.py” filename extension. You should see the temperature value displayed in the OLED display as shown in the image below.

Final Thoughts On Pi Pico Temperature Sensor

The Raspberry Pi Pico Onboard temperature sensor may not be accurate. Depending on the reference voltage, the temperature reading may vary. Raspberry Pi Pico uses 3.3V as a reference voltage for the ADC. A change of 1% in this reference voltage can vary the temperature reading by as much as 4°C. To improve the accuracy, it is recommended to add an external reference voltage(such as TL431 IC) to the Pico.

DHT22 is a reliable temperature and humidity sensor, which is discussed in our article Raspberry Pi Pico DHT22(AM2302) Interfacing Tutorial.

DHT11 is another cheap and popular temperature & humidity sensor. Its humidity sensing range is less than DHT22. We have explained its interfacing details in the article Raspberry Pi Pico DHT11 MicroPython – Easy Interfacing Guide.

Hope you found this guide on Raspberry Pi Pico temperature sensor to be helpful. To share your thoughts, please comment below. Thank you for reading.





4 responses to “Raspberry Pi Pico Onboard Temperature Sensor – MicroPython Guide”

  1. Nandor Avatar

    Everything worked perfectly. Thanks for sharing.

  2. John Avatar

    How do I send the pico cpu temperature to another Pi using mqtt?

  3. Martin Avatar

    It is very good example but IMO there is little mistake:
    “ADC_voltage = adc.read_u16() * (3.3 / (65535))” IMO must be “…/ (65536)”.
    It is not the max 16bit number (65535 = 2^16-1) but number of values (65536 = 2^16).
    🙂 ???

    1. Abhilekh Das Avatar
      Abhilekh Das

      Thanks for your observation. The code has been modified with necessary corrections.

Leave a Reply

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