In this video lesson we show you how to measure temperature and humidity using the DHT11 sensor. For your convenience, we include the code below which we develop in the above video. Enjoy!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
// ==================================================================== // DISCLAIMER: // This code is provided as-is for educational and experimental // purposes only. The author makes no representations or warranties of // any kind concerning the safety, suitability, or accuracy of this // code. Use at your own risk. The author assumes no liability for any // damages, system failures, security breaches, or network issues // resulting from the use or implementation of this script. // ==================================================================== #include <DHT.h> #define DHTPIN 11 #define DHTTYPE DHT11 DHT myDHT(DHTPIN,DHTTYPE); int br=115200; int setUpDelay=1000; int measDelay=1000; float tempC; float tempF; float hum; float HIF; float HIC; String message; void setup() { // put your setup code here, to run once: Serial.begin(br); myDHT.begin(); delay(setUpDelay); } void loop() { // put your main code here, to run repeatedly: tempC=myDHT.readTemperature(false); tempF=myDHT.readTemperature(true); hum=myDHT.readHumidity(); if (isnan(tempC) || isnan(tempF) || isnan(hum)){ Serial.println("Failed to Read Data on DHT11"); return; } HIC=myDHT.computeHeatIndex(tempC,hum,false); HIF=myDHT.computeHeatIndex(tempF,hum,true); message="Temp: "+String(tempC)+" deg C "+String(tempF)+" deg F Humidity: "+String(hum)+" % Heat Index: "+String(HIC)+" deg C "+String(HIF)+" deg F."; Serial.println(message); // Serial.print("Temp: "); // Serial.print(tempC); // Serial.print(" deg C "); // Serial.print(tempF); // Serial.print(" deg F "); // Serial.print("Humidity: "); // Serial.print(hum); // Serial.print(" % "); // Serial.print("Heat Index: "); // Serial.print(HIC); // Serial.print(" deg C "); // Serial.print(HIF); // Serial.println(" deg F "); delay(measDelay); } |