Lab8_temperature_sensor
Lab8_temperature_sensor
__________________________________
Title: Build in temperature sensor Raspberry Pi Pico
Objective:
The objective of this lab is to learn how to read temperature values from internal temperature
sensor and display them on command prompt and I2C LCD with Raspberry Pi Pico.
Introduction:
The internal temperature sensor that comes with the Raspberry Pi Pico is connected to one of the
ADCs or Analog-to-Digital Converters. The ADC pin supports a range of values, which is determined by
the input voltage applied to the pin.
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.
The ADC pins in the Pico board use their own numbering scheme instead of going by their GPIO pin
number. In the pin diagram above you can see the pins labeled ADC0, ADC1, ADC2,
and ADC_VREF (technically ADC3), which are the four externally accessible ADC pins. The temperature
sensor does not have a physical pin in the board but is accessed as ADC4.
import machine
import utime
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
while True:
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706)/0.001721
print("Temperature: {}".format(temperature))
utime.sleep(2)
Code Explanation
1
import machine
2
import utime
3
sensor_temp = machine.ADC(4)
4
conversion_factor = 3.3 / (65535)
5
We first import machine & utime. The machine module provides the ADC() class to work with ADC pins.
If you print the value of the temperature value you are going to get an integer number between 0 and
65535. So, we have to convert this value either to the Celsius or Fahrenheit degree scales.
The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the
temperature. From the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V.
With each additional degree the voltage reduces by 1.721 mV or 0.001721 V. The first step in converting
the 16-bit temperature is to convert it back to volts, which is done based on the 3.3 V maximum voltage
used by the Pico board.
With this conversion, the temperature value holds a value between 0 and 3.3. We again have to do the
second conversion which brings the temperature to the Celsius scale.
1 fahrenheit_degrees = celsius_degrees * 9 / 5 + 32
If you want to convert the temperature value from degree Celcius to Fahrenheit, you can use this
mathematical equation.
Displaying measured temperature on LCD is really straightforward. Simply pass the temperature
variable calculated above to LCD.
import machine
import utime
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
totalRows = 2
totalColumns = 16
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
I2C_ADDR = i2c.scan()[0]
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
while True:
reading = sensor_temp.read_u16() * conversion_factor
temperature = 27 - (reading - 0.706)/0.001721
print("Temperature: {}".format(temperature))
utime.sleep(2)
lcd.putstr(str(temperature))
utime.sleep(1)
lcd.clear()
Task list:
Task1: Get an input from user on thonny shell for asking if they want temperature in degree Celcius
or in Fahrenheit, display the temperature accordingly.
Conclusion:
In this lab, we learned the how to get temperature from build in temperature sensor with Raspberry
Pi Pico .