mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
110 lines
2.3 KiB
C++
110 lines
2.3 KiB
C++
//
|
|
// FILE: DHT20_offset.ino
|
|
// AUTHOR: Rob Tillaart
|
|
// PURPOSE: Demo for DHT20 I2C humidity & temperature sensor
|
|
// URL: https://github.com/RobTillaart/DHT20
|
|
//
|
|
// 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();
|
|
|
|
Wire.begin();
|
|
Wire.setClock(400000);
|
|
|
|
DHT.begin(); // ESP32 default pins 21 22
|
|
|
|
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)
|
|
{
|
|
// READ DATA
|
|
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");
|
|
// DISPLAY DATA, sensor has only one decimal.
|
|
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");
|
|
}
|
|
}
|
|
|
|
|
|
// -- END OF FILE --
|