#include <SPI.h>
#include <Adafruit_GFX.h>
#include "Adafruit_ILI9341.h"
// กำหนดค่า Analog Pin สำหรับ NTC Sensor
const int NTC_PIN = A0;
// กำหนดค่า SPI Pins สำหรับ Adafruit ILI9341
#define TFT_CS 3
#define TFT_DC 2
// กำหนดค่า ILI9341
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
// เริ่มต้น Serial Communication
Serial.begin(9600);
// เริ่มต้น SPI Communication
SPI.begin();
// เริ่มต้น Adafruit ILI9341
tft.begin();
tft.setRotation(3);
// ตั้งค่าพื้นหลัง
tft.fillScreen(ILI9341_BLACK);
// แสดงข้อความ
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("อุณหภูมิ (F): ");
}
void loop() {
// อ่านค่า Analog จาก NTC Sensor
int analogValue = analogRead(NTC_PIN);
// แปลงค่า Analog เป็นอุณหภูมิ (Celsius)
float celsius = getCelsius(analogValue);
// แปลงค่า Celsius เป็น Fahrenheit
float fahrenheit = getFahrenheit(celsius);
// แสดงค่าอุณหภูมิบน Serial Monitor
Serial.print("อุณหภูมิ (C): ");
Serial.print(celsius);
Serial.print(" / อุณหภูมิ (F): ");
Serial.println(fahrenheit);
// แสดงค่าอุณหภูมิบน Adafruit ILI9341
tft.setCursor(120, 0);
tft.print(fahrenheit);
// รอ 1 วินาที
delay(1000);
}
// ฟังก์ชันสำหรับแปลงค่า Analog เป็นอุณหภูมิ (Celsius)
float getCelsius(int analogValue) {
// ค่า R1 และ R2 ของวงจร NTC
const float R1 = 10000.0;
const float R2 = 4700.0;
// คำนวณค่าความต้านทาน NTC
float ntcResistance = (R1 * analogValue) / (1023.0 - analogValue);
// ค่า B ของ NTC Sensor
const float B = 3950.0;
// คำนวณอุณหภูมิ (Celsius)
float celsius = 1.0 / (B * log(ntcResistance / R2) + 1.0 / 298.15) - 273.15;
return celsius;
}
// ฟังก์ชันสำหรับแปลงค่า Celsius เป็น Fahrenheit
float getFahrenheit(float celsius) {
return (celsius * 9.0 / 5.0) + 32.0;
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6