{"id":646,"date":"2021-12-23T04:30:22","date_gmt":"2021-12-23T04:30:22","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=646"},"modified":"2021-12-23T04:30:25","modified_gmt":"2021-12-23T04:30:25","slug":"working-with-stm32-and-sensors-dht11","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=646","title":{"rendered":"Working with STM32 and sensors: DHT11"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/aaloktech.in\/wp-content\/uploads\/2020\/10\/dht11-humidity-and-temperature-sensor-module-2-6000x6000-1.jpg\" alt=\"\" \/><figcaption>DHT11<\/figcaption><\/figure><\/div>\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<p><\/p>\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&#8243; 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&#8243; 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<figure class=\"wp-block-image size-large\"><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>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>We need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Change core frequency to 100MHz (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=454\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=454\" target=\"_blank\">here<\/a>).<\/li><li>OLED driver (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=613\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=613\" target=\"_blank\">Here<\/a>).<\/li><li>Systick interrupt (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=246\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=246\" target=\"_blank\">here<\/a>).<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 Delay in milliseconds:<\/h2>\n\n\n\n<p>We start off by creating delay.c and delay.h <\/p>\n\n\n\n<p>The code for delay in milliseconds inside delay.c 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;delay.h&quot;\n#include &quot;stm32f4xx.h&quot;                  \/\/ Device header\n\n\nvolatile uint64_t ms,rms;\nvoid systick_init_ms(uint32_t freq)\n\t{\n\t__disable_irq();\n\tSysTick-&gt;LOAD=(freq\/1000)-1;\n\tSysTick-&gt;VAL=0;\n\tSysTick-&gt;CTRL=7; \/\/0b00000111;\n\t__enable_irq();\t\n}\n\nuint64_t millis(void)\n\t{\n\t__disable_irq();\n\trms=ms; \/\/store current ms in rms\n\t__enable_irq();\n\treturn rms;\n\t}\n\nvoid SysTick_Handler(void){\nms++;\n}\n\nvoid delay(uint32_t delay)\n\t{\n\t\n\t\tuint64_t start=millis();\n\tdo\n\t\t{}while(millis()-start!=delay);\n\t}\n<\/pre><\/div>\n\n\n\n<p>for delay.h<\/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\n#include &lt;stdint.h&gt;\nuint64_t millis(void);\nvoid systick_init_ms(uint32_t freq);\nvoid delay(uint32_t delay);\n#endif\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 Microseconds timer:<\/h2>\n\n\n\n<p>In order to interface DHT11 with STM32, we need to measure how long the signal stayed high in microseconds as shown two figures below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"552\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-1024x552.png\" alt=\"\" class=\"wp-image-649\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-1024x552.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-300x162.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-768x414.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-1536x828.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-1150x620.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-750x404.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-400x216.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM-250x135.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.15-AM.png 1826w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>DHT11 sends bit 0 indication<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"472\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-1024x472.png\" alt=\"\" class=\"wp-image-650\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-1024x472.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-300x138.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-768x354.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-1536x708.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-1150x530.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-750x346.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-400x184.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM-250x115.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-6.54.26-AM.png 1822w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>DHT11 sends 1 indication<\/figcaption><\/figure>\n\n\n\n<p>Hence, we can create two files named micros.h and micros.c<\/p>\n\n\n\n<p>In micros, we start off by declaring function for initialising micros which takes bus frequency as argument<\/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 micros_init(uint32_t freq)\n\t<\/pre><\/div>\n\n\n\n<p>Within the function, we enable clock access to timer3<\/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;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_TIM3EN; \/\/enable timer3 clock access<\/pre><\/div>\n\n\n\n<p>Set the prescaler <\/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;}\">TIM3-&gt;PSC=freq-1; \/\/100 000 000 Hz \/ 1 00 = 1 000 000 Hz<\/pre><\/div>\n\n\n\n<p>Reset the counter<\/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;}\">TIM3-&gt;CNT=0;<\/pre><\/div>\n\n\n\n<p>Enable counter 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;}\">TIM3-&gt;CR1|=TIM_CR1_CEN;<\/pre><\/div>\n\n\n\n<p>We also need two extra function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Reset the microseconds counter<\/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 reset_micros(void)\n\t{\n\tTIM3-&gt;CR1&amp;=~TIM_CR1_CEN;\n\tTIM3-&gt;CNT=0;\n\tTIM3-&gt;CR1|=TIM_CR1_CEN;\n\t}<\/pre><\/div>\n\n\n\n<p>Return the current timer value<\/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;}\">uint32_t micros(void)\n\t{\n\treturn TIM3-&gt;CNT; \n\t}<\/pre><\/div>\n\n\n\n<p>In micros.h<\/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 __micros__h\n#define __micros__h\n#include &quot;stdint.h&quot;\n\n\nvoid micros_init(uint32_t freq);\nvoid reset_micros(void);\nuint32_t micros(void);\n\n\n#endif\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.3 DHT read:<\/h2>\n\n\n\n<p>In order to read DHT11, the mcu must send low signal for 18 milliseconds as shown in the figure below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"351\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-1024x351.png\" alt=\"\" class=\"wp-image-651\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-1024x351.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-300x103.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-768x263.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-1536x526.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-2048x702.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-1150x394.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-750x257.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-400x137.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-23-at-7.05.11-AM-250x86.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We declare read_dht which returns int8_t and  takes no argument <\/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;}\">int8_t read_dht(void)<\/pre><\/div>\n\n\n\n<p>Inside the read function. we start by enabling 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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>declare some local required variables <\/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;}\">\tuint8_t bits[5];\n\tuint8_t cnt = 7;\n\tuint8_t idx = 0;\n\tfor (int i=0; i&lt; 5; i++) {bits[i] = 0;}<\/pre><\/div>\n\n\n\n<p>Set PA8 to output and set it to low<\/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;}\">\tGPIOA-&gt;MODER|=GPIO_MODER_MODE8_0;\n\tGPIOA-&gt;ODR&amp;=~GPIO_ODR_OD8;\n\tdelay(18);<\/pre><\/div>\n\n\n\n<p>Set PA8 to high<\/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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">GPIOA-&gt;ODR|=GPIO_ODR_OD8;<\/pre><\/div>\n\n\n\n<p>reset micros counter and set PA8 to input<\/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;}\">\treset_micros();\n\tuint32_t start=micros();\n\tfor(int i=0;i&lt;1000;i++); \/\/wait a littl bit\n\tGPIOA-&gt;MODER&amp;=~GPIO_MODER_MODE8_0;<\/pre><\/div>\n\n\n\n<p>Then we check for the response, if there is no response, return -2 as error code<\/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;}\">\tunsigned int loopCnt = 1000000;\n\n\twhile(!(GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)){if (loopCnt-- == 0) return -2;}\n\tloopCnt = 10000;\n\twhile((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8)){if (loopCnt-- == 0) return -2;}\n<\/pre><\/div>\n\n\n\n<p>Since dht11 sends 40-bits of data, we can start acquire them<\/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;}\">\tfor (int i=0; i&lt;40; i++)\n  {\n    loopCnt = 100000;\n    while(!(GPIOA-&gt;IDR &amp; GPIO_IDR_ID8))\n\t\t{if (loopCnt-- == 0) return -2;}\n\n    unsigned long t = micros();\n\n    loopCnt = 1000000;\n    while((GPIOA-&gt;IDR &amp; GPIO_IDR_ID8))\n      if (loopCnt-- == 0)\n\t\t\t{return -2;}\n\n    if ((micros() - t) &gt; 40) bits[idx] |= (1 &lt;&lt; cnt);\n    if (cnt == 0)\n    {\n      cnt = 7;\n      idx++;\n    }\n    else cnt--;\n  }\n\n\ttemp= bits[2];\n\thum= bits[0];\n\tuint8_t sum = bits[0] + bits[2];\n  if (bits[4] != sum)\n\t{return -1;}\n\n  return 0;\n\n\t}<\/pre><\/div>\n\n\n\n<p>Then we can use functions to return the temperature and humidity<\/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;}\">uint8_t read_temp(){return temp;}\nuint8_t read_hum(){return hum;}<\/pre><\/div>\n\n\n\n<p>In main.c<\/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;dht.h&quot;\n#include &quot;micros.h&quot;\n#include &quot;stdio.h&quot;\n#include &quot;fonts.h&quot;\nextern void SysClockConfig(void);\nuint8_t temperature,humidity;\nint8_t error;\n\nchar data[20];\n\nint main(void)\n\t{\n\t\tSysClockConfig();\n\t\tsystick_init_ms(100000000);\n\t\tmicros_init(100);\n\t\tSSD1306_Init();\n\t\tSSD1306_GotoXY (0,0);\n\t\tSSD1306_Puts (&quot;DHT11&quot;, &amp;Font_11x18, 1);\n\t\tSSD1306_GotoXY (0,20);\n\t\tSSD1306_Puts (&quot;STM32&quot;, &amp;Font_11x18, 1);\n\t\tSSD1306_UpdateScreen(); \/\/display\n\t\tdelay(3000);\n\t\tSSD1306_Clear();\n\n\t\twhile(1)\n\t\t\t{\n\t\t\t\terror=read_dht();\n\t\t\t\ttemperature=read_temp();\n\t\t\t\thumidity=read_hum();\n\t\t\t\tsprintf(data,&quot;temp=%u C&quot;,temperature);\n\t\t\t\tSSD1306_GotoXY (0,0);\n\t\t\t\tSSD1306_Puts (data, &amp;Font_11x18, 1);\n\t\t\t\tsprintf(data,&quot;hum=%u C&quot;,humidity);\n\t\t\t\tSSD1306_GotoXY (0,20);\n\t\t\t\tSSD1306_Puts (data, &amp;Font_11x18, 1);\n\t\t\t\tSSD1306_UpdateScreen(); \/\/display\n\t\t\t\tdelay(1000);\n\t\t\t}\n\t}\n\n<\/pre><\/div>\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\/2021\/12\/dht11.zip\">dht11<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/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>After compile, uploading the code to the board, you should get this<\/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\/2021\/12\/IMG_7534-768x1024.jpg\" alt=\"\" class=\"wp-image-653\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-768x1024.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-225x300.jpg 225w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-1152x1536.jpg 1152w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-1536x2048.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-1150x1533.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-750x1000.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-400x533.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-250x333.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/12\/IMG_7534-scaled.jpg 1920w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><figcaption>Results<\/figcaption><\/figure>\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-646","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\/646"}],"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=646"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions\/654"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}