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:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
Table of Contents
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:
- 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.
- Initialize the GPIO pin as an input pin. The STM32 CubeMX tool can be used to generate the initialization code for the GPIO pin.
- 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.
- 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.
- 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.
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.
- 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].
- 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.
- 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.
- 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.
- 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:
- Introducing RTOS with stm32
- STM32 as USB Device
- Simple voltage protector for your appliances
- Digital Clock with DS3231 & PIC microcontroller
- Digital Clock using a PIC microcontroller & RTC DS1307
- AC voltmeter using a PIC16F76 micro-controller and capacitor power supply
For Professional Designs or Help:
0 Comments