Sub topic 4
How the sensor sends data over one wire
This chapter looks at what happens on the data pin between your Arduino and a DHT11 or DHT22. It assumes you can wire the sensor and its pull-up resistor, upload a sketch, and use uint8_t, bit shifts and float in C++. The Adafruit library does the protocol work for you. Knowing the protocol still explains the sampling limit, the NaN results, and why some boards or wiring setups fail.
Why one wire is enough
The sensor has four pins (or three on a breakout board), but only one carries data. The same wire is used in both directions, and the two sides take turns. Neither side drives the line high. Both only pull it low, and a pull-up resistor of about 4.7 kΩ to 10 kΩ pulls it back up to the supply voltage when nobody is pulling.
This has three consequences:
- The idle state of the line is high.
- Information is carried by how long the line stays low or high, not by a clock signal. This is why the protocol is timing-critical.
- With no pull-up, the line never returns to high, and every read fails.
The three phases of a read
A read is a short conversation of roughly 4 ms. The Arduino always starts it, and the sensor never sends data on its own.
- Start signal. The Arduino pulls the line low. A DHT22 needs at least 1 ms of low, and a DHT11 needs at least 18 ms. The Adafruit library holds the line low for about 1 ms for a DHT22 and 20 ms for a DHT11. The Arduino then releases the line and switches the pin to input. The pull-up raises the line for 20 to 40 µs.
- Sensor response. The sensor pulls the line low for about 80 µs, then lets it go high for about 80 µs. This tells the Arduino that a sensor is present and that data follows.
- Data frame. The sensor sends 40 bits, one after another, most significant bit first.
How a single bit is encoded
Every bit begins with the sensor holding the line low for about 50 µs. The length of the following high pulse is the value:
| Pulse | Duration | Meaning |
|---|---|---|
| Low (bit separator) | about 50 µs | A bit is about to start |
| High, short | 26 to 28 µs | Bit is 0 |
| High, long | about 70 µs | Bit is 1 |
The Adafruit library does not compare the high pulse against a fixed 50 µs threshold. It measures the low pulse and the high pulse in CPU cycles and decides that the bit is 1 if the high pulse is longer than the low pulse. This keeps the decoding working at different clock speeds, such as 8 MHz, 16 MHz or the faster ESP32.
Each pulse measurement also has a timeout of about 1 ms. If a pulse never ends, the library gives up and the read fails. This is where most NaN results come from.
The 40-bit frame
The frame is five bytes:
| Byte | Content |
|---|---|
| 0 | Humidity, high byte |
| 1 | Humidity, low byte |
| 2 | Temperature, high byte |
| 3 | Temperature, low byte |
| 4 | Checksum |
The checksum is the low 8 bits of the sum of bytes 0 to 3. If the sum of the first four bytes (kept to 8 bits) does not equal byte 4, the read is corrupt and the library returns NaN.
The sensors interpret bytes 0 to 3 differently:
- DHT22: Bytes 0 and 1 form a 16-bit number that is humidity in tenths of a percent. Bytes 2 and 3 form temperature in tenths of a degree Celsius. The top bit of byte 2 is a sign bit, and the remaining 15 bits are the magnitude. The format is not two's complement.
- DHT11: Bytes 0 and 2 hold the whole-number part of humidity and temperature. Bytes 1 and 3 hold a decimal part, which is zero on most DHT11 parts. The DHT11 resolves only whole percent and whole degrees.
Decoding a frame yourself
The following sketch decodes a frame that is stored in memory. It needs no sensor, so you can run it on any board to check your understanding. The first frame is the worked example from the DHT22 datasheet. The second has the sign bit set.
bool checksumOk(const uint8_t f[5]) {
uint8_t sum = (uint8_t)(f[0] + f[1] + f[2] + f[3]);
return sum == f[4];
}
float decodeHumidity(const uint8_t f[5]) {
uint16_t raw = ((uint16_t)f[0] << 8) | f[1];
return raw / 10.0;
}
float decodeTemperatureC(const uint8_t f[5]) {
uint16_t raw = ((uint16_t)(f[2] & 0x7F) << 8) | f[3];
float t = raw / 10.0;
if (f[2] & 0x80) {
t = -t;
}
return t;
}
void printFrame(const uint8_t f[5]) {
if (!checksumOk(f)) {
Serial.println(F("Checksum mismatch"));
return;
}
Serial.print(F("Humidity: "));
Serial.print(decodeHumidity(f), 1);
Serial.print(F(" % Temperature: "));
Serial.print(decodeTemperatureC(f), 1);
Serial.println(F(" C"));
}
void setup() {
Serial.begin(9600);
const uint8_t warm[5] = {0x02, 0x92, 0x01, 0x0D, 0xA2};
const uint8_t cold[5] = {0x03, 0x20, 0x80, 0x65, 0x08};
const uint8_t broken[5] = {0x02, 0x92, 0x01, 0x0D, 0x00};
printFrame(warm);
printFrame(cold);
printFrame(broken);
}
void loop() {
}The output is:
Humidity: 65.8 % Temperature: 26.9 C
Humidity: 80.0 % Temperature: -10.1 C
Checksum mismatchThe cast to uint16_t before the shift matters. On an AVR board such as the Uno, int is 16 bits wide, so f[0] << 8 can overflow a signed int when f[0] is large.
Why the library blocks interrupts
The pulses are only tens of microseconds long, and the sketch has to time them by polling the pin. If an interrupt handler ran in the middle of a frame, it would stretch one pulse and corrupt a bit. So the library turns interrupts off while it reads the 40 bits, which takes about 4 ms. This has side effects:
millis()can lose a tick or two during a read, so timers drift slightly.- Serial data that arrives during the read can be lost on boards that use interrupts for the UART.
- Other interrupt-driven code, such as a fast encoder or a software PWM, can miss events.
The side effects are small if you read every 2 seconds. They matter if you read as often as the loop allows.
What the protocol implies for sampling
The sensor cannot measure continuously. It wakes when it receives the start signal, sends its frame, and goes back to sleep. The data it sends is from the last completed measurement, not a fresh one. The DHT22 datasheet recommends reading twice if you read infrequently, and using the second result.
The sampling limits come from the sensor's internal conversion time:
| Sensor | Minimum interval between reads |
|---|---|
| DHT11 | 1 second |
| DHT22 | 2 seconds |
The Adafruit library enforces a 2-second minimum. If you call readTemperature() again sooner, it returns the previous result without touching the wire. This is why a fast loop shows unchanged values rather than errors.
Common failure causes
Most read failures map directly to a step in the protocol:
- Missing or weak pull-up. The line never returns to high, so pulse timeouts follow. Add a 4.7 kΩ to 10 kΩ resistor between data and VCC, or use a breakout board that already has one.
- Long cable. Cable capacitance slows the rising edge and distorts pulse lengths. Keep the wire short, or use a lower pull-up value such as 4.7 kΩ.
- Wrong sensor type. Declaring a DHT22 as DHT11 makes the start signal too short or the decoding wrong, and values come out scaled incorrectly.
- Unstable supply. A sensor that browns out during its response sends a short or missing frame. Add a 100 nF capacitor across its supply pins if the supply is noisy.
- Reading too soon after power-up. The sensor needs about a second after power-on before it can answer.
- Checksum errors. These suggest electrical noise or timing disturbance, not a software error. Check the wiring, the supply, and whether anything else is using interrupts.
Exercise
A DHT22 sends the frame 0x01, 0x90, 0x80, 0x0F, 0x20. Is the checksum correct? What humidity and temperature does the frame report?
Show answer
The sum of the first four bytes is 0x01 + 0x90 + 0x80 + 0x0F = 0x120. The low 8 bits are 0x20, which matches byte 4, so the checksum is correct. Humidity is 0x0190 = 400, which is 40.0 %. For temperature, byte 2 is 0x80, so the sign bit is set and the magnitude is 0x000F = 15. The temperature is -1.5 C.
Exercise
Your sketch calls dht.readTemperature() every 200 ms and always prints the same value for ten reads in a row. The wiring is correct. What is the likely cause, and what does the sensor actually do?
Show answer
The library enforces a 2-second minimum between reads and returns the previous result when you call it sooner, without touching the wire. The sensor itself only measures when triggered, and it needs about 2 seconds (DHT22) between triggers. Read every 2 seconds or more.