{"id":1079,"date":"2024-06-23T16:49:19","date_gmt":"2024-06-23T16:49:19","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1079"},"modified":"2024-06-26T15:10:48","modified_gmt":"2024-06-26T15:10:48","slug":"ds18b20-interfacing-with-gpio","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1079","title":{"rendered":"DS18B20(Digital Temperature Sensor) interfacing with GPIO"},"content":{"rendered":"<p><style>\/*! elementor - v3.22.0 - 17-06-2024 *\/<br \/>\n.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DS18B20.jpg\" sizes=\"(max-width: 863px) 100vw, 863px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DS18B20.jpg 863w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DS18B20-300x158.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/DS18B20-768x404.jpg 768w\" alt=\"\" width=\"741\" height=\"390\" \/><\/p>\n<p>The DS18B20 Digital Thermometer provides 9 to 12-bit (configurable) temperature readings which indicate the temperature of the device. Information is sent to\/from the DS18B20 over a 1-Wire interface, so that only one wire (and ground) needs to be connected from a central microprocessor to a DS18B20. Power for reading, writing, and performing temperature conversions can be derived from the data line itself with no need for an external power source.<\/p>\n<p>To interface with DS18B20 digital temperature sensor we need to initialize the sensor with some register parameter. Let\u2019s look at the only important register that should be required for interfacing.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/command-DS18B20.jpg\" sizes=\"(max-width: 1372px) 100vw, 1372px\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/command-DS18B20.jpg 1372w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/command-DS18B20-300x53.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/command-DS18B20-1024x181.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/command-DS18B20-768x135.jpg 768w\" alt=\"\" width=\"1372\" height=\"242\" \/><\/p>\n<p>By default the DS18B20 display temperature in 12bit so other parameter will not consider. Now the sensor has only 1 pin for data in and out. So there is a procedure to write and read bits from the sensor. Now first reset the sensor-<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/reset-ds18b20.jpg\" alt=\"\" width=\"1251\" height=\"549\" \/><\/p>\n<p>Form the timing diagram we show that the sensor pin should be grounded for minimum 480\u00b5s to maximum 960\u00b5s, than we have to release the line and wait for at least 60\u00b5s and read the signal of the sensor pin. Here one important thing that the I\/O pin first configure as output with output low, then wait 480\u00b5s, then we have to release the line for input reading, that will be done with I\/O port define as input pin. Let\u2019s look at the code for simplicity-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-1-1.jpg\" alt=\"\" width=\"1254\" height=\"328\" \/><\/p>\n<p>Here 420\u00b5s used for stable the sensor. If we found output 0 than the sensor is okay otherwise replace the sensor and repeat the process. Now it is time to write the register bit for operation.<\/p>\n<p>To write 0 the output should low for 60\u00b5s~120\u00b5s and to write 1 the output low from 1\u00b5s~15\u00b5s.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/write-ds18b20.jpg\" alt=\"\" width=\"1368\" height=\"487\" \/><\/p>\n<p>Let\u2019s chose 2\u00b5s for reference, if we want to write 1 set the output low for 2\u00b5s than release the line, otherwise wait another 60\u00b5s for writing 0. To write the register byte we have to write 0\/1 bit in a sequence manner, that 0 number bit 1<sup>st<\/sup>, then 1<sup>st<\/sup> bit, then 2<sup>nd<\/sup> bit so on. So we have to right shift the byte for every bit write operation. Let\u2019s look the full code for simplicity-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-write-ds18b20.jpg\" alt=\"\" width=\"1304\" height=\"422\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/read-ds18b20.jpg\" alt=\"\" width=\"1361\" height=\"519\" \/><\/p>\n<p>To read 0 the output should low for &lt;15\u00b5s and to write 1 the output low from &gt;1\u00b5s. Let\u2019s chose 2\u00b5s for reference. In this case we put the output low for 2\u00b5s and release the line and again wait another 14\u00b5s then we read the pin. \u00a0Now we have the 0 and 1 bit and a variable should be assigned to read all the values. Reading procedure is maintained in a sequence i.e. 0<sup>th<\/sup> bit read first, then 2<sup>nd<\/sup> bit and so on. So we have to right shift the byte for every bit read operation. Let\u2019s look the full code for simplicity-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-read-ds18b20.jpg\" alt=\"\" width=\"1263\" height=\"419\" \/><\/p>\n<p>All we are done with read and write functions. Now it is time to send command and received the 12bit temperature value. It will requires the following steps-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-2-1.jpg\" alt=\"\" width=\"1334\" height=\"284\" \/><\/p>\n<p>Our main concern with the first 2 byte.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/bit-1-0-for-ds18b20.jpg\" alt=\"\" width=\"1346\" height=\"108\" \/><\/p>\n<p>Here s=1 for negative temperature and s=0 for positive number. For example-<\/p>\n\n\n<p class=\"wp-block-paragraph\">The full for temperature read as..-> float DS18B20ReadTemp() \u2013 return temperature value in float<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nfloat DS18B20ReadTemp()\n{\n    uint8_t ds18b20_scratchpad&#x5B;8];\n\tuint16_t digit;\n    uint16_t decimal;\n\tfloat temperature;\n\tflag=DS18B20Reset();\n\tDS18B20Write(DS18B20_CMD_SKIPROM);\n\tDS18B20Write(DS18B20_CMD_CONVERTTEMP);\n\t\/\/Wait until conversion is complete 0 complete 1 processing\n\twhile(!DS18B20Read());\n\tflag=DS18B20Reset();\n\tDS18B20Write(DS18B20_CMD_SKIPROM);\n\tDS18B20Write(DS18B20_CMD_RSCRATCHPAD);\n\tfor(uint8_t i=0;i&lt;8;i++)\n\t{\n\t\tds18b20_scratchpad&#x5B;i]=DS18B20Read();\n\t}\n\tsign=ds18b20_scratchpad&#x5B;1]&amp;0xf0;\/\/if not 0 value is negative\n\tif(sign)\n\t{\n\t\tds18b20_scratchpad&#x5B;0]=~ds18b20_scratchpad&#x5B;0]; \/\/inverse the bit\n\t\tds18b20_scratchpad&#x5B;1]=~ds18b20_scratchpad&#x5B;1];\n\t}\n\tdigit=ds18b20_scratchpad&#x5B;0]&gt;&gt;4;\n    digit|=(ds18b20_scratchpad&#x5B;1]&amp;0x7)&lt;&lt;4;\n\tdecimal=ds18b20_scratchpad&#x5B;0]&amp;0xf;\n\ttemperature=digit+(decimal*THERM_DECIMAL_STEPS_12BIT);\n\tif(sign) temperature=-(temperature+THERM_DECIMAL_STEPS_12BIT);\n\treturn temperature;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Download the DS18B20.c and DS18B20.h file. Our main program is simply read the temperature and displays the value in LCD display.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;stdio.h&gt;\n#include&quot;lcd.h&quot;        \/\/ PORTD as LCD port\n#include&quot;DS18B20.h&quot;\nchar name&#x5B;20],str;\nfloat temp;\nint main(void)\n{\nLCD_INIT();\nLCD_Clear();\n_delay_ms(1000);\nLCD_write_string(1,1,&quot;temperature&quot;);\nwhile(1)\n\t{\n\t_delay_ms(100);\n\ttemp=DS18B20ReadTemp();\n\tsprintf(name,&quot;tem= %.4f&quot;, temp);\n\tLCD_write_string(1,2,name);\n\t}\nreturn 0;\n}\n<\/pre><\/div>\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\" style=\"border-radius:11px;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-upper-heading-font-size wp-block-paragraph\"><strong>Download the full program with proteus simulation<\/strong><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The DS18B20 Digital Thermometer provides 9 to 12-bit (configurable) temperature readings which indicate the temperature of the device. Information is sent to\/from the DS18B20 over a 1-Wire interface, so that only one wire (and ground) needs to be connected from a central microprocessor to a DS18B20. Power for reading, writing, and performing temperature conversions can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1079","post","type-post","status-publish","format-standard","hentry","category-i-o-port-operations"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1079","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=1079"}],"version-history":[{"count":43,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":1638,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1079\/revisions\/1638"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}