# 2: Read Temperature and Humidity with DHT22

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

You will need

• ESp8266-NodeMCU Development Board
• DHT22 Temperature&Humidity sensor
• Jumper Wires
• USB to MicroUSB cable

## Connect DHT22 sensor

Connect the DHT22 Temperature and Humidity sensor to Node MCU as shown below. Data (Signal) line is connected to pin D2 of NodeMCU.

Using USB to MicroUSB cable connect NodeMCU to your computer. You should hear a sound indicating that your computer recognized the device, otherwise you need to go back and make sure you've completed all set up steps.

## Sketch for DHT22

2. Copy the following code to a new Sketch. You can replace the prefilled functions since this code already contains them.

Note: To comment something out use a double slash (//). If you need to comment out more than one line you can highlight them, Right-click --> Comment/Uncomment. There is also an option with /* and */ (see example in the code below).

/*
*Read Temperature and Humidity with DHT22
*Sketch for ESP8266-NodeMCU
*/

#include "DHT.h"  //Library for the sensor
#define DHTTYPE DHT22 // There are several DHT sensors that this code can be used for. In our case we use DHT22
#define DHTPIN D2 // DHT22 sensor's data line is connected to pin D2

float h; //It is always a good idea to declare variables in the very beginning.
float t;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(115200);  //Start serial monitor. We only need to do this once, hence this line of code is in setup()
dht.begin(); // Start the sensor
}

void loop() {
h = dht.readHumidity(); //Store humidity value in variable h
t = dht.readTemperature(); ////Store temperature value in variable t

Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");

delay(10*1000);       // print new values every 10 seconds (10,000 milliseconds)
}

4. Open Serial Monitor by clicking  on the magnifying glass in the top right corner of Arduino IDE window.

5. Make sure baud is set to 115200 as we declared in the code.