Last active
December 28, 2015 12:49
-
-
Save dmccreary/7503212 to your computer and use it in GitHub Desktop.
Demonstration of reading digital temperature and relative humidity from DHT22 sensor with Arduino UNO. You can purchase the device here
* https://www.sparkfun.com/products/10167 Based on Ben Adams driver at http://www.sparkfun.com/products/10167 Using Arduino 1.5.4 IDE I got the following compile data:
Sketch uses 5,406 bytes (16%) of program st…
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
// you can get the header file at http://www.sparkfun.com/products/10167 | |
#include <DHT22.h> | |
// Only used for sprintf | |
#include <stdio.h> | |
// Data wire is plugged into port 2 on the Arduino | |
#define DHT22_PIN 2 | |
// Ben's original code suggested a 4.7K resistor between VCC and the data pin (strong pullup) | |
// But I found this was not needed since the pullup can be in software | |
// Setup a DHT22 instance | |
DHT22 myDHT22(DHT22_PIN); | |
void setup(void) | |
{ | |
// start serial port | |
Serial.begin(9600); | |
// Serial.println("DHT22 Library Demo"); | |
} | |
void loop(void) | |
{ | |
DHT22_ERROR_t errorCode; | |
// The sensor can only be read from every 1-2s, and requires a minimum | |
// nice to have a short warm-up after power-on. | |
delay(5000); | |
// Serial.print("Requesting data..."); | |
errorCode = myDHT22.readData(); | |
switch(errorCode) | |
{ | |
case DHT_ERROR_NONE: | |
// Serial.print("Got Data "); | |
// convert temperature from C to F | |
Serial.print(myDHT22.getTemperatureC() * 1.8 + 32); | |
Serial.print("F "); | |
Serial.print(myDHT22.getHumidity()); | |
Serial.println("%"); | |
// Alternately, with integer formatting which is clumsier but more compact to store and | |
// can be compared reliably for equality: | |
// char buf[128]; | |
// sprintf(buf, "Integer-only reading: Temperature %hi.%01hi C, Humidity %i.%01i %% RH", | |
// myDHT22.getTemperatureCInt()/10, abs(myDHT22.getTemperatureCInt()%10), | |
// myDHT22.getHumidityInt()/10, myDHT22.getHumidityInt()%10); | |
// Serial.println(buf); | |
break; | |
case DHT_ERROR_CHECKSUM: | |
Serial.print("check sum error "); | |
Serial.print(myDHT22.getTemperatureC() * 1.8 + 32); | |
Serial.print("F "); | |
Serial.print(myDHT22.getHumidity()); | |
Serial.println("%"); | |
break; | |
case DHT_BUS_HUNG: | |
Serial.println("BUS Hung "); | |
break; | |
case DHT_ERROR_NOT_PRESENT: | |
Serial.println("Not Present "); | |
break; | |
case DHT_ERROR_ACK_TOO_LONG: | |
Serial.println("ACK time out "); | |
break; | |
case DHT_ERROR_SYNC_TIMEOUT: | |
Serial.println("Sync Timeout "); | |
break; | |
case DHT_ERROR_DATA_TIMEOUT: | |
Serial.println("Data Timeout "); | |
break; | |
case DHT_ERROR_TOOQUICK: | |
Serial.println("Polled to quick "); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment