วงจรวัดอุณหภูมิและความชื้น
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Uncomment whatever type you're using!
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
// I2C Address of the LCD
const int LCD_ADDR = 0x27;
// LCD dimensions
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
// Initialize LCD
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLS, LCD_ROWS);
void setup() {
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
dht.begin();
// Initialize LCD
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.backlight();
}
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(F("Failed to read from DHT sensor!"));
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
// Clear the LCD screen
lcd.clear();
// Display Humidity
lcd.setCursor(0, 0);
lcd.print(F("Humidity: "));
lcd.print(h);
lcd.print(F("%"));
// Display Temperature
lcd.setCursor(0, 1);
lcd.print(F("Temp: "));
lcd.print(t);
lcd.print(F("C / "));
lcd.print(f);
lcd.print(F("F"));
delay(5000); // Display for 5 seconds
}
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN 2 // Pin where DHT11 is connected
#define DHTTYPE DHT11 // DHT11 sensor
DHT dht (DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd (0x3F, 20, 4); // Set the LCD address to 0x3F for a 1602 display
void setup() {
lcd.begin(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
dht.begin(); // Initialize the DHT sensor
Serial.begin(9600); // Initialize serial communication
}
void loop() {
float temperature = dht.readTemperature();// Read temperature from DHT sensor
float humidity = dht.readHumidity(); // Read humidity from DHT sensor
lcd.setCursor(0,0); // Set cursor to first line
lcd.print ("Hi,Wittayakorn"); // Print message to LCD
lcd.setCursor(0,1); // Set cursor to second line
lcd.print ("T: "); // Print label for temperature
lcd.print (temperature); // Print temperature value
lcd.print (" C H: "); // Print label for humidity
lcd.print (humidity); // Print humidity value
lcd.print ("%");// Print unit for humidity
delay(2000); // Wait 2 seconds before reading sensor again
}