Skip to content

Instantly share code, notes, and snippets.

@zoracon
Last active February 23, 2021 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoracon/b5548a1f69e7d022038a95b32b04fe8d to your computer and use it in GitHub Desktop.
Save zoracon/b5548a1f69e7d022038a95b32b04fe8d to your computer and use it in GitHub Desktop.
DHT22 HUM & TMP + LCD for Arduino Uno

DHT22 TMP and Humidifier Sensor + LCD Display

Alternative to IoT based devices for nursery

#include <LiquidCrystal.h>

// Create an LCD object. Parameters: (RS, E, D4, D5, D6, D7):
//* Arduino example code for DHT11, DHT22/AM2302 and DHT21/AM2301 temperature and humidity sensors. More info: www.makerguides.com */
LiquidCrystal lcd = LiquidCrystal(3, 4, 5, 6, 7, 8);

#include <DHT.h>

// Set DHT pin:
#define DHTPIN 2

// Set DHT type, uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor for normal 16mhz Arduino:
DHT dht = DHT(DHTPIN, DHTTYPE);

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);

  // Setup sensor:
  dht.begin();

   // Specify the LCD's number of columns and rows. Change to (20, 4) for a 20x4 LCD:
  lcd.begin(16, 2);
}

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

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

  // Read the humidity in %:
  float h = dht.readHumidity();
  // Read the temperature as Fahrenheit:
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again):
  if (isnan(h) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  } else {
    lcd.setCursor(2, 0);
    lcd.print("H: ");
    lcd.print(h);
    lcd.print(" % ");
    lcd.setCursor(2, 1);
    lcd.print("T: ");
    lcd.print(f);
    lcd.print(" F ");
  }

  // Compute heat index in Fahrenheit (default):
  //float hif = dht.computeHeatIndex(f, h);

  delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment