Read internal temperature sensor
- Read the internal temperature sensor of the Raspberry Pi Pico
- Print Temperature values on REPL
Now we are going to modify our Blinking led program a little bit. Here we will import a new module called microcontroller. See the changes in the code and load to your Raspberry Pi Pico.
import board import digitalio import microcontroller import time led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT while True: temp = microcontroller.cpu.temperature print(temp) led.value = True time.sleep(2) led.value = False time.sleep(1)
You will see on the REPL the temperature is Celsius degrees, and the led will be blinking slower. If you want to see a change in the temperature, you can put your finger over the microprocessor (black squared chip) of your Pico board.
Code Update (You can try this one too)
Of you want to costume the REPL to show the de units you can introduce the command print(f’text’) and pass the value of the temp variable:
import digitalio import microcontroller import time led = digitalio.DigitalInOut(board.LED) led.direction = digitalio.Direction.OUTPUT while True: temp = microcontroller.cpu.temperature temp = round(temp,2) print(f'Temperature {temp} ºC') led.value = True time.sleep(0.5) led.value = False time.sleep(0.5)