2022-01-11 14:48:39 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: DHT20.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: Arduino library for DHT20 I2C temperature and humidity sensor.
|
2023-10-25 11:19:14 -04:00
|
|
|
// VERSION: 0.3.0
|
2022-01-11 14:48:39 -05:00
|
|
|
// URL: https://github.com/RobTillaart/DHT20
|
|
|
|
//
|
|
|
|
|
2022-09-11 05:44:04 -04:00
|
|
|
// Always check datasheet - front view
|
|
|
|
//
|
|
|
|
// +--------------+
|
|
|
|
// VDD ----| 1 |
|
|
|
|
// SDA ----| 2 DHT20 |
|
|
|
|
// GND ----| 3 |
|
|
|
|
// SCL ----| 4 |
|
|
|
|
// +--------------+
|
|
|
|
|
2022-01-11 14:48:39 -05:00
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
2023-10-25 11:19:14 -04:00
|
|
|
#define DHT20_LIB_VERSION (F("0.3.0"))
|
2022-01-11 14:48:39 -05:00
|
|
|
|
|
|
|
#define DHT20_OK 0
|
|
|
|
#define DHT20_ERROR_CHECKSUM -10
|
|
|
|
#define DHT20_ERROR_CONNECT -11
|
|
|
|
#define DHT20_MISSING_BYTES -12
|
2022-09-16 13:27:01 -04:00
|
|
|
#define DHT20_ERROR_BYTES_ALL_ZERO -13
|
2022-09-17 07:50:11 -04:00
|
|
|
#define DHT20_ERROR_READ_TIMEOUT -14
|
|
|
|
#define DHT20_ERROR_LASTREAD -15
|
2022-01-11 14:48:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
class DHT20
|
|
|
|
{
|
|
|
|
public:
|
2022-09-18 13:13:39 -04:00
|
|
|
// CONSTRUCTOR
|
|
|
|
// fixed address 0x38
|
|
|
|
DHT20(TwoWire *wire = &Wire);
|
2022-01-11 14:48:39 -05:00
|
|
|
|
|
|
|
bool begin();
|
|
|
|
bool isConnected();
|
2022-12-21 15:48:21 -05:00
|
|
|
uint8_t getAddress();
|
2022-01-11 14:48:39 -05:00
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
|
2022-09-11 05:44:04 -04:00
|
|
|
// ASYNCHRONUOUS CALL
|
2022-09-18 13:13:39 -04:00
|
|
|
// trigger acquisition.
|
2022-09-17 07:50:11 -04:00
|
|
|
int requestData();
|
2022-09-18 13:13:39 -04:00
|
|
|
// read the raw data.
|
2022-09-17 07:50:11 -04:00
|
|
|
int readData();
|
2023-02-10 10:22:51 -05:00
|
|
|
// converts raw data bits to temperature and humidity.
|
2022-09-11 05:44:04 -04:00
|
|
|
int convert();
|
2022-01-11 14:48:39 -05:00
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
|
2023-02-10 10:22:51 -05:00
|
|
|
// SYNCHRONOUS CALL
|
2022-09-18 13:13:39 -04:00
|
|
|
// blocking read call to read + convert data
|
2022-01-11 14:48:39 -05:00
|
|
|
int read();
|
2022-09-18 13:13:39 -04:00
|
|
|
// access the converted temperature & humidity
|
2022-09-17 07:50:11 -04:00
|
|
|
float getHumidity();
|
|
|
|
float getTemperature();
|
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
|
|
|
|
// OFFSET 1st order adjustments
|
2023-02-10 10:22:51 -05:00
|
|
|
void setHumOffset(float offset = 0);
|
|
|
|
void setTempOffset(float offset = 0);
|
2022-09-17 07:50:11 -04:00
|
|
|
float getHumOffset();
|
|
|
|
float getTempOffset();
|
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
|
2022-09-17 07:50:11 -04:00
|
|
|
// READ STATUS
|
|
|
|
uint8_t readStatus();
|
2022-09-18 13:13:39 -04:00
|
|
|
// 3 wrapper functions around readStatus()
|
2022-09-17 07:50:11 -04:00
|
|
|
bool isCalibrated();
|
|
|
|
bool isMeasuring();
|
|
|
|
bool isIdle();
|
2022-09-18 13:13:39 -04:00
|
|
|
// status from last read()
|
2022-09-17 07:50:11 -04:00
|
|
|
int internalStatus();
|
2022-01-11 14:48:39 -05:00
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
|
|
|
|
// TIMING
|
2022-09-17 07:50:11 -04:00
|
|
|
uint32_t lastRead();
|
|
|
|
uint32_t lastRequest();
|
|
|
|
|
2022-01-11 14:48:39 -05:00
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
// RESET (new since 0.1.4)
|
2023-02-10 10:22:51 -05:00
|
|
|
// use with care
|
2022-09-18 13:13:39 -04:00
|
|
|
// returns number of registers reset => must be 3
|
|
|
|
// 3 = OK
|
|
|
|
// 0,1,2 = error.
|
|
|
|
// 255 = no reset needed.
|
|
|
|
// See datasheet 7.4 Sensor Reading Process, point 1
|
2023-02-10 10:22:51 -05:00
|
|
|
// use with care
|
2022-09-18 13:13:39 -04:00
|
|
|
uint8_t resetSensor();
|
|
|
|
|
|
|
|
|
2022-01-11 14:48:39 -05:00
|
|
|
private:
|
|
|
|
float _humidity;
|
|
|
|
float _temperature;
|
|
|
|
float _humOffset;
|
|
|
|
float _tempOffset;
|
2022-09-17 07:50:11 -04:00
|
|
|
|
2022-01-11 14:48:39 -05:00
|
|
|
uint8_t _status;
|
|
|
|
uint32_t _lastRequest;
|
|
|
|
uint32_t _lastRead;
|
|
|
|
uint8_t _bits[7];
|
|
|
|
|
|
|
|
uint8_t _crc8(uint8_t *ptr, uint8_t len);
|
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
// use with care
|
|
|
|
bool _resetRegister(uint8_t reg);
|
|
|
|
|
2022-01-11 14:48:39 -05:00
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-10 10:22:51 -05:00
|
|
|
// -- END OF FILE --
|
2022-01-11 14:48:39 -05:00
|
|
|
|