Raspberry Pi Pico using onboard Temperature Sensor

Hans-Petter Halvorsen

1 Introduction

In this Tutorial we will use Raspberry Pi Pico. We will use the built-in Temperature Sensor on the Pico hardware. The Temperature Sensor is inside the RP2040 microcontroller chip which is located on the Raspberry Pi Pico. It can be used for simple application and for test and demo purposes. You can use it as an approximation of the surrounding air temperature, but the value will typically be a bit higher since the sensor is located on the RP2040 microcontroller chip itself.

Raspberry Pi Pico using onboard Temperature Sensor (YouTube) + PowerPoint (PDF)

 

1.1 Raspberry Pi Pico

Raspberry Pi Pico is a “downscaled” version of the original Raspberry Pi and is more comparable with Arduino compared to the original Raspberry Pi. You also need to use a downscaled version of Python, called MicroPython.

Haven't used Raspberry Pi Pico before? Getting Started with Raspberry Pi Pico

 

1.2 Python

As mentioned, Raspberry Pi Pico uses a downscaled version of Python, called MicroPython, but most of the original Python will work.

Here you will find more Python Resources.

 

2. Built-in Temperature Sensor

The raspberry Pi Pico has a built-in Temperature Sensor. The Temperature Sensor is inside the RP2040 microcontroller chip which is located on the Raspberry Pi Pico. It can be used for simple application and for test and demo purposes.

The main code structure to read a value from the temperature sensor is as follows:

1. Initialization

adcpin = 4

sensor = machine.ADC(adcpin)

The internal temperature sensor is connected to an internal ADC pin #4.

 

2. Read from ADC:

adc_value = read_u16()

The built-in Analog-to-Digital Converter (ADC) on Raspberry Pi Pico has a 16 bit resolution (2^16 = 65536 different levels), producing values from 0 to 65535. The read_u16() function gives a value between 0 and 65535.

 

3. Convert raw ADC Value (0-65535) to Voltage Value (0-3.3v):

volt = (3.3/65535)*adc_value

The read_u16() function gives a value between 0 and 65535. It must be converted to a Voltage Signal 0 - 3.3v.

 

4. Convert from Voltage Value to Temperature in degrees Celsius:

temperature = 27 - (volt - 0.706)/0.001721

Finally we convert the volatge value to a value in degrees Celsius. This formula is documented in the Raspberry Pi Pico RP2040 datasheet.

 

MicroPython Code Example:

Basic Code Example that reads the Temperature value from the built-in Temperature Sensor:


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)

You find more information about this sensor in the Datasheet (Chapter 4.9.5. Temperature Sensor)
https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf

You can use it as an approximation of the surrounding air temperature. The Temperature Sensor is located inside the RP2040 microcontroller on the Raspberry Pi Pico board, so the values from the sensor will typcally be higher than the room temperature.

More information/resources regarding creating and using Functions in Python:

Create Functions with Python (YouTube) + PowerPoint (PDF)

 

 

Final Application

You may also want to structure the code into a Class and a seperate Python module to make it easier to reuse the code in different applications. The code is shown below.

Temperature Sensor Module (Filename: PicoSensor.py):


from machine import ADC

class Temperature:
    def __init__(self):
        adcpin = 4
        self.sensor = ADC(adcpin)
        
    def ReadTemperature(self):
        adc_value = self.sensor.read_u16()
        volt = (3.3/65535)*adc_value
        temperature = 27 - (volt - 0.706)/0.001721
        return round(temperature, 1)

 

More information/resources regarding Functions, Classes and Modules in Python:

Create Functions with Python (YouTube) + PowerPoint (PDF)

Create Classes in Python (YouTube) + PowerPoint (PDF)

 

Main Program:


from PicoSensor import Temperature
import time

sensor = Temperature()

while True:
    temperature = sensor.ReadTemperature()
    print(temperature)
    time.sleep(5)

 

For more Raspberry Pi Pico Resources, Tutorials, Examples and Videos see the main Raspberry Pi Pico page.

 

Additional Resources

Below you will find more interesting and relevant resources


IoT

Internet of Things

Raspberry Pi

Raspberry Pi resouces

Arduino

Arduino resouces

Python

Here you find lots of interesting IoT Projects, etc.