DHT11 = 温湿度复合传感器:

数据帧 40 bit:
| 字节 | 内容 |
|---|---|
| 0 | 湿度整数 |
| 1 | 湿度小数(常为 0) |
| 2 | 温度整数 |
| 3 | 温度小数(常为 0) |
| 4 | 校验和(前 4 字节相加末 8 位) |

PG11 接 DQ,外接 4.7kΩ 上拉。

uint8_t dht11_read(uint8_t *temp, uint8_t *humi) {
dht11_init(); // 复位
for (int i = 0; i < 5; i++) {
dht11_buf[i] = dht11_read_byte();
}
if (dht11_buf[4] == (dht11_buf[0]+dht11_buf[1]+dht11_buf[2]+dht11_buf[3]) & 0xFF) {
*humi = dht11_buf[0];
*temp = dht11_buf[2];
return 0;
}
return 1; // 校验失败
}
串口每秒打印温度和湿度,呼吸产生的水汽看湿度变化。