Measure the Temperature with using thermometer sensor

  1. Introduction:

This project involves using a thermistor to measure temperature and display the values in degrees Celsius on a Raspberry Pi Pico. The project will be implemented in both MicroPython and Embedded C. The Raspberry Pi Pico will read the thermistor's resistance, convert it into a temperature reading, and display it.

  1. Scope:

The scope of this project includes:

  • Designing a simple temperature measurement system using a thermistor.

  • Implementing the temperature calculation using both MicroPython and Embedded C.

  • Displaying the temperature values in degrees Celsius.

  • Providing a clear explanation, connection details, and demonstration of the working system.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Breadboard

  • NTC Thermister

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.



  1. Connection Details:

  • Thermistor: One end connected to 3.3V (VCC) on the Pico, the other end connected to the ADC pin (GPIO 28).

  • 10k Ohm Resistor: Connected between the ADC pin (GPIO 28) and Ground.





  1. Working Principle:

T A thermistor is a type of resistor whose resistance changes significantly with temperature. The word "thermistor" is a portmanteau of "thermal" and "resistor." Thermistors are widely used as temperature sensors, and they come in two main types:

  1. NTC Thermistors (Negative Temperature Coefficient):

    • In NTC thermistors, the resistance decreases as the temperature increases.

    • They are commonly used in temperature sensing, temperature compensation, and overcurrent protection applications.

    • NTC thermistors are more common than PTC thermistors for general temperature sensing.

  2. PTC Thermistors (Positive Temperature Coefficient):

    • In PTC thermistors, the resistance increases as the temperature increases.

    • They are often used in overcurrent protection and self-regulating heating elements.

How Thermistors Work

Thermistors rely on the property that the electrical resistance of certain materials changes with temperature. For NTC thermistors, the relationship between temperature and resistance is often exponential, meaning that small changes in temperature can result in large changes in resistance.

Thermistor Characteristics

  1. Resistance at 25°C (R25):

    • This is the resistance of the thermistor at room temperature, typically 25°C. For example, a common thermistor might have a resistance of 10k ohms at 25°C.

  2. Beta Value (β or B):

    • The Beta value is a constant that describes the relationship between the resistance of the thermistor and temperature. It is used in the Steinhart-Hart equation or a simplified formula to calculate the temperature from the resistance. 

Steinhart-Hart equation

There is a non-linear relationship between resistance and excitement. To find the temperature we can use the Steinhart-Hart equation:

where A = 0.001129148, B = 0.000234125 and C = 8.76741E−08.

Tk is the Temperature in degrees Kelvin and R is the resistance.

Application code:

  • MicroPython:

import machine

import utime

import math

# Initialize the ADC for the thermistor (Assumed to be connected to GPIO 28)

thermistor = machine.ADC(28)

# Constants

BETA = 3950 # Beta value of the thermistor

R_NOMINAL = 10000 # Nominal resistance at 25°C (10k Ohm)

TEMP_NOMINAL = 25 # Nominal temperature (25°C)

VCC = 3.3 # Reference voltage


while True:

# Read the raw ADC value (0-65535)

raw_value = thermistor.read_u16()


# Calculate the voltage across the thermistor

V_out = VCC * raw_value / 65535


# Calculate the resistance of the thermistor

R_t = R_NOMINAL * (V_out / (VCC - V_out))


# Calculate the temperature in Kelvin using the Steinhart-Hart equation

temp_kelvin = 1 / ((math.log(R_t / R_NOMINAL) / BETA) + (1 / (TEMP_NOMINAL + 273.15)))

# Convert Kelvin to Celsius

temp_celsius = temp_kelvin - 273.15

# Print the temperature values

print('Celsius: %.2f C' % (temp_celsius))


# Wait for 200 milliseconds before the next reading

utime.sleep_ms(200)



  • Embedded C:

#include "pico/stdlib.h"

#include <math.h>


// Constants

#define BETA 3950 // Beta value of the thermistor

#define R_NOMINAL 10000 // Nominal resistance at 25°C (10k Ohm)

#define TEMP_NOMINAL 25 // Nominal temperature (25°C)

#define VCC 3.3 // Reference voltage


int main() {

stdio_init_all();


// Initialize ADC on GPIO 28

adc_init();

adc_gpio_init(28);

adc_select_input(2); // GPIO 28 corresponds to ADC input 2


while (1) {

// Read raw ADC value (0-4095)

uint16_t raw_value = adc_read();


// Calculate voltage across the thermistor

float V_out = VCC * raw_value / 4095.0;


// Calculate the resistance of the thermistor

float R_t = R_NOMINAL * (V_out / (VCC - V_out));


// Calculate the temperature in Kelvin using the Steinhart-Hart equation

float temp_kelvin = 1 / ((log(R_t / R_NOMINAL) / BETA) + (1 / (TEMP_NOMINAL + 273.15)));


// Convert Kelvin to Celsius

float temp_celsius = temp_kelvin - 273.15;


// Print the temperature in Celsius

printf("Temperature: %.2f C\n", temp_celsius);


// Delay before the next reading

sleep_ms(200);

}

}




  1. Hardware Image:



  1. Output:

  • MicroPython Output: Temperature readings in degrees Celsius will be displayed in the Thonny IDE's REPL.

  • Embedded C Output: Temperature readings in degrees Celsius will be displayed on the terminal connected to the Pico.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

# Set minimum CMake version

cmake_minimum_required(VERSION 3.13)


# Set the project name and version

project(Thermistor_Temperature_Project C CXX ASM)


# Specify the target Pico SDK version

set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


# Initialize the Pico SDK

# PICO_SDK_PATH environment variable should point to the pico-sdk directory

include(pico_sdk_import.cmake)


# Initialize the Pico SDK

pico_sdk_init()


# Add the executable for your project

add_executable(thermistor_temperature

main.c # Replace with your source file if named differently

)


# Link the required libraries

target_link_libraries(thermistor_temperature

pico_stdlib # Standard Pico SDK library

hardware_adc # Library for ADC usage

)


# Enable USB serial output

pico_enable_stdio_usb(thermistor_temperature 1)


# Enable UART serial output (optional, if you want to use UART)

pico_enable_stdio_uart(thermistor_temperature 0)


# Create map/bin/hex/uf2 files

pico_add_extra_outputs(thermistor_temperature)

  1. References:



These references provide comprehensive information

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)