0

$USD

$USD
TUTORIALS Arduino

Arduino Intermediate Kit Tutorial 12: Temperature and Humidity Sensor

DFRobot Sep 01 2017 1620

In this session we will create a device that can measure the temperature and humidity in the environment and display the results in real time on a LCD screen. 

COMPONENTS

1×  DHT 11 Temperature & Humidity Sensor
1×  I2C LCD1602 module
DFRduion UNO R3 (similar as Arduino UNO)
IO Expansion Shield for Arduino V7.1


HARDWARE CONNECTIONS
Connect the DHT 11 Temperature & Humidity Sensor to digital pin 4 on the Sensor extension board.

The LCD connections are as follows:
LCD GND -- GND
LCD VCC --  5V
LCD SDA --  SDA
LCD SCL -- SCL


On the LCD screen, cover A0, A1, A2 with jumpers.

Be sure that your power, ground and signal connections are correct or you risk damaging your components.

When you have connected the components and checked your connections, plug the Arduino in to your PC with the USB cable so you can upload a program.


CODE INPUT

NOTE:
We will be using the following libraries in this program:
“dht11”
“LiquidCrystal_ I2C”


Make sure they are included in your Arduino IDE. If they are not, you can download them online. 

Put libraries “dht11” and “LiquidCrystal_I2C” into Arduino IDE’s libraries folder. If you don’t know how to load libraries, please refer to Experiment 12 for details.

Sample Code 9-1:
#include <dht11.h>   	
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);  // Set LCD’s address to be 0x20. Let it display 2 lines with 16 characters per line
dht11 DHT;
#define DHT11_PIN 4
void setup(){
  lcd.init();                 //LCD initial setup
  lcd.backlight();           // open LCD’s backlight
  Serial.begin(9600);       // Set serial port’s baud rate to be 9600
  // serial port prints ”Type, status, Humidity(%), Temperature(C)”
  Serial.println("Type,\tstatus,\tHumidity(%),\tTemperature(C)");
  lcd.print("Humidity(%): ");    //LCD screen prints ”Humidity(%):” 
  lcd.setCursor(0, 1);            //cursor to be moved to the first character in the 2nd line 
  lcd.print("Temp(C): ");         //LCD screen prints”Temp(C):”
}
void loop(){
  int chk;     //for storage of data collected by DHT11 sensor
  Serial.print("DHT11, \t");     
  // read data collected by DHT11 sensor
  chk = DHT.read(DHT11_PIN);    
  switch (chk){    
    case DHTLIB_OK:  
                Serial.print("OK,\t"); 
                break;
    case DHTLIB_ERROR_CHECKSUM: 
                Serial.print("Checksum error,\t"); 
                break;
    case DHTLIB_ERROR_TIMEOUT: 
                Serial.print("Time out error,\t"); 
                break;
    default: 
                Serial.print("Unknown error,\t"); 
                break;
  }
 // serial port shows temperature and humidity
  Serial.print(DHT.humidity,1);
  Serial.print(",\t");
  Serial.println(DHT.temperature,1);
 
  //LCD screen prints temperature and humidity
  lcd.setCursor(12, 0);
  lcd.print(DHT.humidity,1);
  lcd.setCursor(8, 1);
  lcd.print(DHT.temperature,1);
  delay(1000);
}

Use this sample code to implement the behavior we want.
You can copy and paste it in to the Arduino IDE, but if you want to develop you skills we recommend typing it out.

When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your Arduino.
   
Once the code has been uploaded, temperature & humidity will be shown on the LCD screen. You can also check the values in the serial monitor.



CODE ANALYSIS

First we include the libraries we need.
#include <dht11.h>   
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

We downloaded the dht11.h and LiquidCrystal_I2C.h libraries, but we don’t need to download Wire.h because it is already included in the Arduino IDE.

Now we can declare the LCD.

LiquidCrystal_I2C lcd(0x20,16,2);

0x20:I2C Address
The address is determined by the A0~A2 on the back side. 

16:16 characters per line
2: 2 lines

Functions included in LiquidCrystal_I2C:

lcd.init()

Initialize LCD

lcd.backlight()

Open LCD backlight

lcd.print()

print on screen

lcd.setCursor()

Set cursor on LCD screen

Note: Check LiquidCrystal_I2C/examples for more usages.


switch…case structure
   “switch” lets you execute different actions in different cases. Unlike the “if” structure, “switch” can have many cases.

switch(var){
       case 1:

           //When var=1, do something
           break;  //Get out of switch structure
       case 2:
           // When var=2, do something
           break;
       default:
           //If no case satisfy, run default. Default is optional.
}


Note:
1.”case” lines end with colons, not semicolons.
2.”break” is used to exit the switch structure. It is usually used in every case. If there is no break, the execution will continue to the next case until it sees break or the switch ends.

You can check out examples/05.Control/switchCase in the Arduino IDE for more information on switch…case usage.

Once you are familiar with Arduino, you can even add network module to your controller to allow you to post the real-time data collected to an online platform like weibo or twitter.

If you have any questions, please feel free to comment.
There is also another best selling arduino starter kit for you to choose.
Related category: arduino kits > education kits
Last Arduino Tutorial:  Arduino Intermediate Kit Tutorial 11: Fire Alarm
Next Arduino Tutorial: Arduino Intermediate Kit Tutorial 13: Open Sesame!