Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use the DHT11 Sensor with Arduino to Measure Temperature and Humidity

The DHT11 is a commonly used sensor for measuring temperature and humidity. It is popular in Arduino projects due to its simplicity and low cost. This article will guide you through the process of connecting a DHT11 sensor to an Arduino board and writing a simple program to read and display the sensor data.

Examples:

Components Required:

  1. Arduino Uno (or any compatible board)
  2. DHT11 Sensor
  3. Jumper wires
  4. Breadboard (optional)
  5. 10k ohm resistor (optional for better stability)

Wiring the DHT11 Sensor:

The DHT11 sensor has four pins, but only three are used:

  1. VCC: Connect to 5V on the Arduino
  2. GND: Connect to GND on the Arduino
  3. DATA: Connect to any digital pin on the Arduino (e.g., pin 2)
  4. NC: Not connected

Optionally, place a 10k ohm resistor between VCC and DATA for better signal stability.

Arduino Code:

To read data from the DHT11 sensor, we will use the DHT library. First, install the DHT sensor library in the Arduino IDE:

  1. Open the Arduino IDE.
  2. Go to Sketch -> Include Library -> Manage Libraries.
  3. Search for "DHT sensor library" by Adafruit and install it.

Here is a sample code to read temperature and humidity from the DHT11 sensor:

#include <DHT.h>

#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  delay(2000);  // Wait a few seconds between measurements

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");
}

Explanation:

  • The DHT library is included to facilitate communication with the sensor.
  • The DHT object is initialized with the pin number and sensor type.
  • In the setup() function, the serial communication is initialized, and the DHT sensor is started.
  • The loop() function reads the humidity and temperature every two seconds and prints the values to the Serial Monitor.
  • The isnan() function checks if the readings are valid.

Running the Program:

  1. Connect your Arduino to your computer.
  2. Open the Arduino IDE and upload the code to the board.
  3. Open the Serial Monitor (Tools -> Serial Monitor) to view the temperature and humidity readings.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.