We will use a DHT11 sensor to get data about temperature and humidity of the environment.
As you can see it has three outputs (labels near to the pins): VCC (+5V), GND (ground), DATA (data transmission output).For ease of connection and assembling of the circuit, we will use the breadboard.
The diagram below will allow you to better understand how it works. The red, blue and black lines show how the pins are connected.
You can see that in order to connect a wire to your board, you need to connect it next to the corresponding pin.
To connect the sensor in our example you need to connect pin VCC
to the pin 3V
(3.3V) of the device, GND
to the G
(ground), DATA
to the D1
(GPIO5
). Typically, for sensors and other external devices, colored wires are selected to make it easier to connect them. To ease the task, the lightest is used for power wires (white in the example), darkest - for GND
(black), and others for outputs of data management and data transmission. Connect the sensor to the board.
If everything is connected correctly and the board is turned on in USB - the LED on the sensor will light up.
To work with the sensor, we need to install the appropriate library. Click the "Home" button at the toolbar.
Then select "Libraries" tab.
Search for the "DHT sensor library" by the author of "Adafruit Industries":
Click on the library and install it:
In the same way install Adafruit Unified Sensor
:
Return to your main.cpp
file and add the following code to it:
#include <Arduino.h>
// Connecting the library which help us
// easy to use the sensor
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Defining the constant with
// the number of the pin which
// connected to our sensor
#define DHT_PIN 5
#define DHT_TYPE DHT11
// Create an object of the DHT class and store it
// in the dht variable. As a parameter, we pass the pin number
// and type of the our sensor
DHT dht(DHT_PIN, DHT_TYPE);
void setup(){
Serial.begin(115200);
Serial.println("DHTxx test!");
// Launch the sensor scan
dht.begin();
}
void loop(){
// Make a delay between measurements
delay(2000);
// Reading data from the sensor takes 250ms!
// Data may be outdated for 2 seconds (this is a fairly slow sensor).
// Read the percentage of humidity
float h = dht.readHumidity();
// Read the temperature in Celsius (default)
float t = dht.readTemperature();
// Read the temperature in Fahrenheit (is Fahrenheit = true)
float f = dht.readTemperature(true);
// We check is all the measurements are correct. If
// some error will happen, method will return
// NaN (not a number)
// Error reading is not uncommon.
// There are many factors that can cause them.
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
// If at least one of the measure is not correct, then finish the program before
// and start measure again
return;
}
// Calculate the heating index in Farydayahati (by default)
float hif = dht.computeHeatIndex(f, h);
// Calculate the heating index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
Open the monitor of the serial port. If everything was done correctly and everything works, then you will see the following screen:
To check the sensor, press it in the palm of your hand and blow it with warm air. After a few seconds, you will see that the temperature and humidity data will change.
In the next section, we will learn how to store the temperature and humidity data in Firebase.