Introduction: Temperature and Humidity Sensor With LCD and Arduino

A few months ago i presented a workshop on Arduino and decided to build a simple weather station. The station measured the environment temperature and humidity, showing the data on an LCD and activating a visual indicator. This indicator consisted of three LEDs : one blue, one green and one red.

If the temperature drops below 20°C the blue LED turn on; if it was between 20ºC and 30ºC turn on the green and above 30ºC turn on the red LED.

Step 1: Components

Step 2: Wiring Diagram

Step 3: Code Header Section

To use the DHT22 and the LCD we need the libraries "DHT.h" and "LiquidCrystal.h". In "LiquidCrystal.h" it is necessary to define the Arduino pins to the LCD will use .

In "DHT.h" when initialized , you must choose what type of sensor we use . The library supports multiple sensors.

You can download the "DHT.h" and the "LiquidCrystal.h" from here:

DHT.h - https://github.com/adafruit/DHT-sensor-library

"LiquidCrystal.h" - https://github.com/adafruit/Adafruit_LiquidCrystal

/** Tiago Santos
* dark_storm@groundzero.com.pt
*  http://space.groundzero.com.pt

* free to share
*/
#include "DHT.h"
#include "LiquidCrystal.h"
#define DHTPIN 6
#define DHTTYPE DHT22
#define blue 8
#define green 9
#define red 10
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Step 4: Code Setup Section

Then we started the setup, where we define the pins whre the LEDs are connected and that are OUTPUT, started the dht22 and set that the LCD has 16 columns by 2 lines.

void setup()
{
 Serial.begin(9600);
 pinMode(blue,OUTPUT);
 pinMode(green, OUTPUT);
 pinMode(red,OUTPUT);
 
 dht.begin();
 lcd.begin(16, 2);
}

Step 5: Code Loop Section

In the loop section, we read the humidity and the temperature and present them on the LCD. Humidity in line 0 and the temperature in line 1. Next, define what LED turn on. If the temperature is below 20°C we turn on blue. Between 20ºC and 30ºC turn on the green. Above 30 ° C we turn on red.

void loop(){
 delay(2000);
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 if (isnan(h) || isnan(t) ){
  Serial.println("Failed to read from DHT sensor!");
  return;
 }
 lcd.setCursor(0, 0);
 lcd.print("Humid.: ");
 lcd.print(h);
 lcd.print(" %");
 lcd.setCursor(0, 1);
 lcd.print("Temp. : ");
 lcd.print(t);
 lcd.print(" C");
 Serial.print("Humidity: ");
 Serial.print(h);
 Serial.print(" %\t");
 Serial.print("Temperature: ");
 Serial.print(t);
 Serial.println(" *C ");
 if( t < 20 )
 {
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
  digitalWrite(blue, HIGH);
 }
 else
 {
  if(t > 30)
  {
   digitalWrite(green, LOW);
   digitalWrite(blue, LOW);
   digitalWrite(red, HIGH); 
  }
  else
  {
   digitalWrite(blue, LOW);
   digitalWrite(red, LOW);
   digitalWrite(green, HIGH); 
  }
 }
}

All the code is avaiable in my GitHub Account:

https://github.com/d4rks70rm/Arduino-DHT22-Sensor-with-LCD