RHT22 / RHT03

Over the last year my Garduino project has morphed into more of simply a weather station.  While I still have plans to activate a water valve with the Arduino, my failure in creating a reliable moisture sensor has led me to more or less indirectly guess the soil moisture my measuring the temperature, rain, and humidity.

To measure the temperature and humidity, I’m using the RHT03 (referred to as an RHT22 and DHT-22 as well), available from Sparkfun, and on eBay.


RHT03 in breadboard connected to the Saleae Logic Analyzer

Most of my work on the RHT03 is based on the work of Craig Ringer and Nethoncho.   The RHT03 is a bit of an odd device, with kind of a non-standard signalling protocol.  All signalling is done over 1 data line, and the Arduino sets that signal line to an OUTPUT and pulls the line low for about 3ms, and then high for about 30us.  At that point, the Arduino sets the data line pin to an INPUT, and the RHT03 takes control of that pin; pulling the line low for 80us, then high for 80us, and then sending the temperature and humidity in a series of pulses.  High pulses of 26us represent a 0 and 80us represents a 1.  You can try to decipher the whole protocol by reading the datasheet, if you are not too picky about grammar (and if you are reading my blog, you must not be). The following is the code I am using:

#define RHT22_PIN 7
unsigned int temp;
unsigned int humidity;
void setup()
void loop()
 Serial.println("Reading sensor...");

 boolean success = readRHT03();
 if (success) {
   char buf[128];
   sprintf(buf, "Unit 1 Reading: %i.%i degrees C at %i.%i relative humidity", temp/10, abs(temp%10), humidity/10, humidity%10);
boolean readRHT03()
 unsigned int rht22_timings[88];


 pinMode(RHT22_PIN, OUTPUT);
 digitalWrite(RHT22_PIN, LOW);

 digitalWrite(RHT22_PIN, HIGH);
 pinMode(RHT22_PIN, INPUT);

 int state=digitalRead(RHT22_PIN);
 unsigned int counter=0;
 unsigned int signalLineChanges=0;
 while (counter!=0xffff)
     rht22_timings[signalLineChanges] = TCNT1;
 boolean errorFlag=false;
 if (signalLineChanges != 83)

 return !errorFlag;
void initRHT03()
 //for (int i = 0; i < 86; i++) { rht22_timings[i] = 0; }


// DEBUG routine: dump timings array to serial
void debugPrintTimings(unsigned int rht22_timings[]) { // XXX DEBUG
for (int i = 0; i < 88; i++) { // XXX DEBUG
 if (i%10==0) { Serial.print("\n\t"); }
 char buf[24];
 sprintf(buf, i%2==0 ? "H[%02i]: %-3i " : "L[%02i]: %-3i ", i, rht22_timings[i]);
 Serial.print("\n"); // XXX DEBUG
boolean getHumidityAndTemp(unsigned int rht22_timings[])
  // 25us = 400, 70us = 1120;
  for(int i=0;i<32;i+=2)

      humidity|=(1 << (15-(i/2)));
  for(int i=0;i<32;i+=2)


  int cksum=0;
  for(int i=0;i<16;i+=2)


  int cChksum=((humidity >> 8)+(humidity & 0xff) + (temp >> 8) + (temp &0xff)) & 0xFF;  
  if(temp & 0x8000)
    temp=(temp & 0x7fff)*-1;
  if(cChksum == cksum)
    return true;
  return false;

Signal handling is done in the readRHT03 function.  After sending the start sending signal, the Arduino uses Timer/Counter 1 to time the incoming pulses.  Originally, I was trying to use micros() to measure the timing, but I got some very erratic results.  This gave me the opportunity to use my new Saleae Logic Analyzer.  As you can see in the code, I have Pin 6 follow the state of the data input from the RHT03.  Normally, there is a 15us delay between the time the RHT03 changes state and the time Pin 6 changes state (the time to execute the if statement and the digitalWrite()), but sometimes it is longer.  The highlighted pulse on Channel 1, though, shows the delay is at least 10us longer.  To make a long story short, this is caused by interrupts in the Arduino, and turning off the interrupts before I start timing the pulses fixes that problem.  Functions like millis() and micros() need interrupts to be update, though, so I could not use them with the interrupts disabled.  This means I had to use the hardware timer/counter to time the pulse length, which is what the references to the TCCR1A, TCCR1B and TCNT1 do.  Additionally, to measure the time the interrupts are disabled, I set Pin 5 (Channel 2) to go high when the interrupts are disabled and low when they are re-enabled, which gave me about 4ms of “interrupt-less” time.  Since my weather station is going to be polled at 60 second intervals, I now know that the timers won’t be running for about 4ms of that time, so I have to set my timers to 59996 to get a 60 second poll.

Anyone using this code should pull out all the references to Pin 5 and 6, unless they want to use them for debugging as well.

Note the short pulse caused by the Arduino handling other interrupts.

The other thing to note is the temperature is pretty inaccurate when the device is in direct sunlight.  My original design had the RHT03 sharing the waterproof container of my MAX44009 light sensor, but even though it is open to the air at the bottom on the container, the temperature inside the container got well above 110°F on bright sunny 90°F+ days.  I’m now extending the cable to the RHT03 and putting it in it’s own container, shaded by the solar cell.  I will update on how well that works.

RHT03 Mount

Mount for the RHT03 to keep it out of the rain and the sun.


Sunny Location

The RHT03 is in the container on the left with the blue top. This container also houses the MAX44009 sun sensor.

Shade RGT03

New location of the RHT03, under the black cover.


10 thoughts on “RHT22 / RHT03

  1. I would like you to help me… I need an arduino code which will sense humidity and temperature from DHT22. I am using PIC16F628A and 16×2 lcd display in addition.

  2. I need your help… I am new to arduino
    …. can u send me arduino program for anemometer measuring speed direction using potentiometer and tipping bucket rain gauge output should be in 16*2LCD display

  3. I am new to arduino
    …. can u send me arduino program for anemometer measuring speed direction using potentiometer and tipping bucket rain gauge output should be in 16*2LCD display

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s