2022-09-18 13:13:39 -04:00
|
|
|
//
|
|
|
|
// FILE: DHT20_offset.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
|
2024-01-26 07:10:13 -05:00
|
|
|
// URL: https://github.com/RobTillaart/DHT20
|
2022-09-18 13:13:39 -04:00
|
|
|
//
|
|
|
|
// Always check datasheet - front view
|
|
|
|
//
|
|
|
|
// +--------------+
|
|
|
|
// VDD ----| 1 |
|
|
|
|
// SDA ----| 2 DHT20 |
|
|
|
|
// GND ----| 3 |
|
|
|
|
// SCL ----| 4 |
|
|
|
|
// +--------------+
|
|
|
|
|
|
|
|
|
|
|
|
#include "DHT20.h"
|
|
|
|
|
|
|
|
DHT20 DHT;
|
|
|
|
|
|
|
|
uint8_t count = 0;
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println(__FILE__);
|
|
|
|
Serial.print("DHT20 LIBRARY VERSION: ");
|
|
|
|
Serial.println(DHT20_LIB_VERSION);
|
|
|
|
Serial.println();
|
|
|
|
|
2023-10-25 11:19:14 -04:00
|
|
|
Wire.begin();
|
|
|
|
Wire.setClock(400000);
|
|
|
|
|
|
|
|
DHT.begin(); // ESP32 default pins 21 22
|
|
|
|
|
2022-09-18 13:13:39 -04:00
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// set an offset after 10 seconds.
|
|
|
|
if (millis() > 10000)
|
|
|
|
{
|
|
|
|
DHT.setTempOffset(1.7);
|
|
|
|
DHT.setHumOffset(-2.3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a measurement every 2 seconds
|
|
|
|
if (millis() - DHT.lastRead() >= 2000)
|
|
|
|
{
|
2023-10-25 11:19:14 -04:00
|
|
|
// READ DATA
|
2022-09-18 13:13:39 -04:00
|
|
|
uint32_t start = micros();
|
|
|
|
int status = DHT.read();
|
|
|
|
uint32_t stop = micros();
|
|
|
|
|
|
|
|
if ((count % 10) == 0)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("Type\tHumidity (%)\tTemp (°C)\tTime (µs)\tStatus\tOffset");
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
Serial.print("DHT20 \t");
|
2023-10-25 11:19:14 -04:00
|
|
|
// DISPLAY DATA, sensor has only one decimal.
|
2022-09-18 13:13:39 -04:00
|
|
|
Serial.print(DHT.getHumidity(), 1);
|
|
|
|
Serial.print("\t\t");
|
|
|
|
Serial.print(DHT.getTemperature(), 1);
|
|
|
|
Serial.print("\t\t");
|
|
|
|
Serial.print(stop - start);
|
|
|
|
Serial.print("\t\t");
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case DHT20_OK:
|
|
|
|
Serial.print("OK");
|
|
|
|
break;
|
|
|
|
case DHT20_ERROR_CHECKSUM:
|
|
|
|
Serial.print("Checksum error");
|
|
|
|
break;
|
|
|
|
case DHT20_ERROR_CONNECT:
|
|
|
|
Serial.print("Connect error");
|
|
|
|
break;
|
|
|
|
case DHT20_MISSING_BYTES:
|
|
|
|
Serial.print("Missing bytes");
|
|
|
|
break;
|
|
|
|
case DHT20_ERROR_BYTES_ALL_ZERO:
|
|
|
|
Serial.print("All bytes read zero");
|
|
|
|
break;
|
|
|
|
case DHT20_ERROR_READ_TIMEOUT:
|
|
|
|
Serial.print("Read time out");
|
|
|
|
break;
|
|
|
|
case DHT20_ERROR_LASTREAD:
|
|
|
|
Serial.print("Error read too fast");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.print("Unknown error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(DHT.getHumOffset());
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(DHT.getTempOffset());
|
|
|
|
Serial.print("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-25 11:19:14 -04:00
|
|
|
// -- END OF FILE --
|