In this guide, you will learn how to interface DHT11 with STM32 microcontroller to read temperature and humidity values from the sensor. The guide covers step-by-step instructions on how to connect the sensor to the MCU, write code to communicate with the sensor, and convert the raw data into actual temperature and humidity values. By following this guide, you will be able to build a simple yet effective temperature and humidity monitoring system using DHT11 and STM32.

⚠️ Disclaimer ⚠️

Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.

Advertisements

All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.

If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.

Need help or have questions? Leave a comment below — the author is always happy to assist!

Interfacing DHT11 with STM32

The DHT11 is a popular temperature and humidity sensor that can be easily interfaced with an STM32 microcontroller. Here are the steps to interface DHT11 with STM32:

  1. Connect the DHT11 sensor to the STM32 microcontroller. The sensor has three pins: VCC, GND, and DATA. Connect VCC to 5V, GND to GND, and DATA to a GPIO pin on the STM32.
  2. Initialize the GPIO pin as an input pin. The STM32 CubeMX tool can be used to generate the initialization code for the GPIO pin.
  3. To read data from the DHT11 sensor, send a start signal to the sensor by setting the GPIO pin low for at least 18 milliseconds, then set the pin high for 20-40 microseconds.
  4. Once the start signal is sent, the DHT11 will respond by sending 40 bits of data. Each bit is represented by a low pulse of 50 microseconds followed by a high pulse of 26-28 microseconds.
  5. Read the 40 bits of data from the DHT11 sensor and convert it into temperature and humidity values. The first 16 bits represent the humidity value, the next 16 bits represent the temperature value, and the last 8 bits represent a checksum value.

But first you need to know the DHT11 sensor.

About DHT11:

The DHT11 is a low-cost digital temperature and humidity sensor that is commonly used in electronic projects. It consists of a capacitive humidity sensor and a thermistor for temperature sensing and provides reliable readings of temperature and relative humidity. The DHT11 sensor is popular because it is easy to use, requires no additional components, and can be easily interfaced with microcontrollers like Arduino, STM32, and Raspberry Pi. It is commonly used in applications such as weather stations, home automation systems, and environmental monitoring systems.

Datasheet of DHT11

Circuit diagram:

Here is the circuit diagram you can follow.

Example code:

Here’s an example code snippet in C using HAL (Hardware Abstraction Layer) libraries for STM32 to interface with the DHT11 sensor:

#define DHT11_GPIO_PORT GPIOA
#define DHT11_GPIO_PIN GPIO_PIN_0

uint8_t data[5];

// function to read data from DHT11
void DHT11_ReadData(void)
{
    uint8_t i;
    
    // send start signal
    HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_RESET);
    HAL_Delay(20);
    HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_SET);
    
    // wait for response
    HAL_Delay(40);
    
    // initialize data array
    memset(data, 0, sizeof(data));
    
    // read 40 bits of data
    for(i=0; i<40; i++)
    {
        // wait for low pulse
        while(!HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
        
        // wait for high pulse
        uint32_t t = 0;
        while(HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
        {
            t++;
            HAL_Delay(1);
        }
        
        // store bit value in data array
        if(t > 30) 
            data[i/8] |= (1 << (7 - (i % 8)));
    }
    
    // verify checksum
    if(data[4] == (data[0] + data[1] + data[2] + data[3]))
    {
        // convert temperature and humidity values
        uint8_t hum = data[0];
        uint8_t temp = data[2];
        
        // do something with temperature and humidity values
        // ...
    }
}

Note that the above code snippet is just an example, and it may need to be modified to work with your specific STM32 microcontroller and development environment.

  1. To convert the raw data obtained from DHT11 into actual temperature and humidity values, use the following formulas: Humidity = (data[0] << 8 | data[1]) / 10.0; Temperature = ((data[2] & 0x7F) << 8 | data[3]) / 10.0; if (data[2] & 0x80) Temperature *= -1; Note that the temperature value is a signed value and the sign is indicated by the most significant bit of data[2].
  2. You can now use the temperature and humidity values in your application as needed.

Here’s an updated example code snippet that includes the data conversion:

#define DHT11_GPIO_PORT GPIOA
#define DHT11_GPIO_PIN GPIO_PIN_0

uint8_t data[5];

// function to read data from DHT11
void DHT11_ReadData(float *temperature, float *humidity)
{
    uint8_t i;
    
    // send start signal
    HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_RESET);
    HAL_Delay(18);
    HAL_GPIO_WritePin(DHT11_GPIO_PORT, DHT11_GPIO_PIN, GPIO_PIN_SET);
    
    // wait for response
    HAL_Delay(40);
    
    // initialize data array
    memset(data, 0, sizeof(data));
    
    // read 40 bits of data
    for(i=0; i<40; i++)
    {
        // wait for low pulse
        while(!HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN));
        
        // wait for high pulse
        uint32_t t = 0;
        while(HAL_GPIO_ReadPin(DHT11_GPIO_PORT, DHT11_GPIO_PIN))
        {
            t++;
            HAL_Delay(1);
        }
        
        // store bit value in data array
        if(t > 30) 
            data[i/8] |= (1 << (7 - (i % 8)));
    }
    
    // verify checksum
    if(data[4] == (data[0] + data[1] + data[2] + data[3]))
    {
        // convert temperature and humidity values
        *humidity = (data[0] << 8 | data[1]) / 10.0;
        *temperature = ((data[2] & 0x7F) << 8 | data[3]) / 10.0;
        if (data[2] & 0x80) *temperature *= -1;
    }
}

Note that this updated code snippet also takes two pointers as arguments, which will allow you to return the temperature and humidity values back to the calling function. You can modify the code to suit your specific requirements.

8. In your main function, you can call the DHT11_ReadData function to read temperature and humidity values from DHT11. Here’s an example:

int main(void)
{
    float temperature, humidity;

    // initialize GPIO and timer
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    while (1)
    {
        // read data from DHT11
        DHT11_ReadData(&temperature, &humidity);

        // print temperature and humidity values
        printf("Temperature: %.1fC\r\n", temperature);
        printf("Humidity: %.1f%%\r\n", humidity);

        // wait for 1 second
        HAL_Delay(1000);
    }
}

This code continuously reads temperature and humidity values from DHT11 and prints them to the console. You can modify the code to suit your specific requirements, such as storing the temperature and humidity values in a database or sending them to a remote server via the network.

Note that this is just a basic example of interfacing DHT11 with STM32. You can add more features and functionalities to this code to make it more robust and reliable.

  1. Make sure to handle any errors that may occur during the communication with DHT11. For example, if the data checksum is not valid, you can discard the received data and try again. Similarly, if the communication with DHT11 fails repeatedly, you can raise an error and take appropriate action, such as resetting the MCU or displaying an error message.
  2. It’s also a good practice to add some error-checking code to your functions to ensure that the input values are valid and the functions execute as expected. For example, you can add checks to ensure that the GPIO port and pin are valid and that the data buffer has enough space to hold the received data.
  3. Finally, remember to test your code thoroughly before deploying it in your application. Test it under different conditions and scenarios to ensure that it works as expected.

With these steps, you should be able to successfully interface DHT11 with STM32 and obtain temperature and humidity values from it. Good luck!

Read more on:

Liked this article? Subscribe to our newsletter:

Loading

or,

Visit LabProjectsBD.com for more inspiring projects and tutorials.

Thank you!


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *