DHT11 Arduino Library: A simple and efficient library for reading temperature and humidity data from the DHT11 sensor without dependencies.
Go to file
Dhruba Saha cb4c11ffa1
Enhanced readability
Signed-off-by: Dhruba Saha <dhrubasaha08@gmail.com>
2023-09-20 09:03:31 +05:30
assets v2.0.0: Enhanced docs, error handling & examples 2023-09-19 11:02:49 +05:30
examples Added ESP32, ESP8266 compatibility 2023-09-20 06:45:14 +05:30
src v2.0.0: Enhanced docs, error handling & examples 2023-09-19 11:02:49 +05:30
CODE_OF_CONDUCT.md v2.0.0: Enhanced docs, error handling & examples 2023-09-19 11:02:49 +05:30
CONTRIBUTING.md v2.0.0: Enhanced docs, error handling & examples 2023-09-19 11:02:49 +05:30
LICENSE Initial commit 2023-05-30 10:59:21 +05:30
README.md Enhanced readability 2023-09-20 09:03:31 +05:30
library.properties v2.0.0: Enhanced docs, error handling & examples 2023-09-19 11:02:49 +05:30

README.md

DHT11 Arduino Library

Table of Contents

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.0.0

License: MIT

v2.0.0 Updates

  • Changed the return type of readTemperature() and readHumidity() methods from float to int. This aligns with the DHT11 sensor's 1-degree resolution.
  • Enhanced code documentation for easier maintenance and better readability.
  • Added the getErrorString method to return human-readable error messages based on error codes.
  • Major code refactoring for better maintainability and robustness.
  • Introduced the CONTRIBUTING.md file to guide contributors on how to effectively contribute to the library.
  • Added a CODE_OF_CONDUCT.md to establish community guidelines and expectations for behavior.
  • Introduced the ReadPlot example 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:

  1. Sending a start signal to the DHT11 sensor.
  2. Reading a series of pulses from the sensor representing data bits.
  3. Interpreting the received bits to extract temperature and humidity values.
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries....
  3. In the Library Manager, enter "DHT11" into the search box.
  4. Find the DHT11 library in the list and install it.

Installing Manually from GitHub

  1. Download the latest release of the library from this GitHub repository as a ZIP file.
  2. Open the Arduino IDE.
  3. Go to Sketch > Include Library > Add .ZIP Library....
  4. Navigate to the downloaded ZIP file and select it to install.

Usage

Basic Usage

  • Include the DHT11.h header file.
  • Create an instance of the DHT11 class, specifying the digital pin connected to the sensor's data pin.
  • Use readTemperature() and 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.

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.

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. This is perfect for those who want a visual representation and trend analysis of the sensor's readings.

Error Handling

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 readTemperature() and readHumidity() return the value DHT11::ERROR_TIMEOUT.
  • If there's a checksum mismatch indicating data corruption, the methods return the value DHT11::ERROR_CHECKSUM.

For translating these error codes to human-readable strings, the library offers the DHT11::getErrorString(int errorCode) method.

Troubleshooting

  • 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.
  • Checksum Errors:

    • 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.

FAQ

  1. 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.

  2. What's the recommended frequency for sensor readings?
    It's best to allow at least 1 second between readings for accurate and stable results.

  3. 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.

  4. 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.

  5. 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.

Compatibility

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.

  1. Fork the repository.
  2. Update the README with the board you've tested.
  3. Create a pull request with your changes.

Your contribution will be greatly appreciated, and it will benefit the entire Arduino community.

Contributing

For guidelines on contributing to this project, please see the CONTRIBUTING.md file.

Code of Conduct

To understand the community guidelines and expected behavior, please refer to the CODE_OF_CONDUCT.md file.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

External References