3. DHT11 Sensor Interfacing

3.1 Sensor Fundamentals 
 A sensor captures physical phenomena from the real world and converts them into electrical signals (analog or digital) for computation. 
 For DHT11 specifically: 
 
 It measures temperature and humidity. 
 It is commonly used in educational and basic room-monitoring applications. 
 Typical module-noted range: 
 Temperature: 0 to 50 deg C 
 Humidity: 20% to 90% RH 
 
 3.2 DHT11 Variants and Wiring 
 DHT11 can be found as: 
 
 bare sensor package, 
 breakout module. 
 
 Breakout modules are typically easier to wire and often include support components. 
 3.3 DHT11 Protocol and Timing 
 DHT11 uses a single-wire, pulse-width-based digital protocol. Communication sequence: 
 
 MCU sends start signal (low pulse around 18-20 ms, then high). 
 DHT11 sends response pulse. 
 DHT11 transmits 40-bit serial data. 
 Bit value is represented by pulse width: 
 
 
 short high pulse = 0 
 long high pulse = 1 
 
 3.4 40-bit Data Format 
 
 
 
 Byte 
 Data Field 
 
 
 
 
 1 
 Humidity integer part 
 
 
 2 
 Humidity decimal part 
 
 
 3 
 Temperature integer part 
 
 
 4 
 Temperature decimal part 
 
 
 5 
 Checksum 
 
 
 
 Checksum rule: 
 Checksum = (Byte1 + Byte2 + Byte3 + Byte4) & 0xFF 
 3.5 Why Accurate Delay Matters 
 DHT11 decoding depends on timing precision. If delays are too short or too long: 
 
 start handshake may fail, 
 bit sampling can shift, 
 decoded bytes can become invalid, 
 checksum mismatch may occur. 
 
 3.6 DHT11 Assembly Example 
 ;------------------------
; Assembly Code
;------------------------
#define __SFR_OFFSET 0x00
#include "avr/io.h"
;------------------------
.global main
;=================================================================
main:
;------------
 LDI R17, 0xFF
 OUT DDRC, R17 ;set port C for o/p
 OUT DDRD, R17 ;set port D for o/p
;-----------------------------------------------------------------
agn:RCALL delay_2s ;wait 2s for DHT11 to get ready
;-----------------------------------------------------------------
;send start signal
;------------
 SBI DDRB, 1 ;pin PB0 as o/p
 CBI PORTB, 1 ;first, send low pulse
 RCALL delay_20ms ;for 20ms
 SBI PORTB, 1 ;then send high pulse
;-----------------------------------------------------------------
;wait for response signal
;---------------
 CBI DDRB, 1 ;pin PB0 as i/p
w1: SBIC PINB, 1
 RJMP w1 ;wait for DHT11 low pulse
w2: SBIS PINB, 1
 RJMP w2 ;wait for DHT11 high pulse
w3: SBIC PINB, 1
 RJMP w3 ;wait for DHT11 low pulse
;-----------------------------------------------------------------
 RCALL DHT11_reading ;read humidity (1st byte of 40-bit data)
 MOV R19, R18
 RCALL DHT11_reading
 RCALL DHT11_reading ;read temp (3rd byte of 40-bit data)
;-----------------------------------------------------------------
 OUT PORTD, R19 ;o/p temp byte to port C
 OUT PORTC, R18 ;o/p humidity byte to port D
 RJMP agn ;go back & get another sensor reading
;=================================================================
DHT11_reading:
 LDI R17, 8 ;set counter for receiving 8 bits
 CLR R18 ;clear data register
 ;-------------------------------------------------------
w4: SBIS PINB, 1
 RJMP w4 ;detect data bit (high pulse)
 RCALL delay_timer0 ;wait 50us then check bit value
 ;-------------------------------------------------------
 SBIS PINB, 1 ;if bit=1, skip next instruction
 RJMP skp ;else bit=0
 SEC ;C=1
 ROL R18 ;shift in 1
 RJMP w5
skp:LSL R18 ;shift in 0
 ;-------------------------------------------------------
w5: SBIC PINB, 1
 RJMP w5 ;wait for low pulse
 ;-------------------------------------------------------
 DEC R17
 BRNE w4
 RET