SkillAgentSearch skills...

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/DHT11
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

DHT11 Arduino Library

Static Badge GitHub Release GitHub GitHub contributors GitHub Downloads (all assets, all releases) DOI

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.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 readTemperatureHumidity method, making it consistent with the readTemperature and readHumidity methods.
  • 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() 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.
  • 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 an int indicating 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

View on GitHub
GitHub Stars64
CategoryDevelopment
Updated1mo ago
Forks14

Languages

C++

Security Score

100/100

Audited on Feb 21, 2026

No findings