DHT11
DHT11 Arduino Library: A simple and efficient library for reading temperature and humidity data from the DHT11 sensor without dependencies.
Install / Use
/learn @dhrubasaha08/DHT11README
DHT11 Arduino Library
Table of Contents
- DHT11 Arduino Library
Introduction
This Arduino library is designed for the DHT11 temperature and humidity sensor. It simplifies the process of reading temperature and humidity data, making it easy to integrate the DHT11 sensor into your Arduino projects.
Author: Dhruba Saha
Version: 2.1.0
License: MIT
Updates
v2.1.0 Hotfix Latest
- Introduced the
readTemperatureHumidity(int &temperature, int &humidity)method, allowing for efficient simultaneous reading of temperature and humidity, streamlining the data acquisition process. - Refactored the internal data reading process, reducing code repetition and improving maintainability.
- Enhanced error handling: Now, specific error codes are returned from the
readTemperatureHumiditymethod, making it consistent with thereadTemperatureandreadHumiditymethods. - Documentation updated to reflect the new changes and provide clear guidance on using the new library version.
Note: The updates in version 2.1.0 are backward compatible with the previous versions(v2.x.x), ensuring a smooth transition for existing projects.
v2.0.0 Update
- Changed the return type of
readTemperature()andreadHumidity()methods fromfloattoint. This aligns with the DHT11 sensor's 1-degree resolution. - Enhanced code documentation for easier maintenance and better readability.
- Added the
getErrorStringmethod to return human-readable error messages based on error codes. - Major code refactoring for better maintainability and robustness.
- Introduced the
CONTRIBUTING.mdfile to guide contributors on how to effectively contribute to the library. - Added a
CODE_OF_CONDUCT.mdto establish community guidelines and expectations for behavior. - Introduced the
ReadPlotexample which uses the Arduino Serial Plotter to display temperature and humidity data. This example provides a visual representation of the sensor's readings, making it easier to monitor environmental conditions in real-time. - Added ESP32/ESP8266 compatibility.
How It Works
The DHT11 sensor uses a custom single-wire protocol for communication. The basic process for reading data involves:
- Sending a start signal to the DHT11 sensor.
- Reading a series of pulses from the sensor representing data bits.
- Interpreting the received bits to extract temperature and humidity values.
- Validating the data using a checksum.
The library handles these steps internally, providing the user with a simple interface for reading temperature and humidity.
Internal Protocol Handling
The library initiates the data request to the DHT11 sensor and then reads a 40-bit data stream in response. The data stream is parsed to obtain accurate temperature and humidity readings. The process of reading data from the DHT11 involves multiple steps:
Source & Credit : Mouser DHT11 Datasheet
-
Initialization and Request:
- The MCU (like an Arduino) sends a start signal by pulling the data line low for at least 18ms.
- The MCU then pulls the line high for 20-40us to indicate that it's ready to receive a response.
-
DHT11 Response:
- Upon detecting the start signal from the MCU, the DHT11 sends a response signal.
- This response consists of a 80us low voltage level followed by an 80us high voltage level.
-
Data Transmission:
- The DHT11 transmits its data in a series of pulses. Each bit of data is represented by a specific combination of high and low voltage durations.
- A '0' is represented by 50us of low voltage followed by 26-28us of high voltage.
- A '1' is represented by 50us of low voltage followed by 70us of high voltage.
- The DHT11 sends 40 bits of data in total: 16 bits for humidity, 16 bits for temperature, and 8 bits for checksum.
- The checksum is the last 8 bits of the sum of the first 32 bits. It's used to verify data integrity.
-
Data Interpretation:
- After reading the 40 bits, the MCU processes the data to extract temperature and humidity values.
- The bits are grouped to form the integral and decimal parts of the temperature and humidity readings, though the DHT11 provides only integer values.
-
Completion:
- After data transmission, the DHT11 pulls the data line low for 50us, marking the end of the communication. The MCU then pulls the line high, putting the DHT11 in a low-power standby mode.
This library abstracts these complexities, allowing users to easily read temperature and humidity values with simple function calls. Behind the scenes, it manages the signaling, data reading, and interpretation based on the DHT11's protocol.
Features
- Easy to Use: Easy to use interface for reading temperature and humidity from the DHT11 sensor.
- No External Dependencies: The library is standalone and doesn't require any external libraries.
- Example Sketches: The library package includes example Arduino sketches to get you started quickly.
- Error Handling: The library package includes Error handling mechanisms to ensure robustness.
Installation
Arduino IDE Library Manager
- Open the Arduino IDE.
- Go to
Sketch>Include Library>Manage Libraries.... - In the Library Manager, enter "DHT11" into the search box.
- Find the DHT11 library in the list and install it.
Installing Manually from GitHub
- Download the latest release of the library from this GitHub repository as a ZIP file.
- Open the Arduino IDE.
- Go to
Sketch>Include Library>Add .ZIP Library.... - Navigate to the downloaded ZIP file and select it to install.
Usage
Basic Usage
- Include the
DHT11.hheader file. - Create an instance of the DHT11 class, specifying the digital pin connected to the sensor's data pin.
- Use
readTemperature()andreadHumidity()methods to read the data. getErrorString(int errorCode): Returns a human-readable error message based on the provided error code.
New Methods
readTemperatureHumidity(int &temperature, int &humidity): This new method allows for simultaneous reading of temperature and humidity, reducing the complexity and time required for separate readings. It returns anintindicating the success or specific error encountered during the operation.
Wiring Details
The DHT11 sensor has three or four pins, depending on the variant:
- VCC: Connect to 3.3V or 5V on your MCU (based on your sensor's specification).
- Data: Connect to a digital I/O pin on your MCU (not an analog pin). For the provided examples, we use digital pin 2 by default.
- Ground (GND): Connect to the ground of your MCU.
- NC (No Connect): Some variants have this pin. It is not used and can be left unconnected.
Remember to use a pull-up resistor (typically 10kΩ) between the VCC and Data pins for reliable communication.(Optional)
Examples
All examples provided use Arduino UNO's digital pin 2 as the default connection to the DHT11 sensor's data pin. Modify this in the code if using a different pin.
-
Read Humidity
This example demonstrates how to simply read the humidity value from the DHT11 sensor and display it on the Arduino Serial Monitor. It's a basic introduction to using the library to get humidity data. -
Read Temperature and Humidity
A more comprehensive example that shows how to read both temperature and humidity values from the DHT11 sensor. Results are displayed on the Arduino Serial Monitor, giving users a complete view of the environment. -
Read Temperature
Similar to the 'Read Humidity' example, but focused solely on reading and displaying the temperature value from the DHT11 sensor. It provides a straightforward way to monitor temperature using the library. -
Read Plot
A visually interactive example that uses the Arduino Serial Plotter to graphically display temperature and humidity data in real-time.
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
