Raspberry Pi Pico OnBoard Temperature Sensor:
In this article, I am going to explain how to use the Raspberry Pi Pico Onboard temperature sensor and display its value on the SSD1306 Oled display module. I going to use this temperature sensor for monitoring the ambient temperature. I don’t know if this sensor is going to give me the exact value as its directly mounted on the Pico board as it may sense some heat from the board.
This onboard temperature sensor can be quite useful in situations when used in high-temperature areas, so this sensor can monitor the Pico board temperature and when the temperature exceeds a predefined value a Fan or other cooling system is automatically turned ON. So, let’s go ahead and take a look at the circuit diagram.
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Raspberry Pi Pico Temperature Sensor Circuit Diagram:
For this project, you only need to interface the Oled display module with the Raspberry Pi Pico. The connections I have already explained in example5.
Now, you might be thinking to which pin this onboard temperature sensor is connected? This onboard temperature sensor is connected to one of the ADCs or Analog-to-Digital Converters. The temperature sensor does not have a physical pin in the board but is accessed as ADC4. This onboard temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature. If you check the Raspberry Pi Pico datasheet you will find that a temperature of 27 degrees Celsius delivers a voltage of 0.706 volts.
In the RP2040 Pico Board, the ADC pins support 12-bits, which means that the value can go from 0 to 4095. But the MicroPython code can scale the ADC values to a 16-bit range. So we effectively get the range from 0 to 65535. The microcontroller works at 3.3 V, which means that an ADC pin will return a value of 65535 when 3.3 V is applied to it or 0 when there is no voltage. We can obtain all the in-between values when the voltage applied to the pin is between 0 and 3.3 V. Let’s take a look at the code and you will get the idea.
Raspberry Pi Pico Temperature Sensor Programming:
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from oled import Write, GFX, SSD1306_I2C
from oled.fonts import ubuntu_mono_15, ubuntu_mono_20
TempSensor = machine.ADC(4)
WIDTH = 128
HEIGHT = 64
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000)
conversion_factor = 3.3 / 65535
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
data = TempSensor.read_u16() * conversion_factor
temperature = 27-(data-0.706)/0.001721
write20 = Write(oled, ubuntu_mono_20)
write20.text("Temperature: ", 0, 0)
Now, you should be able to understand why am I using ADC4 and why am I dividing 3.3 by 65535. As I explained earlier, if you check the Raspberry Pi Pico datasheet you will find that a temperature of 27 degrees Celsius delivers a voltage of 0.706 volts, so that’s why you can see these numbers and then this value is used to convert the temperature reading into Celsius. So, that’s all about the code and now let’s run this code.
Next article, Raspberry Pi Pico Ultrasonic Sensor