The DHT11 is a popular and inexpensive temperature and humidity sensor widely used in basic environmental monitoring applications. It is simpler and less accurate than the DHT22, but still offers decent performance for many projects where high precision is not critical. The DHT11 can be used with Arduino, Raspberry Pi, and other microcontroller platforms to measure temperature and humidity in environments like homes, gardens, or simple weather stations.
The DHT11 module typically has 3 or 4 pins depending on the version, with the following pin configuration:
PinNameDescription1VCCPower supply (3.3V to 5V)2DataData output (connected to the microcontroller)3NCNot Connected (some modules have this pin)4GNDGround (0V)
The DHT11 sensor uses a thermistor to measure temperature and a capacitive humidity sensor to measure humidity. It generates a digital signal containing the temperature and humidity data, which is transmitted to a microcontroller (such as an Arduino or Raspberry Pi) over a single-wire interface.
Each time you read the sensor, it sends a new set of data for the temperature and humidity.
To connect the DHT11 sensor to an Arduino, follow these simple steps:
Here’s a basic connection diagram:
DHT11 Pin -> Arduino Pin ------------------------------ VCC -> 5V (or 3.3V) GND -> GND DATA -> Pin 2 (or any other digital pin)
To read the temperature and humidity from the DHT11 sensor, you can use the DHT sensor library for Arduino. This library simplifies the communication between the Arduino and the sensor.
#include <DHT.h> // Define the pin where the DHT11 data line is connected #define DHTPIN 2 // Define the type of sensor (DHT11) #define DHTTYPE DHT11 // Initialize the DHT sensor DHT dht(DHTPIN, DHTTYPE); void setup() { // Start the serial communication Serial.begin(9600); // Initialize the DHT sensor dht.begin(); } void loop() { // Wait a few seconds between readings delay(2000); // Read temperature as Celsius float temperature = dht.readTemperature(); // Read humidity float humidity = dht.readHumidity(); // Check if readings failed, if so, display error message if (isnan(temperature) || isnan(humidity)) { Serial.println("Failed to read from DHT sensor!"); return; } // Print temperature and humidity values to the serial monitor Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C"); Serial.print(" Humidity: "); Serial.print(humidity); Serial.println(" %"); }
DHT dht(DHTPIN, DHTTYPE)
: Initializes the DHT11 sensor on the specified pin and sensor type.dht.readTemperature()
: Reads the temperature in Celsius.dht.readHumidity()
: Reads the humidity level as a percentage.delay(2000)
).If you're using a Raspberry Pi, you can read the data from the DHT11 sensor using the GPIO pins. You’ll need to install the Adafruit Python DHT library to interact with the sensor.
To install the necessary libraries on Raspberry Pi:
sudo apt-get update sudo apt-get install build-essential python-dev python-openssl sudo pip3 install Adafruit-DHT
Once installed, you can use the following Python code to read the DHT11 sensor:
import Adafruit_DHT # Set sensor type and pin sensor = Adafruit_DHT.DHT11 pin = 4 # GPIO pin (e.g., GPIO4) # Read temperature and humidity humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temp: {0:0.1f} C Humidity: {1:0.1f}%'.format(temperature, humidity)) else: print('Failed to get reading. Try again!')
This code will print the temperature and humidity values to the terminal.
Origin:- China
Brand:- Generic