Temperature and Humidity Sensor With LCD and Arduino

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

Be the First to Share

    Recommendations

    • Make It Bridge

      Make It Bridge
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Big and Small Contest

      Big and Small Contest

    8 Comments

    0
    Niphiz
    Niphiz

    6 years 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.

    0
    TiagoSantos
    TiagoSantos

    Reply 6 years ago

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

    0
    siddhantw
    siddhantw

    Reply 4 years ago

    no the code is not right yet

    0
    Niphiz
    Niphiz

    Reply 6 years ago

    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?

    0
    TiagoSantos
    TiagoSantos

    Reply 6 years ago

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

    1
    ZukZ
    ZukZ

    5 years ago

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

    Capture.PNG
    0
    ZukZ
    ZukZ

    5 years ago

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

    0
    TiagoSantos
    TiagoSantos

    Reply 5 years ago

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