ผู้เขียน หัวข้อ: งานครั้งที่ 15 เขียนโปรแกรมแสดงอุณหภูมิที่ตรวจจับได้จาก NTC  (อ่าน 4591 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมแสดงอุณหภูมิที่ตรวจจับได้จาก NTC
ฟังก์ชั่นอยู่ในกลุ่ม Serial http://arduino.cc/en/Reference/Serial
-Serial.begin() http://arduino.cc/en/Serial/Begin
-if (Serial) http://arduino.cc/en/Serial/IfSerial
-Serial.available() http://arduino.cc/en/Serial/Available
-Serial.read() http://arduino.cc/en/Serial/Read
-Serial.print() http://arduino.cc/en/Serial/Print
-Serial.println() http://arduino.cc/en/Serial/Println
-Serial.flush() http://arduino.cc/en/Serial/Flush

ฟังก์ชั่นทั่วไป
-analogRead(A0)
-pinMode() http://arduino.cc/en/Reference/PinMode
-digitalWrite() http://arduino.cc/en/Reference/DigitalWrite
-delay() http://arduino.cc/en/reference/delay

Analog input
ESP8266 has a single ADC channel available to users. It may be used either to read voltage at ADC pin, or to read module supply voltage (VCC).
To read external voltage applied to ADC pin, use analogRead(A0). Input voltage range is 0 — 1.0V.
reference http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html

ค่าแอนาลอกที่อ่านได้


พิเศษสำหรับ NodeMCU จะมีวงจรแบ่งแรงดันมาให้แล้วซึ่งทำให้สามารถอ่านค่าอนาลอกที่มีย่านของค่าแรงดันระหว่างกราวด์กับไฟเลี้ยงได้ ( 0-3.2V เนื่องจากค่าตัวต้านทานที่ใช้เป็น 100k กับ 220k)



โจทย์โปรแกรม
-เขียนโปรแกรมวัดอุณหภูมิด้วย NTC แสดงข้อความผ่านทาง UART
-โปรแกรมรายละเอียดพิเศษรายกลุ่ม (แจ้งให้ทราบเมื่อถึงชั่วโมงเรียน)

วงจรที่ใช้ทดลอง


ตัวอย่างโปรแกรม
โค๊ด: [Select]
#include <math.h>
double Thermistor(int RawADC)
{
  double Cal;
  Cal = log(10000.0/((1024.0/RawADC-1)));
  Cal = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Cal * Cal ))* Cal );
  Cal = Cal - 273.15;            /* Convert Kelvin to Celcius */
  return Cal;
}
void setup()
{
  Serial.begin(115200);
}
void loop()
{
  float Temp=Thermistor(analogRead(A0));
  Serial.println(Temp);
  delay(1000);
}
« แก้ไขครั้งสุดท้าย: พฤศจิกายน 10, 2017, 10:36:00 AM โดย admin »