Onboard Temperature Sensor

1. Learning Objectives

  1. Learn the basic use of the ADC pins of the Raspberry Pi Pico W board.
  2. Learn how to read the temperature by the onboard temperature sensor.

2. Hardware Construction

This course directly utilizes the temperature sensor on the Raspberry Pi Pico W board without additional hardware equipment.

The RP2040 microcontroller from the Raspberry Pi Pico W is a digital device, like all mainstream microcontrollers: it's made up of thousands of transistors, tiny switching devices that either turn on or off. So your PICO W can't really understand an analog signal - it can be anything on the spectrum between fully off and fully on - without relying on extra hardware: an analog-to-digital converter (ADC).

An ADC has two key characteristics: its resolution, measured in digital bits, and its channels, or how many analog signals it can accept and convert at a time. The ADC in your PICO W has a resolution of 12 bits, which means it can convert an analog signal to a digital signal with numbers ranging from 0 to 4095 - although this is handled in MicroPython, converting to a 16-bit number from 0 to 65,535, so it behaves the same as the ADC on other MicroPython microcontrollers. It has three channels brought to the GPIO pins: GP26, GP27, and GP28, which are also called GP26_ADC0, GP27_ADC1, and GP28_ADC2 for analog channels 0, 1, and 2. There is also a fourth ADC channel, which is connected to a temperature sensor built into the RP2040.

3. Program Analysis

C programming

For the use of SDK in C, please refer to the previous courses related to environment building.

#include "hardware/adc.h"

This library contains the related functions used by the adc's hardware.

stdio_init_all()

Initializes all current standard stdio types linked to the binary. Once you have set the clock, call this method to enable support for UART, USB, and semi-managed stdio depending on the presence of their respective libraries in the binary.

adc_init()

Initialize ADC.

adc_set_temp_sensor_enabled(true)

The ADC input selection is 4, and the parameters 0...3GPIO are 26...29 respectively. Input 4 is the onboard temperature sensor.

const float conversion_factor = 3.3f /(1 << 12)

The resolution is 12 bits, the highest IO voltage is 3.3V, and the ratio can be obtained to obtain the voltage corresponding to the unit value, so as to calculate the voltage read by the IO.

adc_read()

Perform ADC conversion, wait for result, then return result.

float reading = adc_read()* conversion_factor

Calculate the converted voltage value.

float temperature = 27 - (reading - 0.706)/0.001721

Converted to temperature value.

printf("reslut: 0x%x, reading: %f V, temperature: %f \n", result, reading,temperature)

Print related variable values, which can be viewed through serial port 0.

4. Program creation and burning

To create a project, please refer to the previous tutorial on creating a project, which will not be repeated here. After writing the program, you need to add the standard library to the build according to the program you wrote before compiling, open the CMakeLists.txt in the project, and check the target_link_libraries(temptutre pico_stdlib hardware_adc), you can see that because the previous program uses pico_stdlib and hardware_adc library. If other libraries are added to the functions used, the corresponding libraries need to be added. If you do not know where to add the libraries, you can go to the official pico-examples case downloaded earlier, which contains the cases of each function and the corresponding CMakeLists.txt file for reference. After the modification is completed, save and exit, and enter the bulid path of the project. Enter the command to compile

Needs attention This routine only provides source code. If you want to compile, please create a new project by yourself, and put the source code files into the new project.

Burning After compiling, you can generate files in formats such as .bin .hex .elf .uf2 in the build directory. Drag the u2f in the above file to the disk recognized by Pico W to burn. (Note: When programming for the first time, it is an empty code. Connect Pico W to the USB to directly identify the disk. When there is an executable program in it, you need to press and hold the BOOTSEL button before connecting to the USB)

5. Experimental phenomenon

After the program download is complete, observe the temperature value through the serial port debugging assistant.