{"id":1636,"date":"2024-06-26T15:09:39","date_gmt":"2024-06-26T15:09:39","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1636"},"modified":"2024-08-11T16:52:28","modified_gmt":"2024-08-11T16:52:28","slug":"temperature-humidity-sensor","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1636","title":{"rendered":"Temperature &amp; Humidity Sensor"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1087057d142403a8cefa11eacbd3bb57 wp-block-paragraph\" style=\"color:#5c5c5c\">DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. The sensor includes a resistive sense of wet components and an NTC temperature measurement device and connected with a high-performance 8-bit microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-36cb3da79309304b674f56aa16c8b8d9 wp-block-paragraph\" style=\"color:#5c5c5c\">R<sub>T<\/sub>: Typical application circuit recommended in the short cable length of 20m on the 5.1k\u03a9 pull-up register, the resistance of greater than 20m under the pull-up resistor on the lower actual situation. To get the temperature and humidity data we need 3 steps. Let\u2019s look at all steps one by one<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"644\" height=\"454\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT11-sensor.jpg\" alt=\"\" class=\"wp-image-1642\" style=\"width:257px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT11-sensor.jpg 644w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT11-sensor-300x211.jpg 300w\" sizes=\"(max-width: 644px) 100vw, 644px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"923\" height=\"1024\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-3-923x1024.png\" alt=\"\" class=\"wp-image-1649\" style=\"width:682px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-3-923x1024.png 923w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-3-270x300.png 270w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-3-768x852.png 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/image-3.png 1333w\" sizes=\"(max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-e61884838aa2bf26cbfcfb115ae19286 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the pulse width of 26\u00b5s ~ 28\u00b5s indicates a bit 0 and 70\u00b5s represent bit 1. We measure the pulse using External Interrupt and timer. We enable external interrupt at any edge. Let\u2019s look at the program below for simplicity-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"678\" height=\"41\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-2.jpg\" alt=\"\" class=\"wp-image-1654\" style=\"width:397px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-2.jpg 678w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-2-300x18.jpg 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n void init_dht11(void)\n  {\n    DDRD|=(1&lt;&lt;DDD2);       \/\/DD2 for read data\n    PORTD|=(1&lt;&lt;PORTD2);    \/\/Output high\n    PORTD&amp;=~(1&lt;&lt;PORTD2);   \/\/Ouput Low for 18ms\n    _delay_ms(18);\n    PORTD|=(1&lt;&lt;PORTD2);    \/\/Output high for relise bus\n    DDRD&amp;=~(1&lt;&lt;DDD2);      \/\/Input sate declear for interrupt triger\n    while(PIND&amp;(1&lt;&lt;PIND2));\/\/Wait for pull the data line low\n    while(!(PIND&amp;(1&lt;&lt;PIND2)));\/\/wait 80us low\n    while(PIND&amp;(1&lt;&lt;PIND2)); \/\/wait 80us high\n    while(!(PIND&amp;(1&lt;&lt;PIND2)));\/\/wait 50us low\n    sei();\n    \/\/SREG|=(1&lt;&lt;I);\t       \/\/Global interrupt enable\n    GICR |=(1&lt;&lt;INT0);\t   \/\/External Interrupt Request 0 Enable\n    MCUCR|=(1&lt;&lt;ISC00);\t   \/\/any edge of INT0 generates an interrupt request\n }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-857f5d0dd22369f67e3ae46378fbe691 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s use timer0 with prescaler 8. So the new frequency &amp; time of timer0 is<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8370c9550351710001ea22cbd3881f9a wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u0192<sub>timer0<\/sub> = 8MHz\/8 = 1MHz &nbsp;&amp; T<sub>timer0<\/sub> = 1\u00b5s<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-663771a4e7cb43260af3ade36f344f0a wp-block-paragraph\" style=\"color:#5c5c5c\">TCNT0 value will determine the pulse width value.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"250\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pulse-width-1024x250.jpg\" alt=\"\" class=\"wp-image-1660\" style=\"width:717px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pulse-width-1024x250.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pulse-width-300x73.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pulse-width-768x187.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/pulse-width.jpg 1079w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-79ec0d0e94047c4d9311f9a9e320849d wp-block-paragraph\" style=\"color:#5c5c5c\">Look at the interrupt service routine function for more details \u2013<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n uint8_t start_flag=1,millisecond;\n\/**** Interrupt service routine ********************\/\nISR(INT0_vect)\n{ \n if(start_flag){\n\t\tpulse=TCNT0;\n\t\tTCCR0=0; \/\/ Timer stop\n\t\tif(pulse&lt;30) sample&#x5B;count]=0;\n        else if(pulse&gt;50 &amp;&amp; pulse&lt;75) sample&#x5B;count]=1;\n        count++;\n        start_flag=0;\n    }else{ \/\/ start timer with flag clear\n    TCCR0|=(1&lt;&lt;CS01);                       \/\/prescaler 8\n    TCNT0=0;                                \/\/intialize timer value\n   start_flag=1;\n  }\nif(count&gt;40) \n\t{\n\t\tcount=0;\n\t\tstart_flag=1;\n\t}\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-6ab074842e86ac1d35197d6de707828a wp-block-paragraph\" style=\"color:#5c5c5c\">We have found the bit, now it is time for calculation-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"284\" height=\"52\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/c-structure-1.jpg\" alt=\"\" class=\"wp-image-603\" style=\"width:158px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-89652422e6e63ddcdf7c70178720edf4 wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Constant variable<\/strong>: const is used with a data type declaration or definition to specify an unchanging value.<\/p>\n\n\n\n<ul style=\"color:#2a2a2a\" class=\"wp-block-list has-text-color has-link-color wp-elements-77956acb5872e6cba75567a4fad680dd\">\n<li>Example-&nbsp; const int five=5;<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d1c1058d9c2d5e0e3c04534a7e608bb6 wp-block-paragraph\" style=\"color:#5c5c5c\">Const objects may not be changed.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-ca4398bc7d5a47d39ef7b3b9772d3d3f wp-block-paragraph\" style=\"color:#5c5c5c\"><strong>Volatile variable<\/strong>: Volatile specifies a variable whose value may be changed by process outside the current program. One example of a volatile object might be a buffer used to exchange data with an eternal device.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-0fba37819b6f091f4e70b0dda98aa288 wp-block-paragraph\" style=\"color:#5c5c5c\">In our program we declare volatile uint16_t data_packet[40]; and volatile float humidity,temperature; global variable which can be used in any .c file.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-b2979f5d6626142157bf3acdf61cdd46 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s back to our main program. Sensor transmit a total of 40bits containing the temperature and humidity.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"171\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT-byte-1024x171.jpg\" alt=\"\" class=\"wp-image-1669\" style=\"width:605px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT-byte-1024x171.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT-byte-300x50.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT-byte-768x128.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DHT-byte.jpg 1240w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7f2245f0212644893d6f1d05aa01df66 wp-block-paragraph\" style=\"color:#5c5c5c\">So that the humidity is byte1+byte2*0.1 and in temperature calculation the 7<sup>th<\/sup> bit of fractional data byte represent the positive and negative sign. If 7<sup>th<\/sup> bit is 1 temperature is positive and if it is 0 then temperature is positive.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/************************************************************************************************\n*************************************************************************************************\nThis program will read the temperature and humidity data and display in LCD\n             The value sould be update in every 5s\n \n*************************************************************************************************\n\t\tSubeer Kumar Sarkar\n\t\tElectrical &amp; Electronic Engineer\n************************************************************************************************\n************************************************************************************************\n***********************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;avr\/interrupt.h&gt;\n#include&quot;dht11.h&quot;\n#include&quot;lcd.h&quot;\nchar str&#x5B;40],deg=223,hum=37;\nint main(void)\n{\nLCD_INIT();         \/\/LCD intilize\nLCD_write_string(1,1,&quot;Temp and Humidity&quot;);\ntimer2_250ms();\ninit_dht11();\n_delay_ms(1000);\nwhile(1)\n{\n for(uint8_t i=0;i&lt;8;i++)\n {\n   humidity|=(sample&#x5B;i]&lt;&lt;(7-i));\n   temperature|=(sample&#x5B;16+i]&lt;&lt;(7-i));\n }\n sprintf(str,&quot;HR:%02d%c T:%02d%cC  &quot;,humidity,hum,temperature,deg);\n LCD_write_string(1,2,str);\n temperature=0;humidity=0;\n _delay_ms(1000); \/\/delay for LCD update\n}\nreturn 0;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here timer2 is initialized at 250ms to call sensor at every 5s. So the data will be update in every 5s. One interesting thing that we use %02d to display only 2digit of temperature and humidity data.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Interfacing-Temperature-and-Humidity-sensor.rar\" style=\"border-radius:10px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-f4356f3276210854964f82c13fd2fd9d wp-block-paragraph\" style=\"color:#202020\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Interfacing-Temperature-and-Humidity-sensor.rar\">Interfacing Temperature and Humidity sensor.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. The sensor includes a resistive sense of wet components and an NTC temperature measurement device and connected with a high-performance 8-bit microcontroller. RT: Typical application circuit recommended in the short cable length of 20m [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1636","post","type-post","status-publish","format-standard","hentry","category-timer-counter"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1636"}],"version-history":[{"count":22,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions"}],"predecessor-version":[{"id":2603,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions\/2603"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}