Course project · driver and debugging

MSP432 + DHT11: a hand-written single-wire driver

Shanghai University · Embedded Systems Design (autumn 2021)

No sensor library — the 40-bit DHT11 frame is read with GPIO and Timer_A: 18 ms start pulse, 40 µs sampling point, 8-bit checksum. The code is split into a reusable driver and a UART demo, with the original single-file version kept for reference.

Sep 2021 - Jan 2022 C · MSP432 · DriverLib · UART Embedded Systems Design (SHU-23286056) ↗

Why bit-bang it

The DHT11 has no SPI or I²C interface: one data line carries 40 bits as pulse widths. The course required an MSP432P401R + DriverLib implementation without a sensor library, which makes it a compact exercise in GPIO, timers, interrupts and UART.

Wiring

DHT11 on 3.3 V and GND; data on P3.6 with a 4.7–10 kΩ pull-up to 3.3 V.
DHT11 on 3.3 V and GND; data on P3.6 with a 4.7–10 kΩ pull-up to 3.3 V.

The supply range is 3.0–5.5 V, so 3.3 V matches the MCU logic. The data line is open-drain; without a pull-up it floats or stays high, which is the usual reason a read fails.

Timing

A transaction has three parts:

  1. Start: the MCU pulls the line low for at least 18 ms, then releases it.
  2. Response: the sensor pulls low for ~80 µs, then high for ~80 µs.
  3. 40 bits: each bit starts with ~50 µs low; the high width encodes 0 or 1.
Only the high width differs: 26–28 µs is 0, 70 µs is 1. Sampling 40 µs after the falling edge sits between the two.
Only the high width differs: 26–28 µs is 0, 70 µs is 1. Sampling 40 µs after the falling edge sits between the two.

The 40 µs sample point is the heart of the driver: earlier and both bits are high; later and even a 1 may have ended.

Code structure

  • dht11.c: start signal, response detection, read_byte() bit decoding, checksum;
  • dht11.h: dht11_init(), dht11_read(), dht11_delay_ms();
  • main.c: 24 MHz DCO, UART A0 at 115200, one reading every 2 s printed as TxxC RHxx%.

delay_us(n) uses Timer_A: CCR0 is set to 24×n, the timer is started, and the interrupt only sets a flag. No busy-wait calibration, and changing the clock means changing one constant.

read_byte() waits for the 50 µs low to end, delays 40 µs, samples the line, then waits for the high to end. A guard counter prevents an infinite wait if the sensor disappears.

Traps in the original version

The course code was a single file. It worked, but:

  • time_count was not volatile, so the compiler could cache it and the wait loop could never finish;
  • flag was uninitialised, so a checksum failure returned a random value;
  • delay_us and delay_ms reconfigured Timer_A repeatedly, and delay_ms called delay_us once per millisecond;
  • the main loop had no error path: a failed read printed nothing.

The split version fixes these: volatile, initialisation to 0, -1 on failure, and an error message in main. The original file is kept in legacy/.

Results and limits

On an MSP432P401R LaunchPad with a DHT11, the UART prints stable readings and the LED toggles on every successful read. The limits are real:

  • 1 °C / 1 %RH resolution; at most one read per second;
  • the read blocks for about 20 ms; no DMA or input capture;
  • an 8-bit checksum, not a CRC;
  • long wires, a missing pull-up or an unstable supply cause checksum failures.

A next step would be input capture + DMA so the CPU can do other work during the 40-bit frame, but that is beyond the course brief.

Code and wiring notes are in the repository; the implementation story is in this post.

Technical notes

01 Bit-banging DHT11: reading 40 bits on an MSP432 Start signal, 26/70 µs zero/one discrimination, Timer_A delays, checksum, and a few traps in the original code.

Related skills

  • C
  • MSP432
  • DriverLib
  • UART