mirror of
https://github.com/dhrubasaha08/DHT11.git
synced 2023-10-23 22:17:45 +03:00
Created DHT11 sensor library
This commit is contained in:
parent
39b3dd62d1
commit
323e8c192b
35
README.md
35
README.md
@ -1,2 +1,35 @@
|
||||
# DHT11
|
||||
DHT11 Arduino Library: A simple and efficient library for reading temperature and humidity data from the DHT11 sensor without dependencies.
|
||||
|
||||
This is an Arduino library for the DHT11 temperature and humidity sensor. The library provides an easy-to-use interface for reading temperature and humidity data from the DHT11 sensor.
|
||||
|
||||
## How It Works
|
||||
|
||||
The DHT11 sensor provides temperature and humidity data over a single-wire interface. This library handles the low-level communication protocol with the sensor, allowing you to read the temperature and humidity data with simple function calls.
|
||||
|
||||
When you create an instance of the DHT11 class, you specify the pin that the sensor is connected to. Then, you can call the `readTemperature()` and `readHumidity()` methods to read the temperature and humidity data, respectively. These methods return the temperature in degrees Celsius and the relative humidity in percent, or `-1` if there was an error reading the data.
|
||||
|
||||
## Features
|
||||
|
||||
- **Easy to use**: Just create an instance of the DHT11 class and call the `readTemperature()` and `readHumidity()` methods.
|
||||
- **No dependencies**: This library does not depend on any other libraries, so it's easy to install and use.
|
||||
- **Examples included**: The library comes with example sketches that show you how to use it.
|
||||
|
||||
## Installation
|
||||
|
||||
To install the library, download the latest release from this repository and install it using the Arduino IDE's Library Manager.
|
||||
|
||||
## Usage
|
||||
|
||||
To use the library, include the `DHT11.h` header file in your sketch, create an instance of the DHT11 class, and call the `readTemperature()` and `readHumidity()` methods. See the example sketches included in the `examples` directory for more details.
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions to this library are welcome. Please open an issue if you find a bug or have a feature request, and feel free to submit pull requests if you've implemented a new feature or bug fix.
|
||||
|
||||
## License
|
||||
|
||||
This library is licensed under the MIT License. Please see the `LICENSE` file for more details.
|
||||
|
||||
## External References
|
||||
|
||||
- [DHT11 Datasheet](https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf)
|
||||
|
32
examples/ReadHumidity/ReadHumidity.ino
Normal file
32
examples/ReadHumidity/ReadHumidity.ino
Normal file
@ -0,0 +1,32 @@
|
||||
#include <DHT11.h>
|
||||
|
||||
// Create an instance of the DHT11 class and set the digital I/O pin.
|
||||
DHT11 dht11(2);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize serial communication at 115200 baud.
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Read the humidity from the sensor.
|
||||
float humidity = dht11.readHumidity();
|
||||
|
||||
// If the humidity reading was successful, print it to the serial monitor.
|
||||
if (humidity != -1)
|
||||
{
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(humidity);
|
||||
Serial.println(" %");
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the humidity reading failed, print an error message.
|
||||
Serial.println("Error reading humidity");
|
||||
}
|
||||
|
||||
// Wait for 2 seconds before the next reading.
|
||||
delay(2000);
|
||||
}
|
39
examples/ReadTempAndHumidity/ReadTempAndHumidity.ino
Normal file
39
examples/ReadTempAndHumidity/ReadTempAndHumidity.ino
Normal file
@ -0,0 +1,39 @@
|
||||
#include <DHT11.h>
|
||||
|
||||
// Create an instance of the DHT11 class and set the digital I/O pin.
|
||||
DHT11 dht11(2);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize serial communication at 115200 baud.
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Read the humidity from the sensor.
|
||||
float humidity = dht11.readHumidity();
|
||||
|
||||
// Read the temperature from the sensor.
|
||||
float temperature = dht11.readTemperature();
|
||||
|
||||
// If the temperature and humidity readings were successful, print them to the serial monitor.
|
||||
if (temperature != -1 && humidity != -1)
|
||||
{
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(temperature);
|
||||
Serial.println(" C");
|
||||
|
||||
Serial.print("Humidity: ");
|
||||
Serial.print(humidity);
|
||||
Serial.println(" %");
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the temperature or humidity reading failed, print an error message.
|
||||
Serial.println("Error reading data");
|
||||
}
|
||||
|
||||
// Wait for 2 seconds before the next reading.
|
||||
delay(2000);
|
||||
}
|
32
examples/ReadTemperature/ReadTemperature.ino
Normal file
32
examples/ReadTemperature/ReadTemperature.ino
Normal file
@ -0,0 +1,32 @@
|
||||
#include <DHT11.h>
|
||||
|
||||
// Create an instance of the DHT11 class and set the digital I/O pin.
|
||||
DHT11 dht11(2);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize serial communication at 115200 baud.
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Read the temperature from the sensor.
|
||||
float temperature = dht11.readTemperature();
|
||||
|
||||
// If the temperature reading was successful, print it to the serial monitor.
|
||||
if (temperature != -1)
|
||||
{
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(temperature);
|
||||
Serial.println(" C");
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the temperature reading failed, print an error message.
|
||||
Serial.println("Error reading temperature");
|
||||
}
|
||||
|
||||
// Wait for 2 seconds before the next reading.
|
||||
delay(2000);
|
||||
}
|
9
library.properties
Normal file
9
library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=DHT11
|
||||
version=1.0.0
|
||||
author=Dhruba Saha <dhrubasaha@outlook.com>
|
||||
maintainer=Dhruba Saha <dhrubasaha@outlook.com>
|
||||
sentence=A library for the DHT11 temperature and humidity sensor.
|
||||
paragraph=This library provides an easy-to-use interface for reading temperature and humidity data from a DHT11 sensor. It does not depend on any other libraries.
|
||||
category=Sensors
|
||||
url=http://github.com/dhrubasaha08/DHT11
|
||||
architectures=*
|
96
src/DHT11.cpp
Normal file
96
src/DHT11.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include "DHT11.h"
|
||||
|
||||
// Initializes the pin and sets it to output mode.
|
||||
DHT11::DHT11(int pin)
|
||||
{
|
||||
_pin = pin;
|
||||
pinMode(_pin, OUTPUT);
|
||||
digitalWrite(_pin, HIGH);
|
||||
}
|
||||
|
||||
// Reads and returns the temperature from the sensor. Returns -1 if the checksum is incorrect.
|
||||
float DHT11::readTemperature()
|
||||
{
|
||||
delay(150);
|
||||
byte data[5] = {0, 0, 0, 0, 0};
|
||||
startSignal();
|
||||
|
||||
if (digitalRead(_pin) == LOW)
|
||||
{
|
||||
delayMicroseconds(80);
|
||||
if (digitalRead(_pin) == HIGH)
|
||||
{
|
||||
delayMicroseconds(80);
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
data[i] = readByte();
|
||||
}
|
||||
|
||||
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))
|
||||
{
|
||||
return data[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Reads and returns the humidity from the sensor. Returns -1 if the checksum is incorrect.
|
||||
float DHT11::readHumidity()
|
||||
{
|
||||
delay(150);
|
||||
byte data[5] = {0, 0, 0, 0, 0};
|
||||
startSignal();
|
||||
|
||||
if (digitalRead(_pin) == LOW)
|
||||
{
|
||||
delayMicroseconds(80);
|
||||
if (digitalRead(_pin) == HIGH)
|
||||
{
|
||||
delayMicroseconds(80);
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
data[i] = readByte();
|
||||
}
|
||||
|
||||
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))
|
||||
{
|
||||
return data[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Reads a byte of data from the sensor.
|
||||
byte DHT11::readByte()
|
||||
{
|
||||
byte value = 0;
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
while (digitalRead(_pin) == LOW)
|
||||
;
|
||||
delayMicroseconds(30);
|
||||
if (digitalRead(_pin) == HIGH)
|
||||
{
|
||||
value |= (1 << (7 - i));
|
||||
}
|
||||
while (digitalRead(_pin) == HIGH)
|
||||
;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Sends the start signal to the sensor.
|
||||
void DHT11::startSignal()
|
||||
{
|
||||
pinMode(_pin, OUTPUT);
|
||||
digitalWrite(_pin, LOW);
|
||||
delay(18);
|
||||
digitalWrite(_pin, HIGH);
|
||||
delayMicroseconds(40);
|
||||
pinMode(_pin, INPUT);
|
||||
}
|
29
src/DHT11.h
Normal file
29
src/DHT11.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef DHT11_h
|
||||
#define DHT11_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
// DHT11 class for reading temperature and humidity data from a DHT11 sensor.
|
||||
class DHT11
|
||||
{
|
||||
public:
|
||||
// Takes the pin number as an argument.
|
||||
DHT11(int pin);
|
||||
|
||||
// Reads and returns the humidity from the sensor. Returns -1 if the checksum is incorrect.
|
||||
float readHumidity();
|
||||
|
||||
// Reads and returns the temperature from the sensor. Returns -1 if the checksum is incorrect.
|
||||
float readTemperature();
|
||||
|
||||
private:
|
||||
int _pin;
|
||||
|
||||
// Reads a byte of data from the sensor.
|
||||
byte readByte();
|
||||
|
||||
// Sends the start signal to the sensor.
|
||||
void startSignal();
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user