Robojax

Lesson 27: Using DHT11 Temperature Sensor with LCD Screen | Arduino Step by Step Course

Related or required files and link to this lesson

Part 3: LCD, LED and OLED Screens

In this lecture we learn how to use DHT11 to measure the temperature and display it on LCD1602 or LCD2004 LCD screen.


 
/*
 * Robojax Arduino Step By Step Course
 * Lesson 26 DHT11 with LCD1602-I2C display
 

 * This is the Arduino code for  DHT11 module to read temprature and humidity
 * This code can display temprature in:
 * C is used to get Celsius
 * F is used to get fahrenheit
 * K is used for Kelvin

 *  * 
 * Written by Ahmad Shamshiri for Robojax
 * on Jan 04, 2018
 * Robojax.com

   Please watch video instruction here https://youtu.be/0XMLnCYkvbE
 This code is available at http://robojax.com/course1/?vid=lecture11
 
with over 100 lectures Free On  YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339 

  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

// header file from GetHub: https://github.com/adidax/dht11
#include <dht11.h>

dht11 DHT11; // create object of DHT11
#define dhtpin 2 // set the pin to connect to DHT11

// start of settings for LCD1602-I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x3F for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// end of settings for LCD1602-I2C


void setup() {
    Serial.begin(9600);// setting up serial monitor

  // initialize the LCD, 
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.clear();
  lcd.setCursor (0,0); //
  lcd.print("DHT11 Sensor"); 
  delay(2000);
}

void loop() {
	//Robojax Step By Step Arduono Course YouTube Watch it here http://robojax.com/L/?id=338
  DHT11.read(dhtpin);// initialize the reading
  int humidity = DHT11.humidity;// get humidity

  lcd.clear();// clearn previous values from screen
  lcd.setCursor (0,0); //character zero, line 1
  lcd.print("LCD1602 DHT11"); // print text 

  // print distance in degrees C
  lcd.setCursor (0,1); //character 0, line 2
  lcd.print(getTemp('C'));// print Temperature Celsius
  lcd.setCursor (4,1); //character 5, line 2 
  lcd.print((char)223);// print degree 
  lcd.setCursor (5,1); //character 6, line 2 
  lcd.print("C");// print C
  
   // print distance degrees F
  lcd.setCursor (7,1); //character 8, line 2
  lcd.print(getTemp('F'));// print distance in cm
  lcd.setCursor (13,1); //character 14, line 2 
  lcd.print((char)223);// print degree 
  lcd.setCursor (14,1); //character 16, line 2 
  lcd.print("F");// print F 
  
  
  //code for Robojax.com video
   Serial.print(getTemp('C'));
   Serial.print("C ");
   Serial.print(getTemp('F'));
   Serial.print("F ");
   Serial.print(getTemp('K'));
   Serial.print("K ");
   Serial.print(" humidity:");
   Serial.print (humidity);
   Serial.print("% ");
   Serial.println();
 delay(500);
}


/*
 * getTemp(char type)
 * type character of upper case 
 * C is used to get Celsius
 * F is used to get fahrenheit
 * K is used for Kelvin
 */
float getTemp(char type) {
	//Robojax Step By Step Arduono Course YouTube Watch it here http://robojax.com/L/?id=338	
    float temp = (float)DHT11.temperature;//get temp
  if(type =='F')
  {
    return temp * 1.8 + 32;// convert to fahrenheit
  }else if(type =='K')
  {
    return temp + 274.15;// convert to Kelvin
  }else{
   return temp; 
  }
  
}