Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

Virtuabotix DHT22 Pinout and Coding guide

Questions & Answers

How the heck do I hook up the DHT11 & DHT22?

Good question, if you lost your pin-out guide, or don’t have it handy here is how to put it together.

With the grated (non-labeled/non-epoxied) side facing you. Do not reverse bias this sensor (The DHT22 is particularly sensitive to reverse bias)

PinPurposeDetails
VccProvides power to the device (works with 3.3 V or 5 V)Regulated Power (leftmost pin)
ReadoutAllows reading of sensor data through PWM trainSecond to leftmost pin (directly to the right of the Vdd pin)
NCNot ConnectedDo not connect to second from rightmost pin
GroundCommon groundRight most pin

DHT22_PINOUT[1]

What communication protocol does the DHT22 use?

The DHT22 does not use a standard communication protocol like I2C, SPI, or Serial. It is a single wire device that communicates using pulse wave modulation. 

What this means is that the DHT22 relies on the accuracy of your clock, and a relatively slow data rate to transmit the temperature and humidity to you. 

We will be putting up scope shots in the near future, but here is a description of the protocol and the data it carries: 

Basically the protocol starts with a request from the microcontroller that is initiated by a relatively long pulse to ground (16 to 20 Milliseconds). The DHT11 then begins to send data back to the Microcontroller. The data is translated into binary by taking short pulses and storing them as zeros, while long pulses are stored as ones. 

That data is then stored in an array with the following significance: 

BYTE[0] Stores the Humidity Integer BYTE[1] Stores the Humidity Decimal value BYTE[2] Stores the Temperature Integer BYTE[3] Stores the Temperature Decimal Value BYTE[4] Stores the CHECKSUM which is literally bytes 0 through 3 added together.

DHT22_datastream[1]

Translated from datasheet

DHT22_timing[1]

Translated from datasheet

Everyone else puts a pullup resistor on the data pin, why don’t you?

Contrary to popular belief a pull-up resistor is not required for the DHT11 or DHT22 to work with our libraries.

Yes other websites, libraries, and datasheets all indicate the use of a pullup resistor; however we don’t like adding extra cost to your projects so we made the needed adjustments to ensure that the DHT11 and DHT22 both worked without the pullup with our libraries.The Pullup resistor is a hardware solution to a software solvable problem.

If however you want to use the pullup resistor anyways here is a link to some code, and detailed information about how to work with one. Checkout this article by Embedded Lab if you still want to use a pullup

Versalino™ Libraries & Projects

Libraries

Library This guide is meant to provide all of the needed resources to help you get started coding with the Virtuabotix DHT22 Library.

Guides

“Simplest” Possible Code (Short answer: Just use the library)

If you are not a fan of Arduino Libraries, and you don’t need all the features you can just use this simple function to read out data from the DHT11. You may be disappointed by the level of complexity of this code, but one wire digital communication isn’t as simple some other technologies. I strongly recommend that you simply take advantage of the library that we have written since it handles all of the communication and verification automatically, but if you are curious this is the meat and potatoes of the program.

int humidity=0; //this is where our humidity will be stored
int temperature=0;//this is where our temperature will be store

//pass function the pin you want to read from
//it will return one of the following codes:

// 0 : OK <--- you want it to be this, but my experience is //on occasion you will get a checksum or timeout error but giving //it another read attempt will usually resolve the problem.// -1 : checksum error // -2 : timeoutint read(int pin) { // BUFFER TO RECEIVE uint8_t bits[5]; uint8_t cnt = 7; uint8_t idx = 0;// EMPTY BUFFER for (int i=0; i< 5; i++) bits[i] = 0;// REQUEST SAMPLE pinMode(pin, OUTPUT); digitalWrite(pin, LOW); delay(18); digitalWrite(pin, HIGH); delayMicroseconds(40); pinMode(pin, INPUT);// ACKNOWLEDGE or TIMEOUT unsigned int loopCnt = 10000; while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return -2;loopCnt = 10000; while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return -2;// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++) { loopCnt = 10000; while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return -2;unsigned long t = micros();loopCnt = 10000; while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return -2;if ((micros() - t) > 40) bits[idx] |= (1 << cnt); if (cnt == 0) // next byte? { cnt = 7; // restart at MSB idx++; // next byte! } else cnt--; }// WRITE TO RIGHT VARS double humidity = word(bits[0], bits[1]) *.1; //calculates and stores the humidityuint8_t sign = 1; if (bits[2] & 0x80) // negative temperature { bits[2] = bits[2] & 0x7F;//negative temp adjustments sign = -1; } double temperature = sign * word(bits[2], bits[3]) * 0.1; //temp calculation and storageuint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];//sum valuesif (bits[4] != sum) return -1;//failed checksum return 0;//great success! }

 

The Library In Action

Examples

As simple as it gets, minimum for reading a temperature in degrees Celcius.

#include

dht22 myDHT22 = dht22(7); //using pin 7 as our IO

void setup()
{
}

void loop()
{
myDHT22.read();
double myDegreesC= myDHT22.celcius();
}

Hardware Compatibility

Hardware Compatibility

  • DHT22 VERIFIED

5 Responses to “Virtuabotix DHT22 Pinout and Coding guide”

  1. Hi, thanks for a great tutorial and library.

    Is it possible to change the resolution of the sensor?
    At the moment it prints out example “26.500000000” a lot of decimals. Can i get it to print out only one decimal “26.5”?

    I’ve tried looking through the library but i am a bit afraid to change anything messing up the library??

    • No problem, I should probably add a precision setting to the next library release. When you need to change the number of decimal places of a floating point Serial output on the Arduino IDE all you need to do is pass the number of decimal places you want in the function argument:

      Serial.println(virtuabotix_DHT22_readings, 1); // should print 26.5 (1 means one decimals) in your case

      An alternative solution is to cast the floating point number or double into an int thereby truncating (getting rid of) all decimal places.

      Serial.println((int)virtuabotix_DHT22_readings); // should print 26 (casting as int eliminates decimals) in your case

      Hope that helps!

  2. It’s a very good idea to modify array before checking CRC đŸ™‚

Leave a Reply