Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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:
Wiring the DHT11 Sensor:
The DHT11 sensor has four pins, but only three are used:
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:
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:
DHT
library is included to facilitate communication with the sensor.DHT
object is initialized with the pin number and sensor type.setup()
function, the serial communication is initialized, and the DHT sensor is started.loop()
function reads the humidity and temperature every two seconds and prints the values to the Serial Monitor.isnan()
function checks if the readings are valid.Running the Program: