Go Down

Topic: Using a DHT22 temp/humidity sensor with a 2X16 Cytron LCD (Read 2103 times) previous topic - next topic

Ardinaut

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. I'm new to c++ also. Thanks everyone!

Things that helped tremendously... this forum (#1), using the dht.h include library (available at the folowing link - https://github.com/adafruit/DHT-sensor-library. The other library used came with the documentation for the Cytron display. The stdlib.h library was necessary to do the float conversion part.

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.

Additional Note: I am not sure what the resolution of the DHT22 is. When I read the output, it does not come across as 92.30 then 92.31, 92.32 etc, so I am thinking about making the float round to the nearest tenth and drop the last digit. It does the same for both temperature and humidity. I don't have a requirement for rigid accuracy to the 100th of a degree, so that should be fine. I might also make the display show both Farenheit and Celcius. More code to follow if I have the time.

Code: [Select]
// 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);
}



Go Up