ผู้เขียน หัวข้อ: งานครั้งที่ 49 เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผลที่ LCD16x2  (อ่าน 5008 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 706
    • ดูรายละเอียด
    • อีเมล์
เขียนโปรแกรมอุณหภูมิและความชื้นด้วย DHT22 แสดงผลที่ LCD16x2

ไลบารี่เพิ่มเติม
-DHT.h (ไลบรารี่ตัวเดียวกันกับงานครั้งที่ 48 หากติดตั้งแล้วไม่ต้องทำซ้ำ)*ดูวิธีติดตั้งในงานครั้งที่ 48
*ไลบารี่ LiquidCrystal.h มีมาให้แล้วพร้อมโปรแกรม Arduino IDE ไม่ต้องติดตั้งเพิ่ม

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

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


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


ตัวอย่างโปรแกรมอุณหภูมิและความชื้นด้วย DHT22
โค๊ด: [Select]
#include "DHT.h"
#define DHTPIN 13     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  dht.begin();
}
void loop() {
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();      // Read temperature as Celsius (the default)
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t))
 {
     Serial.println("Failed to read from DHT sensor!");
     return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
}

ตัวอย่างโปรแกรมแสดงผล LCD16x2
โค๊ด: [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() {}
« แก้ไขครั้งสุดท้าย: มิถุนายน 28, 2017, 09:38:45 AM โดย admin »