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 -

"LiquidCrystal.h" -

/** Tiago Santos

* free to share
#include "DHT.h"
#include "LiquidCrystal.h"
#define DHTPIN 6
#define DHTTYPE DHT22
#define blue 8
#define green 9
#define red 10
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()
 pinMode(green, OUTPUT);
 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(){
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 if (isnan(h) || isnan(t) ){
  Serial.println("Failed to read from DHT sensor!");
 lcd.setCursor(0, 0);
 lcd.print("Humid.: ");
 lcd.print(" %");
 lcd.setCursor(0, 1);
 lcd.print("Temp. : ");
 lcd.print(" C");
 Serial.print("Humidity: ");
 Serial.print(" %\t");
 Serial.print("Temperature: ");
 Serial.println(" *C ");
 if( t < 20 )
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
  digitalWrite(blue, HIGH);
  if(t > 30)
   digitalWrite(green, LOW);
   digitalWrite(blue, LOW);
   digitalWrite(red, HIGH); 
   digitalWrite(blue, LOW);
   digitalWrite(red, LOW);
   digitalWrite(green, HIGH); 

All the code is avaiable in my GitHub Account: