diff --git a/README.md b/README.md index c137ea3..56e20c0 100644 --- a/README.md +++ b/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) diff --git a/examples/ReadHumidity/ReadHumidity.ino b/examples/ReadHumidity/ReadHumidity.ino new file mode 100644 index 0000000..e108ad5 --- /dev/null +++ b/examples/ReadHumidity/ReadHumidity.ino @@ -0,0 +1,32 @@ +#include + +// 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); +} diff --git a/examples/ReadTempAndHumidity/ReadTempAndHumidity.ino b/examples/ReadTempAndHumidity/ReadTempAndHumidity.ino new file mode 100644 index 0000000..f3a0bd7 --- /dev/null +++ b/examples/ReadTempAndHumidity/ReadTempAndHumidity.ino @@ -0,0 +1,39 @@ +#include + +// 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); +} diff --git a/examples/ReadTemperature/ReadTemperature.ino b/examples/ReadTemperature/ReadTemperature.ino new file mode 100644 index 0000000..3adde37 --- /dev/null +++ b/examples/ReadTemperature/ReadTemperature.ino @@ -0,0 +1,32 @@ +#include + +// 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); +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..2a14df9 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=DHT11 +version=1.0.0 +author=Dhruba Saha +maintainer=Dhruba Saha +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=* diff --git a/src/DHT11.cpp b/src/DHT11.cpp new file mode 100644 index 0000000..1a32d36 --- /dev/null +++ b/src/DHT11.cpp @@ -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); +} diff --git a/src/DHT11.h b/src/DHT11.h new file mode 100644 index 0000000..bffec9e --- /dev/null +++ b/src/DHT11.h @@ -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 \ No newline at end of file