{"id":2338,"date":"2024-02-08T08:50:59","date_gmt":"2024-02-08T08:50:59","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2338"},"modified":"2024-02-08T08:51:03","modified_gmt":"2024-02-08T08:51:03","slug":"revisedworking-with-stm32-and-sensors-dht11","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2338","title":{"rendered":"[Revised]Working with STM32 and sensors: DHT11"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg.webp\" alt=\"\" class=\"wp-image-2339\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg.webp 600w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg-250x250.webp 250w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this article, we shall look at the dth11 temperature and humidity sensor and how does it work and interface it with STM32F4 Nucleo-64 board.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DHT11<\/li><li>Connection<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. DHT11:<\/h2>\n\n\n\n<p>The DHT11 Temperature &amp; Humidity Sensor features a temperature &amp; humidity sensor complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition technique and temperature &amp; humidity sensing technology, it ensures high reliability and excellent long-term stability. This sensor includes a resistive-type humidity measurement component and an NTC temperature measurement component, and connects to a high- performance 8-bit microcontroller, offering excellent quality, fast response, anti-interference ability and cost-effectiveness.<\/p>\n\n\n\n<p>Each DHT11 element is strictly calibrated in the laboratory that is extremely accurate on humidity calibration. The calibration coefficients are stored as programmes in the OTP memory,&nbsp;which are used by the sensor\u2019s internal signal detecting process. The single-wire serial interface makes system integration quick and easy. Its small size, low power consumption and up-to-20 meter signal transmission making it the best choice for various applications, including those most demanding ones. The component is 4-pin single row pin package. It is convenient to connect and special packages can be provided according to users\u2019&nbsp;request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HOW THE DHT11 MEASURES HUMIDITY AND TEMPERATURE<\/h2>\n\n\n\n<p>The DHT11 detects&nbsp;water vapor by measuring the electrical resistance between two electrodes.&nbsp;The humidity sensing component is&nbsp;a moisture holding substrate with electrodes applied to the surface. When water vapor is absorbed by the substrate, ions are released by the substrate which increases the conductivity between the electrodes. The change in resistance between the two electrodes is proportional to the relative humidity. Higher relative humidity decreases the resistance between the electrodes, while lower relative humidity increases the resistance between the electrodes.<\/p>\n\n\n\n<p>The DHT11 measures temperature with a surface mounted&nbsp;NTC temperature sensor&nbsp;(thermistor) built into the unit.&nbsp;<\/p>\n\n\n\n<p>With the plastic housing removed, you can see the electrodes applied to the&nbsp;substrate:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Temperature-and-Humidity-Sensor-Inside-Front-with-Cover-Removed.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Temperature-and-Humidity-Sensor-Inside-Front-with-Cover-Removed.jpg\" alt=\"DHT11 Temperature and Humidity Sensor Inside Front with Cover Removed\" class=\"wp-image-420534\" \/><\/a><\/figure>\n\n\n\n<p>An IC mounted on&nbsp;the back of&nbsp;the unit converts the resistance measurement to relative humidity.&nbsp;It&nbsp;also stores the&nbsp;calibration coefficients, and controls the data signal transmission between the DHT11 and the Arduino:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Temperature-and-Humidity-Sensor-Inside-Back-with-Cover-Removed.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Temperature-and-Humidity-Sensor-Inside-Back-with-Cover-Removed.jpg\" alt=\"DHT11 Temperature and Humidity Sensor Inside Back with Cover Removed\" class=\"wp-image-420533\" \/><\/a><\/figure>\n\n\n\n<p>The DHT11 uses just one signal wire to transmit data&nbsp;to the Arduino. Power comes from separate 5V and ground wires. A 10K Ohm pull-up resistor is needed between&nbsp;the signal line and&nbsp;5V line to make sure the signal level stays high by default&nbsp;(see the datasheet for more info).<\/p>\n\n\n\n<p>There are two different versions&nbsp;of the DHT11 you might come across. One type has four pins, and the other type has three pins and is mounted to a small PCB. The PCB mounted version is nice because&nbsp;it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are the pin outs for both versions:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Pinout-for-three-pin-and-four-pin-types-2.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/www.circuitbasics.com\/wp-content\/uploads\/2015\/12\/DHT11-Pinout-for-three-pin-and-four-pin-types-2.jpg\" alt=\"Comparison of three pin DHT11 vs four pin DHT11\" class=\"wp-image-420522\" \/><\/a><\/figure>\n\n\n\n<p><strong><em>Technical Details<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Low cost<\/li><li>3 to 5V power and I\/O<\/li><li>2.5mA max current use during conversion (while requesting data)<\/li><li>Good for 20-80% humidity readings with 5% accuracy<\/li><li>Good for 0-50\u00b0C temperature readings \u00b12\u00b0C accuracy<\/li><li>No more than 1 Hz sampling rate (once every second)<\/li><li>Body size 15.5mm x 12mm x 5.5mm<\/li><li>4 pins with 0.1\u2033 spacing<\/li><\/ul>\n\n\n\n<p>Since the accuracy of the sensor is not that great, it is mostly used for temperature and humidity indicator in home application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connection:<\/h2>\n\n\n\n<p>In this guide, we will need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>STM32F411RE-Nucleo-64<\/li><li>DHT11 Sensor<\/li><li>0.96\u2033 OLED display<\/li><li>Small breadboard<\/li><li>Hookup wires<\/li><\/ul>\n\n\n\n<p>The connection shall be as shown in the image below<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"778\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-1024x778.png\" alt=\"\" class=\"wp-image-647\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-1024x778.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-300x228.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-768x583.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-1536x1167.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-2048x1556.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-1150x874.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-750x570.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-400x304.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.31.42-AM-250x190.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The DHT11 pin is connected to PA8.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In my case the module used has built-in pull resistor. Hence, no need to connect external one.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start by creating the required delay using systick timer which is available in any ARM Cortex M based mcu.<\/p>\n\n\n\n<p>Create new source and header file with name of delay.c and delay.h.<\/p>\n\n\n\n<p>Within the header file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef __delay__H__\n#define __delay__H__\n\n#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n\n#include &lt;stdint.h&gt;\nvoid delayuS(int us);\nvoid delay(int ms);\n\n\n#endif\n<\/pre><\/div>\n\n\n\n<p>Within the source file:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;delay.h&quot;\n\n\n\n\nvoid delayuS(int us)\n{\n\n\tSysTick-&gt;LOAD=16-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=0x5;\n\t\tfor (int i=0;i&lt;us;i++)\n\t\t{\n\t\t\twhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\t\n\t\n\n\t\t\t \n\t}\n\nvoid delay(int ms)\n{\n\n\tSysTick-&gt;LOAD=16000-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=0x5;\n\t\tfor (int i=0;i&lt;ms;i++)\n\t\t{\n\t\t\twhile(!(SysTick-&gt;CTRL &amp;0x10000)){}\n\t\t}\n\tSysTick-&gt;CTRL=0;\t\n\n}\n<\/pre><\/div>\n\n\n\n<p>For the OLED display, please refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=613\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=613\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, create new source and header file with name of dht11.c and dht11.h respectively.<\/p>\n\n\n\n<p>Within the header file:<\/p>\n\n\n\n<p>Declare the header guard:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef DHT11_H_\n#define DHT11_H_\n\n\n#endif \/* DHT11_H_ *\/<\/pre><\/div>\n\n\n\n<p>Include the stdint library:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>declare an enum to handle if there is a response from the sensor or not as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef enum\n{\n\tResponse_OK=0,\n\tResponse_ERR=1\n\n}DHT11_Response_Typedef;<\/pre><\/div>\n\n\n\n<p>Declare the following function:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void dht11_PinA8_Init(void);<\/pre><\/div>\n\n\n\n<p>This function will enable clock access to GPIOA and take no argument and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void dht11_start(void);<\/pre><\/div>\n\n\n\n<p>This function will start the dht11 to start measuring the temperature and humidity. It Takes no argument and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DHT11_Response_Typedef Check_Response();<\/pre><\/div>\n\n\n\n<p>This function will check the status of the response and return if the response is ok or error.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void Get_DHT_Data(float * TEMP, float *RH);<\/pre><\/div>\n\n\n\n<p>This function will get the DHT11 data, it takes temperature and Relative Humidity as float pointer and returns nothing.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the header file as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef DHT11_H_\n#define DHT11_H_\n\n#include &quot;stdint.h&quot;\n\n\ntypedef enum\n{\n\tResponse_OK=0,\n\tResponse_ERR=1\n\n}DHT11_Response_Typedef;\n\nvoid dht11_PinA8_Init(void);\nvoid dht11_start(void);\nDHT11_Response_Typedef Check_Response();\nvoid Get_DHT_Data(float * TEMP, float *RH);\n\n\n#endif \/* DHT11_H_ *\/<\/pre><\/div>\n\n\n\n<p>In source file:<\/p>\n\n\n\n<p>We start by including the required header files:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;dht11.h&quot;\n#include &quot;stm32f4xx.h&quot;\n#include &quot;delay.h&quot;<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>First function is the enable clock access to GPIOA:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void dht11_PinA8_Init(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n}<\/pre><\/div>\n\n\n\n<p>For the start of the sensor measurement, the followings are the step:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set PA8 to output.<\/li><li>Set the pin to low for 18 milliseconds.<\/li><li>Set PA8 to input.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void dht11_start(void)\n{\n\t\/*Set PA8 as output*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE8_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8_1;\n\n\t\/*Set the pin low*\/\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR8;\n\n\t\/*Wait for 18ms*\/\n\n\tdelay(18);\n\n\t\/*Set the pin to input*\/\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8;\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>To check for the response:<\/p>\n\n\n\n<p>In order to indicate it\u2019s&nbsp;<strong>presence<\/strong>, after receiving the start signal, DHT11 will send a response signal. To do so, it will pull the data line low for 80 us, and than high for another 80 us. To read this response, we will do the following<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wait for 40 us<\/li><li>Read the pin, it must be low at this point<\/li><li>wait for 80 us<\/li><li>Read the pin, this time it should be HIGH<\/li><\/ul>\n\n\n\n<p>If the above conditions are satisfied, that means the sensor is present, and we can proceed with reading the data.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">DHT11_Response_Typedef Check_Response()\n{\n\tuint8_t Response = 0;\n\tdelayuS(40);\n\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8)\n\t{\n\t\tdelayuS (80);\n\t\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8)\n\t\t{\n\t\t\tResponse = Response_OK;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tResponse = Response_ERR;\n\t\t}\n\t}\n\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8);   \/\/ wait for the pin to go low\n\treturn Response;\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>READ DATA<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Wait for the pin to go high<\/li><li>Wait for\u00a0<strong>40 us<\/strong>. This is because the length of \u201c0\u201d bit is\u00a0<strong>26-28 us<\/strong>\u00a0 and if the pin is high after\u00a0<strong>40 us<\/strong>, it indicates that the bit is \u201c1\u201d<\/li><li>write the respective values to the variable<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">static uint8_t DHT11_Read (void)\n{\n\tuint8_t i,j;\n\tfor (j=0;j&lt;8;j++)\n\t{\n\t\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8);   \/\/ wait for the pin to go high\n\t\tdelayuS (40);   \/\/ wait for 40 us\n\t\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8)   \/\/ if the pin is low\n\t\t{\n\t\t\ti&amp;= ~(1&lt;&lt;(7-j));   \/\/ write 0\n\t\t}\n\t\telse i|= (1&lt;&lt;(7-j));  \/\/ if the pin is high, write 1\n\t\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8);  \/\/ wait for the pin to go low\n\t}\n\treturn i;\n}\n<\/pre><\/div>\n\n\n\n<p>To get the data:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void Get_DHT_Data(float * TEMP, float *RH)\n{\n\t  uint8_t Rh_byte1 = DHT11_Read ();\n\n\t  uint8_t Rh_byte2 = DHT11_Read ();\n\n\t  uint8_t Temp_byte1 = DHT11_Read ();\n\n\t  uint8_t Temp_byte2 = DHT11_Read ();\n\n\t  uint8_t SUM = DHT11_Read();\n\n\t  *TEMP = (float)Temp_byte1;\n\t  *RH = (float)Rh_byte1;\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the source code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;dht11.h&quot;\n#include &quot;stm32f4xx.h&quot;\n#include &quot;delay.h&quot;\n\nvoid dht11_PinA8_Init(void)\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n}\n\nvoid dht11_start(void)\n{\n\t\/*Set PA8 as output*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE8_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8_1;\n\n\t\/*Set the pin low*\/\n\tGPIOA-&gt;BSRR=GPIO_BSRR_BR8;\n\n\t\/*Wait for 18ms*\/\n\n\tdelay(18);\n\n\t\/*Set the pin to input*\/\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8;\n}\n\nDHT11_Response_Typedef Check_Response()\n{\n\tuint8_t Response = 0;\n\tdelayuS(40);\n\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8)\n\t{\n\t\tdelayuS (80);\n\t\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8)\n\t\t{\n\t\t\tResponse = Response_OK;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tResponse = Response_ERR;\n\t\t}\n\t}\n\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8);   \/\/ wait for the pin to go low\n\treturn Response;\n}\n\nstatic uint8_t DHT11_Read (void)\n{\n\tuint8_t i,j;\n\tfor (j=0;j&lt;8;j++)\n\t{\n\t\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8);   \/\/ wait for the pin to go high\n\t\tdelayuS (40);   \/\/ wait for 40 us\n\t\tif ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)!=GPIO_IDR_ID8)   \/\/ if the pin is low\n\t\t{\n\t\t\ti&amp;= ~(1&lt;&lt;(7-j));   \/\/ write 0\n\t\t}\n\t\telse i|= (1&lt;&lt;(7-j));  \/\/ if the pin is high, write 1\n\t\twhile ((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)==GPIO_IDR_ID8);  \/\/ wait for the pin to go low\n\t}\n\treturn i;\n}\n\nvoid Get_DHT_Data(float * TEMP, float *RH)\n{\n\t  uint8_t Rh_byte1 = DHT11_Read ();\n\n\t  uint8_t Rh_byte2 = DHT11_Read ();\n\n\t  uint8_t Temp_byte1 = DHT11_Read ();\n\n\t  uint8_t Temp_byte2 = DHT11_Read ();\n\n\t  uint8_t SUM = DHT11_Read();\n\n\t  *TEMP = (float)Temp_byte1;\n\t  *RH = (float)Rh_byte1;\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c code:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;oled.h&quot;\n#include &quot;dht11.h&quot;\n#include &quot;delay.h&quot;\n\nfloat temp,hum;\n\nchar oled_buff[20];\n\n\nint main(void)\n{\n\tSSD1306_Init();\n\tSSD1306_GotoXY (0,0);\n\tSSD1306_Puts (&quot;DHT11 STM32&quot;, &amp;Font_11x18, 1);\n\tSSD1306_UpdateScreen(); \/\/display\n\n\tdht11_PinA8_Init();\n\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE5_0;\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE5_1;\n\n\twhile(1)\n\t{\n\t\tdht11_start();\n\t\tif(Check_Response()==Response_OK)\n\t\t{\n\t\t\tGet_DHT_Data(&amp;temp,&amp;hum);\n\n\t\t\tsprintf(oled_buff,&quot;Temp=%0.1f&quot;,temp);\n\t\t\tSSD1306_GotoXY (0,20);\n\t\t\tSSD1306_Puts(oled_buff,&amp;Font_11x18, 1);\n\n\t\t\tsprintf(oled_buff,&quot;Humi=%0.1f&quot;,hum);\n\t\t\tSSD1306_GotoXY (0,40);\n\t\t\tSSD1306_Puts(oled_buff,&amp;Font_11x18, 1);\n\n\t\t\tSSD1306_UpdateScreen();\n\n\t\t}\n\n\t\tdelay(1000);\n\t\tGPIOA-&gt;ODR ^=GPIO_ODR_OD5;\n\t}\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>You may download the entire project from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/DHT11.zip\">DHT11<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/DHT11.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>You should see this once you uploaded the code:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-768x1024.jpg\" alt=\"\" class=\"wp-image-2340\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-768x1024.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-225x300.jpg 225w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-1152x1536.jpg 1152w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-1150x1533.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-750x1000.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-400x533.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1-250x333.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/02\/IMG_7534-1536x2048-1.jpg 1536w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we shall look at the dth11 temperature and humidity sensor and how does it work and interface it with STM32F4 Nucleo-64 board. In this guide, we shall cover the following: DHT11 Connection Code Demo 1. DHT11: The DHT11 Temperature &amp; Humidity Sensor features a temperature &amp; humidity sensor complex with a calibrated [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,19,11,12],"tags":[],"class_list":["post-2338","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-lcd","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2338"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2338"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2338\/revisions"}],"predecessor-version":[{"id":2342,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2338\/revisions\/2342"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}