Sub topic 4
How the Sensor Sends Data over One Wire
Assume you know the Arduino setup() and loop() functions, can upload a sketch, and understand basic voltage and timing. This chapter explains the DHT11 and DHT22 communication protocol. The Adafruit DHT library normally handles these timing details for you, but understanding them helps you diagnose wiring and reading errors.
What “one wire” means
A DHT sensor uses one data wire for communication with the Arduino. The wire is bidirectional: the Arduino sends a request, then the sensor sends a response on the same wire.
This is not the same as the Dallas 1-Wire protocol used by some other devices. DHT sensors use their own timing-based protocol. A typical DHT sensor has one device on the data wire, so you generally need a separate Arduino pin for each sensor.
The data line needs a pull-up resistor, commonly 4.7 kΩ to 10 kΩ, connected between the data line and the sensor's supply voltage. A pull-up resistor makes the idle line read as HIGH. Both the Arduino and the sensor can pull the line LOW during communication.
The request and response
The communication begins with a start signal from the Arduino:
- The Arduino pulls the data line LOW for at least 18 milliseconds.
- The Arduino releases the line and changes the pin to input mode.
- The sensor pulls the line LOW for about 80 microseconds.
- The sensor releases it HIGH for about 80 microseconds.
- The sensor transmits the measurement.
The Arduino must change direction quickly from output to input. If it keeps driving the line after the request, it can prevent the sensor from responding and may create electrical contention.
How a bit is encoded
The sensor sends 40 bits. Every bit begins with a LOW period of about 50 microseconds. The length of the following HIGH period represents the value:
- A HIGH period of about 26 to 28 microseconds represents
0. - A HIGH period of about 70 microseconds represents
1.
The receiver measures the HIGH period rather than treating the data wire like a normal serial connection. There is no clock wire. The timing of the signal is the clock.
The 40 bits are grouped into five bytes:
| Byte | Meaning |
|---|---|
| 1 | Humidity data, high byte |
| 2 | Humidity data, low byte |
| 3 | Temperature data, high byte |
| 4 | Temperature data, low byte |
| 5 | Checksum |
The checksum is the low eight bits of the sum of the first four bytes. A receiver can calculate this value and compare it with byte five. A mismatch indicates that the reading should be rejected.
DHT11 values usually use integer units with a separate decimal byte. DHT22 values use a higher-resolution 16-bit format. The library converts both formats into floating-point temperature and humidity values.
Why timing causes failures
The protocol depends on microsecond-level timing. Interrupts, long critical sections, weak wiring, and an incorrect pull-up resistor can distort a bit. A sensor may then return a checksum error or no result at all.
The DHT11 and DHT22 also need time between complete measurements. The library waits between reads because requesting data too quickly can produce stale or failed readings. The exact minimum interval depends on the sensor model, so follow the library's guidance rather than repeatedly triggering the protocol yourself.
> callout warning > Do not continuously poll a DHT sensor as fast as loop() runs. Read it at a safe interval, check for a valid result, and allow the data line to return to its idle HIGH state.
What the library hides
The Adafruit DHT library changes the pin mode, generates the start signal, measures the response pulses, reconstructs the five bytes, verifies the checksum, and converts the raw format. Your sketch can therefore call functions such as readHumidity() and readTemperature() without directly measuring microseconds.
This abstraction is useful, but the protocol explains common symptoms:
- No response often means incorrect power, ground, pin selection, or pull-up wiring.
- Intermittent readings often mean long wires, electrical noise, weak power, or timing interference.
- Repeated checksum failures often mean the signal timing or wiring is unreliable.
Exercise
Why does the DHT protocol need a pull-up resistor on its data line?
Show answer
The resistor keeps the shared data line HIGH when neither device is pulling it LOW, creating a stable idle state and allowing both devices to release the line safely.