Signed-off-by: Dhruba Saha <firstname.lastname@example.org>
DHT11 Arduino Library
Table of Contents
- v2.0.0 Updates
- How It Works
- Error Handling
- Code of Conduct
- External References
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
- Changed the return type of
int. 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Arduino IDE Library Manager
- Open the Arduino IDE.
- Go to
- 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
Add .ZIP Library....
- Navigate to the downloaded ZIP file and select it to install.
- Include the
- Create an instance of the DHT11 class, specifying the digital pin connected to the sensor's data pin.
readHumidity()methods to read the data.
New Methods in v2.0.0
getErrorString(int errorCode): Returns a human-readable error message based on the provided error code.
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.
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.
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.
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.
A visually interactive example that uses the Arduino Serial Plotter to graphically display temperature and humidity data in real-time. This is perfect for those who want a visual representation and trend analysis of the sensor's readings.
The library provides clear error handling mechanisms. When reading data:
- If there's a timeout while waiting for a response from the sensor, the methods
readHumidity()return the value
- If there's a checksum mismatch indicating data corruption, the methods return the value
For translating these error codes to human-readable strings, the library offers the
DHT11::getErrorString(int errorCode) method.
Sensor Not Responding or Constant Timeouts:
- Ensure the sensor receives the proper voltage (3.3V or 5V).
- Double-check the data pin connection to the Arduino board.
- Restart the Arduino IDE after any changes.
- Ensure minimal distance between the sensor and the Arduino board.
- Check the surrounding environment for electrical noise sources.
Inaccurate or Unstable Readings:
- Give the sensor time to stabilize after powering on.
- Position the sensor away from direct sources of temperature or humidity changes.
Library Not Found in Arduino IDE:
- Confirm the library's correct installation.
- Restart the Arduino IDE.
Is this library designed for the DHT22 sensor as well?
No. The library caters exclusively to the DHT11 sensor. The DHT22 uses a different data format, necessitating a separate library.
What's the recommended frequency for sensor readings?
It's best to allow at least 1 second between readings for accurate and stable results.
I'm experiencing persistent timeout errors. What can I do?
Ensure proper wiring, verify the sensor's power source, and check if the specified data pin in the Arduino code matches your hardware setup.
Has this library been tested on platforms other than the Arduino IDE?
While it's been primarily tested on the Arduino IDE and Arduino Uno R3 board, it might work on other platforms or boards. However, results may vary.
Is the library compatible with third-party platforms?
Although tailored for the Arduino IDE, the library might function on other platforms. Still, I don't guarantee compatibility or consistent outcomes on third-party platforms.
The library has been tested and confirmed to work on the following boards:
- Arduino Uno R3 (ATmega328P - AVR architecture)
- NodeMCU ESP32S v1.1
ESP-WROOM-32(Tensilica Xtensa LX6 - xtensa architecture)
- NodeMCU ESP8266 v1.0
ESP8266MOD(Tensilica Xtensa LX106 - xtensa architecture)
* For xtensa-based boards (ESP32 and ESP8266), a delay is required between consecutive method calls for optimal performance. Check Examples for the implementation details.
Given the vast number of boards and architectures available, it's a challenge for a solo developer to test on all. Community contributions in terms of compatibility testing are highly encouraged.
Contribute by Testing
If you've successfully used this library on a board not listed above, please consider contributing by letting me know. This will help the community to have a broader understanding of the library's compatibility.
- Fork the repository.
- Update the README with the board you've tested.
- Create a pull request with your changes.
Your contribution will be greatly appreciated, and it will benefit the entire Arduino community.
For guidelines on contributing to this project, please see the
Code of Conduct
To understand the community guidelines and expected behavior, please refer to the
This library is licensed under the
MIT License. See the
LICENSE file for more details.