Skip to content

Instantly share code, notes, and snippets.

@9arduino
Last active April 23, 2025 13:57
Show Gist options
  • Save 9arduino/97b4c47d5d678707e5ad826d48fdb40e to your computer and use it in GitHub Desktop.
Save 9arduino/97b4c47d5d678707e5ad826d48fdb40e to your computer and use it in GitHub Desktop.
ตัวอย่าง การใช้งาน Sensor DHT22 https://www.ab.in.th/b/22
/*
ตัวอย่าง การใช้งาน Sensor DHT22 สินค้า
บทความ : https://www.ab.in.th/b/22
Download Library LCD I2C : https://download.เอบี.ไทย/d.php?file=Arduino-LiquidCrystal-I2C-library-master.zip
Download Library DHT22 : https://download.เอบี.ไทย/d.php?file=DHT-sensor-library-master.zip
*/
#include "DHT.h"
#define DHTPIN 2 // 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();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment