ผู้เขียน หัวข้อ: งานครั้งที่ 35 เขียนโปรแกรมแสดงค่าอุณหภูมิจากเซนเซอร์แอนาลอกแสดงผลที่ LCD 16x2  (อ่าน 5857 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมแสดงค่าอุณหภูมิจากเซนเซอร์แอนาลอกแสดงผลที่ LCD 16x2
ฟังก์ชั่นที่ใช้ในกลุ่ม LCD http://arduino.cc/en/Tutorial/LiquidCrystal
-lcd.begin()http://arduino.cc/en/Reference/LiquidCrystalBegin
-lcd.print() http://arduino.cc/en/Reference/LiquidCrystalPrint
-lcd.setCursor() http://arduino.cc/en/Reference/LiquidCrystalSetCursor
-Liquid Crystal Library (ฟังก์ชั่นอื่น ๆ ที่มีให้ใช้งาน) http://arduino.cc/en/Reference/LiquidCrystal
*ไลบารี่ LiquidCrystal.h มีมาให้แล้วพร้อมโปรแกรม Arduino IDE ไม่ต้องติดตั้งเพิ่ม

ฟังก์ชั่นทั่วไป
-delay() http://arduino.cc/en/Reference/Delay
-delayMicroseconds() http://arduino.cc/en/Reference/DelayMicroseconds
-analogRead() http://arduino.cc/en/Reference/AnalogRead
-map() http://arduino.cc/en/Reference/Map

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

วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้บอร์ด Arduino


วงจรที่ใช้ในการทดลองสำหรับผู้ที่ใช้ไอซี ATmega328 (ที่มี Boot Loader Arduino)


บริเวณใช้งานบอร์ดทดลอง


โปรแกรมตัวอย่างการแสดงผลจอ LCD
โค๊ด: [Select]
#include <LiquidCrystal.h>
/* The circuit:
 * LCD RS pin to digital pin 7
 * LCD E  pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
*/
LiquidCrystal lcd(7,6,5,4,3,2);       // set up the LCD's connection pins
void setup()
{
  lcd.begin(16, 2);       // set up the LCD's number of columns and rows:
  lcd.print("hello, world!");
}
void loop() {}

ตัวอย่างโปรแกรมแสดงอุณภูมิเบื้องต้น
โค๊ด: [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(9600);
}

void loop()
{
  int Temp=Thermistor(analogRead(A3));
  Serial.println(Temp);
  delay(1000);
}
« แก้ไขครั้งสุดท้าย: มิถุนายน 27, 2017, 09:23:22 PM โดย admin »