Contributed by WangChong, curated by Ai-Thinker
List of components needed:
| DHT11 | 1 |
|---|---|
| Ai-M61-32S | 1 |
Expected result: The Ai-M61-32S sends the temperature and humidity data to the host computer via the serial port. 
Timing and code:
1. Initialize the DHT11 — the data line is controlled by the MCU to go from high to low and stay low for at least 18ms, then the MCU drives it from low to high and holds it for 20-40us. 
The code:
// initialize the GPIO pin
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, DHT_PIN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
// send the start signal
bflb_gpio_set(gpio, DHT_PIN);
bflb_mtimer_delay_ms(50);
bflb_gpio_reset(gpio, DHT_PIN);
bflb_mtimer_delay_ms(20);
bflb_gpio_set(gpio, DHT_PIN);
bflb_mtimer_delay_us(30);
**2. Receive the response from the DHT11**
<img src='/img/bbsdiy/ai_m6x/2c7a726136f56953.png' alt='' width='713' />The code:
// read the DHT11 response
while (bflb_gpio_read(gpio, DHT_PIN) == 1)
{
}
while (bflb_gpio_read(gpio, DHT_PIN) == 0)
{
}
while (bflb_gpio_read(gpio, DHT_PIN) == 1)
{
}
**3. Data reading** — After the above, the DHT will send 40 bits of data, including 16 bits of humidity data, 16 bits of temperature data and 8 bits of checksum data.
<img src='/img/bbsdiy/ai_m6x/e884e1735a17f69e.png' alt='' width='480' />The code:
// read the temperature and humidity data, 40 bits in total
// 8bit humidity integer + 8bit humidity decimal
// +8bit temperature integer + 8bit temperature decimal
// +8bit checksum
uint8_t data[5] = {0};
for (int i = 0; i < 5; i++)
{
data[i] = 0;
for (int j = 0; j < 8; j++)
{
while (bflb_gpio_read(gpio, DHT_PIN) == 0)
{
}
bflb_mtimer_delay_us(30);
if (bflb_gpio_read(gpio, DHT_PIN) == 1)
{
data[i] |= (1 << (7 - j));
while (bflb_gpio_read(gpio, DHT_PIN) == 1)
{
}
}
}
}4. Converting the result
// turn off GPIO
bflb_gpio_deinit(gpio, DHT_PIN);
// parse the data
uint8_t humidity_integer = data[0];
uint8_t humidity_decimal = data[1];
uint8_t temperature_integer = data[2];
uint8_t temperature_decimal = data[3];
// calculate the decimal part
float humidity = humidity_integer + (humidity_decimal / 10.0);
float temperature = temperature_integer + (temperature_decimal / 10.0);
// data valid
LOG_I("Humidity: %.1f%%\n", humidity);
LOG_I("Temperature: %.1fC\n", temperature);Complete code:
#include "DHT11.h"
#include "bflb_gpio.h"
#include "bflb_mtimer.h"
#include "log.h"
#define DHT_PIN 33
struct bflb_device_s *gpio;
void DHT_START()
{
// initialize the GPIO pin
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, DHT_PIN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
// send the start signal
bflb_gpio_set(gpio, DHT_PIN);
bflb_mtimer_delay_ms(50);
bflb_gpio_reset(gpio, DHT_PIN);
bflb_mtimer_delay_ms(20);
bflb_gpio_set(gpio, DHT_PIN);
bflb_mtimer_delay_us(30);
// switch to input mode and wait for the DHT11 response
bflb_gpio_init(gpio, DHT_PIN, GPIO_INPUT | GPIO_PULLUP | GPIO_SMT_EN);
// read the DHT11 response
while (bflb_gpio_read(gpio, DHT_PIN) == 1) {
}
while (bflb_gpio_read(gpio, DHT_PIN) == 0) {
}
while (bflb_gpio_read(gpio, DHT_PIN) == 1) {
}
// read the temperature and humidity data, 40 bits in total
// 8bit humidity integer + 8bit humidity decimal
// +8bit temperature integer + 8bit temperature decimal
// +8bit checksum
uint8_t data[5] = {0};
for (int i = 0; i < 5; i++) {
data[i] = 0;
for (int j = 0; j < 8; j++) {
while (bflb_gpio_read(gpio, DHT_PIN) == 0) {
}
bflb_mtimer_delay_us(30);
if (bflb_gpio_read(gpio, DHT_PIN) == 1) {
data[i] |= (1 << (7 - j));
while (bflb_gpio_read(gpio, DHT_PIN) == 1) {
}
}
}
}
// turn off GPIO
bflb_gpio_deinit(gpio, DHT_PIN);
// parse the data
uint8_t humidity_integer = data[0];
uint8_t humidity_decimal = data[1];
uint8_t temperature_integer = data[2];
uint8_t temperature_decimal = data[3];
// calculate the decimal part
float humidity = humidity_integer + (humidity_decimal / 10.0);
float temperature = temperature_integer + (temperature_decimal / 10.0);
// data valid
LOG_I("Humidity: %.1f%%\n", humidity);
LOG_I("Temperature: %.1fC\n", temperature);
}Attachments: 
DHT11-chinese.pdf(677.52 KB, downloads: 2)
Attachments: 
DHT11.zip(3.45 KB, downloads: 7)
PS: in the code above, the DHT11 data line is connected to GPIO33 of the Ai-M61-32S-Kit; you can modify it as needed.
Have questions?
For other questions, please visit the unified discussion area: Ai-Thinker Discussions

