Table of Contents

IB2: Presenting temperature and humidity values on the LCD

Target group

This hands-on lab guide is intended for the Beginners but other target groups may benefit from it, treating it as basics for advanced projects.

Prerequisites

Liquid Crystal

For this library, you may refer to the B1 exercise.

DHT sensors

The nodes of SmartME Network Laboratory are equipped with different versions of DHT sensors: DHT11 and DHT22. The sensors look a bit similar and have the same pinout, but have different characteristics. Below you can find the DHT technical specifications:

DHT11
DHT22

DHT sensors are able to detect temperature and relative humidity. Relative humidity consists of the amount of water vapour in air vs. the saturation point of water vapour in the air. At the saturation point, water vapour starts to condense and accumulate on surfaces forming dew. The saturation point changes with air temperature. Cold air can hold less water vapour before it becomes saturated, and hot air can hold more water vapour before it becomes saturated.

Temperature and relative humidity can be used to determine the apparent temperature or the human perceived equivalent temperature, also called “heat index”. The heat index was developed in 1978 by George Winterling and was adopted next year. It is also known as humiture, according to Wikipedia contributors.

Scenario

First, initialize LCD screen with the labels of temperature and relative humidity. Then, after the sensor detection, next to the labels, the sensor values will be displayed, one per line, every 3 seconds. Note, the function readTemperature() reads temperature as Celsius (the default). If you want to read the temperature as Farenight, you have to set the true value as a parameter of the signature in the readTemperature(true) function. The relative humidity is expressed as a percentage. That means that at 100% RH the condensation occurs, while at 0% RH the air is completely dry. More, but not used in the proposed exercise, it is possible to compute the heat index in Fahrenheit (the default) by using the function computeHeatIndex(temp_value_farenight, humidity_value), or if you want in Celsius (isFahreheit = false) by using the function computeHeatIndex(temp_value_celsius, humidity_value, false). Result You should see the values of temperature and relative humidity, that is sampled and displayed every 3 seconds (3000 ms). Start There are no special steps to be performed.

Steps

Step 1

Include LCD driver library and DHT library:

#include <LiquidCrystal.h>
#include "DHT.h"
Step 2

Instantiate the software controller component for the LCD display. Then set up:

  1. the DHTPIN (which refers to the digital pin we use to get the signal);
  2. the DHT sensor type in use (DHT11 or DHT22, it depends on the node of the lab you are using).

Then initialize the DHT sensor.

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 13, 2);
#define DHTPIN 8 // what pin we are connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11
//#define DHTTYPE DHT22
// Initialize DHT sensor 
DHT dht(DHTPIN, DHTTYPE);
Step 3

Initialize display and Initialize the DHT sensor - we suggest to do it in the setup() function:

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print Temperature label to the LCD.
lcd.print("Temp. *C");
lcd.setCursor(0,1);
// Print the Humidity label to the LCD.
lcd.print("Hum. %");
dht.begin();
}
Step 4

Implement loop() to sample and display the values of temperature and relative humidity on the LCD display, every 3 seconds:

void loop() {
  // Wait three seconds between measurements.
  delay(3000);
 
// read relative humidity
float h = dht.readHumidity();
// read temperature as Celsius
float t = dht.readTemperature();
// read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// print Temperature value to the LCD
lcd.setCursor(14, 0);
lcd.print(t);
// print the Humidity value to the LCD
lcd.setCursor(14, 1);
lcd.print(h);
}

Result validation

Observe the temperature and relative humidity values shown in the LCD display. The display will refresh values every 3 seconds.

Platformio.ini

[env:uno]
platform = atmelavr
board = uno
framework = arduino
 
lib_ldf_mode=deep+
lib_compat_mode=strict
 
lib_deps =
 DHT sensor library@1.3.0
 Adafruit Unified Sensor@1.0.3
 
lib_deps_external =
 https://github.com/arduino-libraries/LiquidCrystal.git#1.0.7

IB2.cpp

#include <LiquidCrystal.h>
#include "DHT.h"
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 13, 2);
 
#define DHTPIN 8 // what pin we are connected to
// uncomment whatever type you're using
#define DHTTYPE DHT22
//#define DHTTYPE DHT22
 
// initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
 // set up the LCD's number of columns and rows
 lcd.begin(16, 2);
 // Print Temperature label to the LCD
 lcd.print("Temp. C");
 lcd.setCursor(0,1);
 // Print the Humidity label to the LCD
 lcd.print("Hum. %");
 dht.begin();
}
 
void loop() {
   // wait three seconds between measurements
   delay(3000);
   // read humidity
   float h = dht.readHumidity();
   // read temperature as Celsius
   float t = dht.readTemperature();
   // read temperature as Fahrenheit
   //float f = dht.readTemperature(true);
 
   // compute heat index in Fahrenheit (the default)
 //  float hif = dht.computeHeatIndex(f, h);
   // compute heat index in Celsius (isFahreheit = false);
   float hic = dht.computeHeatIndex(t, h, false);
 
   // print Temperature value to the LCD
   lcd.setCursor(14, 0);
   lcd.print(t);
   // print the Humidity value to the LCD
   lcd.setCursor(14, 1);
   lcd.print(h);
}