ATmega328P from scratchPart 12 of 12

Build note

DHT11: a protocol built out of delays

Every bus so far had a peripheral behind it. The DHT11 has none. The protocol lives entirely in the timing of one wire, so the code has to toggle a pin and count microseconds itself.

It is a conversation where the other person answers in the length of their pauses rather than their words. A short pause means no, a long pause means yes.

The exchange in three stages

Wake-up. The microcontroller drives the line low for at least 18 ms, then releases it high for 20 to 40 microseconds.

Handshake. The sensor pulls the line low for 80 microseconds, then high for 80 microseconds. That pair says “I am here”.

Forty bits. Every bit begins with a 50 microsecond low. The high pulse that follows carries the value:

High pulse Meaning
About 27 microseconds 0
About 70 microseconds 1

The code does not measure the pulse. It waits 30 microseconds after the line goes high and samples it. If the line is still high, the bit is a 1. If it has already fallen, the bit is a 0. Choosing a sample point between the two possible lengths is what makes the read work without a timer.

The five bytes

Byte Contents
1 Humidity, integer
2 Humidity, decimal
3 Temperature, integer
4 Temperature, decimal
5 Checksum

The checksum is the low eight bits of the sum of the first four bytes. If it does not match, the whole sample is thrown away. That is the best property of this sensor for learning: it fails loudly instead of returning a plausible wrong number.

Switching the pin’s direction

One wire, two devices, and only one drives at a time:

DDRD |=  (1 << PD2);     // output, drive the wake-up pulse
PORTD &= ~(1 << PD2);    // low for 18 ms
DHT11_DDR &= ~(1 << DHT11_BIT);  // back to input, sensor takes over

That direction switch is the whole trick. DDRD decides who is allowed to drive, and PORTD enables the pull-up when the pin is an input.

The timing is approximate, and that is fine

The waits are a loop around _delay_us(1), which costs more than a microsecond per pass because each pass is several instructions. The fixed 30 microsecond sample is closer, but still not exact.

That is acceptable because the margins are tens of microseconds wide and the sample point only has to land between 27 and 70. It would not be acceptable for a protocol with a two microsecond margin, which is why the SPI and I2C parts use hardware peripherals instead.

One consequence: an interrupt that fires during a read can stretch a delay and corrupt a bit. For a reliable driver, disable interrupts around the transfer, or accept occasional checksum failures and retry.

A comment in the source that is backwards

The prose comment near the bottom of dht11.c says a 1 bit lasts 27 microseconds and a 0 bit lasts 70. That is inverted. The timing table a few lines below it, the sampling logic in dht11_read_bit(), and the datasheet all agree that 27 means 0 and 70 means 1.

The code is correct. Only that one sentence is wrong. It is worth knowing before it sends you looking for a bug that is not there.

What the sensor needs around it

  • At least one second between reads, and about a second after power-up before the first read.
  • A 10 k resistor from data to 5 V is recommended. The internal pull-up helps, but the external resistor keeps the line stable while the sensor drives it.
  • Short wires. The protocol is timing, and long wires add capacitance.

Why this is the last part

Every earlier part used a peripheral to hide the timing. This one does not, which is why it comes last. Once you have made a bit-banged protocol work, the hardware buses look like the convenience they are.

How this was made

  • Written by: Rupesh, from his own c-10-sensors/01-dht11 driver and notes, with AI drafting and editing.
  • Timing: from the DHT11 datasheet excerpt in the source file.
  • AI-assisted sections: structure and copy editing.