Temperature and Humidity Sensor With LCD and Arduino

Introduction: Temperature and Humidity Sensor With LCD and Arduino

About: https://about.me/tiago.santos

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
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:




    • Fix It! Contest

      Fix It! Contest
    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    7 Discussions


    6 months ago

    can u help me with this sir. i dunno what the problem with my arduino.


    6 months ago

    hi!can u send show the pin connection for every compenent?

    1 reply

    Hello. You can see a bigger diagram on my page at: https://space.groundzero.com.pt/sensor-de-temperatura-com-lcd/


    1 year ago

    Hi! What version od arduino IDE did you use? your code doesnt seem to work,there a lot of

    exit status 1
    expected unqualified-id before '<' token

    since I have never programmed before in c++ (thats what arduino uses right?) the only experience I have is in computercraft in minecraft,and its very diffrent,anyways,in your code there are a lot of faults,and I dont know what wrong,the libraries? or the version,I want to use an arduino leonardo pro mini clone,but the problem is in the code anyways....do you have any fix for this? I searched the internet but they say its something that you didnt close or something like that but there dont seem to be any faults in my view......

    Hope you understand me,I comment in a nice and informative way,I dont want to put hate on you.

    3 replies

    ohh nevermind,I download your whole sketch from github and it doesnt have any faults,waht went wrong there? At first I just copy pasted the code,no problem then,I have one question though,you say you used an arduino nano,but in the pictures and in the schematic you seem to use an arduino leonardo pro micro?

    The arduino is an Arduino Nano. I've added the links to the material used.

    The code on instructables has some wrong tags. When i edited, some html was added and i don't notice. It's repaired.