IreuN
Published © GPL3+

DHT22 Setup, with Simple Error Checking

My simple setup, with LED indication and averaging of humidity.

BeginnerShowcase (no instructions)6,668
DHT22 Setup, with Simple Error Checking

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
LED (generic)
LED (generic)
×3
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 221 ohm
Resistor 221 ohm
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wiring

Code

Code

C/C++
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

void setup() {
	Serial.begin(9600);
	Serial.println("Reading DHT22 data!");
	dht.begin();
	pinMode(12, OUTPUT); // Green
	pinMode(11, OUTPUT); // Red
}

void loop() {
	// Reading temperature or humidity takes about 250 milliseconds!
	// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
	float hum1 = dht.readHumidity();
	// Read temperature as Celsius
	float temp1 = dht.readTemperature();


	// Check if any reads failed and exit early (to try again).
	if (isnan(hum1) || isnan(temp1)) {
		digitalWrite(11, HIGH);
		digitalWrite(12, LOW);
		Serial.println("Error in reading sensor data!");
		while (isnan(hum1) || isnan(temp1)) {
			hum1 = dht.readHumidity();
			temp1 = dht.readTemperature();
		}
	}
	else {
		digitalWrite(11, LOW);
		digitalWrite(12, HIGH);
	}

	// Wait a few seconds between measurements.
	delay(3000);
	float hum2 = dht.readHumidity();
	float temp2 = dht.readTemperature();

	// Compute heat index in Celsius (isFahreheit = false)
	float hic = dht.computeHeatIndex(temp1, hum1, false);
	Serial.print("Humidity: ");
	Serial.print(round)(hum1 + hum2) / 2));
	Serial.print(" % ");
	Serial.print("Temperature: ");
	Serial.print((temp1 + temp2) / 2);
	Serial.print(" *C ");
	Serial.print("Heat index: ");
	Serial.print(round(hic));
	Serial.println(" *C ");
}

Credits

IreuN

IreuN

1 project • 1 follower
Thanks to ladyada.

Comments