Sensor Library for the AM2302 Sensor (aka DHT22) from ASAIR.
This Library is a controller independent library for reading the AM2302 Sensor also known as DHT22.
One small Docu you will find in the docs folder of this repo.
The actual manufacturer page is linked here: ASAIR AM2302.
The most detailed datasheet you will find here: AM2302 Product Manual.
The Sensor-Library documentation you will find in the provided refman.pdf, or under github-pages github.io.
REMARK:
Against the most documentations and datasheets the following Pin description is correct (from left to right):
- Pin1: VDD (3,3...5 V)
- Pin2: SDA (Serial Data, two way)
- Pin3: GND
- PIN4: GND
The library is intended to be used on each microcontroller for Example:
- Arnuino Nano
- Arduino Nano 33 IOT
- ESP8266
- ESP32
- etc ...
Include the library
#include <AM2302-Sensor.h>
The library use namespaces, so the object can be instantiated and used by:
AM2302::AM2302_Sensor am2302{PIN};
void setup() {
am2302.begin();
auto status = am2302.read();
Serial.print("\n\nstatus of sensor read(): ");
Serial.println(AM2302::AM2302_Sensor::get_sensorState(status));
Serial.print("Temperature: ");
Serial.println(am2302.get_Temperature());
Serial.print("Humidity: ");
Serial.println(am2302.get_Humidity());
}
The following status codes exists:
- AM2302_READ_OK {0};
- AM2302_ERROR_CHECKSUM {-1};
- AM2302_ERROR_TIMEOUT {-2};
- AM2302_ERROR_READ_FREQ {-3};
The project contains unit tests for the pure helper functions in src/AM2302-Sensor_Tools.h. These tests validate:
- checksum calculation
- humidity decoding
- temperature decoding
- regression cases for bit shift and sign handling
The tests are based on GoogleTest and are built with CMake.
Required tools:
- CMake
- a C++17 compatible compiler
With Visual Studio or Visual Studio Build Tools installed, run the commands in a Developer PowerShell:
# Generate Build System
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build a Project
cmake --build build
# Execute Tests
ctest --test-dir build --output-on-failureInstall CMake and a compiler toolchain, for example g++ or clang++, and run:
# Generate Build System
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build a Project
cmake --build build
# Execute Tests
ctest --test-dir build --output-on-failureInstall Xcode Command Line Tools and CMake, then run:
# Generate Build System
cmake -B build -DCMAKE_BUILD_TYPE=Release
# Build a Project
cmake --build build
# Execute Tests
ctest --test-dir build --output-on-failureThe GitHub Actions workflow unit_tests.yml builds and runs the unit tests automatically on every push and pull request.
This library is licensed under MIT Licence.
