搜索代码

ESP32 Tutorial 25/55 - Measuring Temperature using NTC & LCD | SunFounder's ESP32 IoT Learning kit

ESP32 Tutorial 25/55 - Measuring Temperature using NTC & LCD | SunFounder's ESP32 IoT Learning kit

In this tutorial, we will learn how to measure temperature using a Negative Temperature Coefficient (NTC) thermistor and display the readings on an LCD screen using the ESP32 microcontroller. The project will involve connecting an NTC thermistor to the ESP32 and using an LCD to show temperature readings in both Celsius and Fahrenheit. This is a great way to get started with temperature sensing and display techniques using the ESP32 platform.

ESP32-25-NTC_thermometeLCD

We will be utilizing SunFounder's ESP32 extension board, which enhances the capabilities of the ESP32 with built-in Wi-Fi and Bluetooth. This board can be easily integrated with various sensors and displays, making it ideal for IoT projects. The NTC thermistor will provide temperature readings based on its resistance, which varies inversely with temperature. For further clarification on the setup and code, be sure to check out the video (in video at 00:00).

Hardware Explained

The main components used in this project include the ESP32 microcontroller, an NTC thermistor, a resistor, and an LCD display. The ESP32 serves as the central processing unit, handling data from the thermistor and controlling the LCD display. The NTC thermistor changes its resistance according to temperature, allowing us to calculate the temperature based on the voltage divider configuration with a fixed resistor.

NTC_thermistor

The LCD display will show the temperature readings in both Celsius and Fahrenheit. We will use the I2C protocol to communicate with the LCD, which simplifies wiring by reducing the number of pins needed. The NTC thermistor has a characteristic where its resistance decreases with an increase in temperature, which is crucial for our calculations.

Datasheet Details

Manufacturer SunFounder
Part number NTC Thermistor
Nominal Resistance 10 kΩ
Beta Value 3950 K
Temperature Range -40°C to 125°C
Package Axial

 

  • Ensure the thermistor is rated for the expected temperature range.
  • Use a 10 kΩ resistor for the voltage divider; otherwise, readings will be inaccurate.
  • Verify connections to avoid short circuits.
  • Keep the I2C address of the LCD correct (typically 0x27).
  • Use appropriate power supply levels for the ESP32 and peripherals.
  • Check that the serial monitor baud rate matches the code settings (115200).
NTC_formula

Wiring Instructions

ESP32-11_LCD-wiring
ESP32-25-NTC_thermometer_wiring

To wire the components, start by connecting the NTC thermistor to the ESP32. The thermistor has no polarity, so it can be connected in either direction. Connect one lead of the thermistor to the ground (GND) pin on the ESP32. The other lead connects to a 10 kΩ resistor, which then connects to the 3.3V pin on the ESP32. The junction between the thermistor and the resistor will connect to pin 35 on the ESP32, which will read the voltage for temperature calculations.

For the LCD, connect the ground pin (typically the second pin from the top) to GND on the ESP32. Then, connect the VCC (often the first pin) to the 5V pin on the ESP32. The SDA pin (usually the third pin) should connect to pin 21 on the ESP32, and the SCL pin (often the fourth pin) should connect to pin 22. Ensure all connections are secure and double-check the pin numbers to avoid any mistakes.

Code Examples & Walkthrough

The code begins by defining the constants for the thermistor, including the pin number, reference voltage, and resistance values. The setup function initializes the serial communication and sets the thermistor pin as an input.

const int thermistorPin = 35; // Pin connected to the thermistor
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // the 'other' resistor

The main loop reads the analog value from the thermistor pin, calculates the resistance, and then computes the temperature in both Celsius and Fahrenheit using the Beta parameter equation. The calculated values are printed to the serial monitor.

int adcValue = analogRead(thermistorPin); // Read ADC value
float voltage = (adcValue * referenceVoltage) / 4095.0; // Calculate voltage
float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // Calculate thermistor resistance

Finally, the temperature is displayed on the LCD. The lcd.print() function is used to show the temperature values, along with the degree symbol.

lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(tempC, 1);
lcd.write(223); // Degree symbol
lcd.print("C");

This code effectively updates the LCD every 300 milliseconds with the latest temperature readings, allowing for real-time monitoring.

Demonstration / What to Expect

Upon running the code and completing the wiring, you should see the temperature readings displayed on the LCD in both Celsius and Fahrenheit. If you hold the thermistor in your hand, you should notice the temperature increasing as the resistance decreases. Be cautious of reversed polarity and ensure the connections are secure, as improper wiring can lead to inaccurate readings (in video at 04:50).

Video Timestamps

  • 00:00 Start
  • 1:50 introduction to the project
  • 5:32 wiring explained
  • 8:20 Arduino NTC code for ESP32 explained
  • 13:38 Selecting ESP32 board and COM port in Arduino IDE
  • 15:20 NTC measuring Temperature demonstration
  • 17:34 NTC temperature on LCD1602 with ESP32
  • 18:42 NTC Arduino code with LCD explained
  • 21:15 Demonstration of Temperature on LCD
  • 23:13 Green LCD or Blue LCD1601?

图像

ESP32-11_LCD-wiring
ESP32-11_LCD-wiring
ESP32-11_LCD-wiring-schematic
ESP32-11_LCD-wiring-schematic
NTC_thermistor
NTC_thermistor
ESP32-25-NTC_thermometer_schematic
ESP32-25-NTC_thermometer_schematic
ESP32-25-NTC_thermometer_wiring
ESP32-25-NTC_thermometer_wiring
NTC_formula
NTC_formula
ESP32-25-NTC_thermometeLCD
ESP32-25-NTC_thermometeLCD
824-ESP32 Tutorial 25/55- SunFounder doc page NTC Thermometer
语言: C++
/*
 * // 定义常量
 */
const int thermistorPin = 35; // 连接到热敏电阻的引脚
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // 另一个电阻器
const float beta = 3950; // β值(典型值)
const float nominalTemperature = 25; // 计算温度系数的标称温度
const float nominalResistance = 10000; // 额定温度下的电阻值

void setup() {
  Serial.begin(115200); // 初始化串行通信
  pinMode(thermistorPin, INPUT); // 将引脚设置为输入
}

void loop() {
  int adcValue = analogRead(thermistorPin); // 读取ADC值
  float voltage = (adcValue * referenceVoltage) / 4095.0; // 计算电压
  float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // 计算更新配置下的热敏电阻阻值

 // 使用贝塔参数方程计算温度
  float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));

  float tempC = tempK - 273.15; // 获取摄氏温度
  float tempF = 1.8 * tempC + 32.0; // 获取华氏温度

 // 打印温度
  printf("TempC: %.2f C\n", tempC);
  printf("TempF: %.2f F\n", tempF);

  delay(300); // 等待1秒钟
}

825-ESP32 Tutorial 25/55- Arduino code for LCD with NTC thermistor
语言: C++
/*
 * 这是SunFounder ESP32物联网学习套件代码的第25/55课
 * 在LCD屏幕上显示NTC温度
 * 由Ahmad Shamshiri为SunFounder ESP32物联网学习套件撰写
 * 观看完整视频 https://youtu.be/zJh-gWY0DmE
 * 从 https://robojax.com/RJT707 下载此代码、接线图和其他资源
 */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

 // SDA->21,SCL->22
LiquidCrystal_I2C lcd(0x27,16,2); // 将LCD地址设置为0x27,适用于16字符和2行显示。


 // 定义常量
const int thermistorPin = 35; // 引脚连接到热敏电阻
const float referenceVoltage = 3.3;
const float referenceResistor = 10000; // 电阻值 (10k)
const float beta = 3950; // 贝塔值(典型值)
const float nominalTemperature = 25; // 计算温度系数的名义温度
const float nominalResistance = 10000; // 额定温度下的电阻值

void setup() {

  lcd.clear();
  lcd.init(); // 初始化LCD
  lcd.backlight(); // 打开LCD背光。

 // Serial.begin(115200); // 初始化串口通信
  pinMode(thermistorPin, INPUT); // 将引脚设置为输入
}

void loop() {
  int adcValue = analogRead(thermistorPin); // 读取ADC值
  float voltage = (adcValue * referenceVoltage) / 4095.0; // 计算电压
  float resistance = (voltage * referenceResistor) / (referenceVoltage - voltage); // 使用更新的配置计算热敏电阻电阻值

 // 使用 Beta 参数方程计算温度
  float tempK = 1 / (((log(resistance / nominalResistance)) / beta) + (1 / (nominalTemperature + 273.15)));

  float tempC = tempK - 273.15; // 获取摄氏温度
  float tempF = 1.8 * tempC + 32.0; // 获取华氏温度

 // 打印温度
 // printf("温度: %.2f 摄氏度\n", tempC);
 // printf("温度: %.2f 华氏度\n", tempF);

 // 在LCD上显示温度
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(tempC, 1);
  lcd.write(223); // 度符号
  lcd.print("C");

  lcd.setCursor(0, 1);
  lcd.print("Temp: ");
  lcd.print(tempF, 1);
  lcd.write(223); // 度符号
  lcd.print("F");

  delay(300); // 等待 1 秒钟
}

资源与参考

文件📁

没有可用的文件。