Created
June 16, 2024 04:24
-
-
Save creationix/8567b257f6209948da957714968a9b7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example testing sketch for various DHT humidity/temperature sensors | |
// Written by ladyada, public domain | |
#include "Grove_Temperature_And_Humidity_Sensor.h" | |
#define PIN_GROVE_POWER 15 // https://github.com/esphome/issues/issues/223 | |
// Uncomment whatever type you're using! | |
#define DHTTYPE DHT11 // DHT 11 | |
// #define DHTTYPE DHT22 // DHT 22 (AM2302) | |
//#define DHTTYPE DHT21 // DHT 21 (AM2301) | |
//#define DHTTYPE DHT10 // DHT 10 | |
//#define DHTTYPE DHT20 // DHT 20 | |
/*Notice: The DHT10 and DHT20 is different from other DHT* sensor ,it uses i2c interface rather than one wire*/ | |
/*So it doesn't require a pin.*/ | |
#define DHTPIN 13 // what pin we're connected to(DHT10 and DHT20 don't need define it) | |
DHT dht(DHTPIN, DHTTYPE); // DHT11 DHT21 DHT22 | |
//DHT dht(DHTTYPE); // DHT10 DHT20 don't need to define Pin | |
// Connect pin 1 (on the left) of the sensor to +5V | |
// Connect pin 2 of the sensor to whatever your DHTPIN is | |
// Connect pin 4 (on the right) of the sensor to GROUND | |
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor | |
#if defined(ARDUINO_ARCH_AVR) | |
#define debug Serial | |
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) | |
#define debug SerialUSB | |
#else | |
#define debug Serial | |
#endif | |
void setup() { | |
debug.begin(115200); | |
debug.println("DHTxx test!"); | |
Wire.begin(); | |
/*if using WIO link,must pull up the power pin.*/ | |
pinMode(PIN_GROVE_POWER, OUTPUT); | |
digitalWrite(PIN_GROVE_POWER, 1); | |
dht.begin(); | |
} | |
void loop() { | |
float temp_hum_val[2] = {0}; | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
if (!dht.readTempAndHumidity(temp_hum_val)) { | |
debug.print("{ \"humidity\": "); | |
debug.print(temp_hum_val[0]); | |
debug.print(", \"temperature\": "); | |
debug.print(temp_hum_val[1]); | |
debug.println(" }"); | |
} else { | |
debug.println("\"Failed to get temprature and humidity value.\""); | |
} | |
delay(1500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment