DHT22 Temp/Humidity sensor with Cytron LCD how to

All,

What it does - reads the temp and humidity from the DHT22 sensor and displays on the LCD (temp is in F). If connection or power is lost, the display shows an error message. It also outputs to the Serial port so you can monitor progress or debug the LCD as necessary.

This is my first project with the Arduino and am having a blast after 20+ years away from the electronics industry. After working on this for a couple of days, I thought it would be helpful to post the code I put together to make the following combination of parts work.

Parts I spent some frustration with - converting the 'float' output from the sensor to a 'character' input for the LCD. Finally, I searched this forum and found the necessary code. Thanks everyone!

Hardware used
Arduino Mega 2560
Cytron 2 X 16 LCD display
DHT22 temp/humidity sensor
Breadboard power supply

Sensor output is plugged into port 53 of the mega board.

// Include statements

#include <LCD4Bit_mod.h> 
#include "DHT.h"
#include <stdlib.h>

// Hardware setup

// Sensor setup
#define DHTPIN 53 // Sensor digital input
#define DHTTYPE DHT22 // Sensor type
DHT dht(DHTPIN, DHTTYPE);

// LCD setup
LCD4Bit_mod lcd = LCD4Bit_mod(2); 


// Declare variables for the Float - Char string conversion
  char temp_str[10];
  char rh_str[10];


void setup() { 
  pinMode(13, OUTPUT);  //we'll use the debug LED to output a heartbeat
  dht.begin();
  Serial.begin(9600);   
  Serial.println("DHTxx test!");
  
  lcd.init();
  lcd.clear();
  lcd.printIn("Ardinaut");
  lcd.cursorTo(2, 0);
  lcd.printIn("Temp/Humidity");  
  delay(3000);  
  lcd.clear();
  lcd.cursorTo(1, 0);
  lcd.printIn("Initializing...");
}

void loop() {
  lcd.cursorTo(2, 0);                      
  float humidity = dht.readHumidity();  	 // get the values from the DHT22
  float temp = dht.readTemperature();
  float convtemp = (1.8*temp)+32;                // convert it to Farenheit!
  dtostrf(convtemp, 6, 2, temp_str);
  dtostrf(humidity, 6, 2, rh_str);
  
  if (isnan(temp) || isnan(humidity)) 
  {    
    Serial.println("Failed to read from DHT");
    lcd.clear();
    lcd.cursorTo(1,0);
    lcd.printIn("Sensor Fail");
    lcd.cursorTo(2,0);
    lcd.printIn("Check PWR & link");
  } 
  else 
  {
    Serial.print("Humidity: "); 
    Serial.print(humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print((1.8*temp)+32);
    Serial.println(" *F");
    lcd.cursorTo(1,0);
    lcd.clear();
    lcd.printIn("Temp=");
    lcd.cursorTo(1,5);
    lcd.printIn(temp_str);
    lcd.cursorTo(2,0);
    lcd.printIn("Humidity=");
    lcd.cursorTo(2,9);
    lcd.printIn(rh_str);  
  }

  delay(2000);
  digitalWrite(13, LOW);
}

Moderator edit: [code] [/code] tags added.

Well done!

Still a few remarks:

  • The right section for your post is - Exhibition / Gallery - there people can show their tricks (done)
  • Please modify your post, select the code part and press the #button , that makes your code look better. (done)
  • maybe a link to the libraries used makes sense.

Rob

Hi, Here's another similar version:
http://arduino-info.wikispaces.com/PROJECT-Temp-Humidity-Display

This works because Rob figured out the DHT protocols!