Untitled

 avatar
unknown
plain_text
a month ago
1.8 kB
13
Indexable
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Pin definitions
#define DHTPIN 2     
#define DHTTYPE DHT11  

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// Initialize I2C LCD with address 0x27 (common address for 16x2 I2C LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();

  // Initialize the LCD
  lcd.init();
  lcd.backlight();
  lcd.print("Initializing...");
}

void loop() {
  // Wait a few seconds between measurements
  delay(2000);

  // Reading temperature and humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();      // Celsius
  float f = dht.readTemperature(true);  // Fahrenheit

  // Check if any reads failed and exit early (to try again)
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    lcd.clear();
    lcd.print("Sensor Error!");
    return;
  }

  // Compute heat index in Celsius and Fahrenheit
  float hic = dht.computeHeatIndex(t, h, false);
  float hif = dht.computeHeatIndex(f, h);

  // Print to Serial Monitor
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  // Display temperature and humidity on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);  // Set cursor to first line
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print(" C");

  lcd.setCursor(0, 1);  // Set cursor to second line
  lcd.print("Humidity: ");
  lcd.print(h);
  lcd.print(" %");
}
Editor is loading...
Leave a Comment