{"id":3144,"date":"2024-11-07T14:18:19","date_gmt":"2024-11-07T14:18:19","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=3144"},"modified":"2024-11-07T15:25:29","modified_gmt":"2024-11-07T15:25:29","slug":"adc-ntc-sensor-vs-ds18b20","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=3144","title":{"rendered":"ADC: NTC sensor vs DS18B20"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-bcf4ea0893b3341c96d8be2cbf5cff34 wp-block-paragraph\" style=\"color:#5c5c5c\">In ATmel section we learn about ADC (Analog to Digital Converter). In AVR microcontroller we have 10<sup>bit<\/sup> ADC: 2<sup>10<\/sup> , but in STm microcontroller we have 12<sup>bit<\/sup> ADC: 2<sup>12<\/sup>. In AVR we have 5V as reference voltage that\u2019s work on most sensors, but in STm microcontroller we have ref voltage 3.3V. The voltage 3.3V divide into 0 to 2<sup>12<\/sup>-1 or 0~4095. So the ADC output voltage-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-a5f7cef658316eb6d352b5a95d83d281 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; V<sub>out <\/sub>= ( V<sub>in <\/sub>x(2<sup>n<\/sup> -1))\/ V<sub>ref <\/sub>, here n=12 &amp; V<sub>ref <\/sub>= 3.3V<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c92b329a41e39b4b755bc86eb46ee5a2 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; V<sub>out <\/sub>= ( V<sub>in <\/sub>x 4095)\/3.3 V<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-648a9173bb2dca801ccb1f7ccad4be61 wp-block-paragraph\" style=\"color:#5c5c5c\">ADC can 12-bit, 10-bit, 8-bit or 6-bit configurable resolution; 1.0 \u00b5s for 12-bit resolution (1 MHz), 0.93 \u00b5s conversion time for 10-bit resolution, faster conversion times can be obtained by lowering resolution, DMA support. Most ST has internal temperature sensor (V<sub>SENSE<\/sub>) &amp; internal reference voltage (V<sub>REFINT<\/sub>). It has some mode of conversion like; can convert a single channel or can scan a sequence of channels, single mode converts selected inputs once per trigger, continuous mode converts selected inputs continuously, discontinuous mode. Interrupt generation at the end of sampling, end of conversion; end of sequence conversion, and in case of analog watchdog or overrun events. It has also analog watchdog feature. In this article we discuss about NTC sensor for temperature measurements using ADC discontinuous mode with 12<sup>bit <\/sup>resolution with suitable sampling time.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6b38fdc6bbe468f6f2d14a0e3a6c607c wp-block-paragraph\" style=\"color:#5c5c5c\"> There are basically 2 type of sensor NTC &amp; PTC. NTC stands for negative temperature coefficient, and its resistance decreases as its temperature increases. PTC stands for positive temperature coefficient, and its resistance increases as its temperature increases. PTC thermistors are used to protect sensitive components or devices from damage due to over current or short circuits. They can automatically shut off and reset themselves when over-current conditions occur. NTC temperature sensors are mostly used in fridge or AC. It is a low cost sensor &amp; easy to implement. It is nothing but a variable resistor that resistance increase as temperature decreases. For calculating temperature we just need 2 values Resistance at 25 C &amp; Beta Value B (25\/50). In my case \u2013<\/p>\n\n\n\n<ul style=\"color:#252525\" class=\"wp-block-list has-text-color has-link-color wp-elements-5c4cb55459b9b912ac6036899586f6d6\">\n<li>Resistance at 25C R25: 5000 ohm+\/-2%<\/li>\n\n\n\n<li>Beta Value B(25\/50): 3950K+\/-1%<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-1100c4cd94aa54790d10d06c48e4258b wp-block-paragraph\" style=\"color:#5c5c5c\">The room temperature defines the xK ohm, where xK may be 10K, 20K etc. The data sheet provide the Beta value, that we will have to know. Calculation the temperature value in Kelvin, the formula is-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-1aa8c62f085a9633373437fa799c8637 wp-block-paragraph\" style=\"color:#252525\">            Temperature in Kelvin,                T<sub>measure <\/sub>=1\/(ln(R<sub>k<\/sub>\/R<sub>ntc<\/sub>)\/B+1\/(273.15+25)),<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-db78bac86d041ed574317d1ba3c14a92 wp-block-paragraph\" style=\"color:#5c5c5c\">here R<sub>ntc <\/sub>=5K ohm &amp; R<sub>k<\/sub> = NTC measure resistance at environment temperature<\/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\">\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"709\" height=\"502\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/NTC-Sensor.jpg\" alt=\"\" class=\"wp-image-3152\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/NTC-Sensor.jpg 709w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/NTC-Sensor-300x212.jpg 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color wp-elements-d4194c4853855fa86ea679e796702c15 wp-block-paragraph\" style=\"color:#5c5c5c\">In figure the NTC sensor is series with resistor R. According to voltage divider rules the output voltage<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-cc617e86e34998da90a1abb6680750dd wp-block-paragraph\" style=\"color:#252525\">&nbsp;V<sub>out <\/sub>= (R\/( R<sub>ntc <\/sub>+ R)) x V<sub>in<\/sub> , where V<sub>in <\/sub>= 3.3V &amp; R in Kilo ohm (on board resistor)<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d7ee426e88d4f6a08b36eb31edd71f99 wp-block-paragraph\" style=\"color:#252525\">           Or R<sub>ntc<\/sub> = R x 3.3 \/ V<sub>out <\/sub>\u00a0&#8211;\u00a0 R<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7ef7f658ecb9534d3bdf8b207c8344eb wp-block-paragraph\" style=\"color:#5c5c5c\">Here we use 12<sup>bit <\/sup>ADC so V<sub>out <\/sub>is the measure ADC_Value &amp; 3.3V is 4095, So the final result is-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7d4d16100796ebf3f3eb63ad1019977c wp-block-paragraph\" style=\"color:#252525\">\u00a0\u00a0\u00a0 \u00a0    \u00a0R<sub>ntc\u00a0 <\/sub>= R<sub> \u00a0<\/sub>x 4095 \/ ADC_Value &#8211; R, <\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-a427a74f6d46f5aac00f9b7e8a9b9681 wp-block-paragraph\" style=\"color:#5c5c5c\">If on board R is 10k ohm, then R<sub>ntc \u00a0<\/sub>= 10 x 4095\/ ADC_Value \u2013 10<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include &lt;math.h&gt;\n#define ADC_MAX 4095\nvolatile float ntc_temp;\nvolatile uint16_t adc_value;\nvolatile float resistance;\nfloat ntc_temperature(void)\n  {\n    float cal;\n    resistance=(float)ADC_MAX\/adc_value;\n    resistance=(10*resistance)-10;          \/\/ value of on board resistor is 10k\n    cal=log(resistance\/5);\n    ntc_temp=(cal\/3950)+(1\/298.15);\n    ntc_temp=1\/ntc_temp;                    \/\/ In kalvin\n    ntc_temp=ntc_temp-273.15;               \/\/ In Degree Celsius\n    return (float)ntc_temp;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-aa5629e6876c2e2eaded88bec12a3676 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s first initialize the ADC with 2<sup>12<\/sup> bit resolution, discontinues mode with interrupt enable \u2013<\/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\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"950\" height=\"825\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-13.jpg\" alt=\"\" class=\"wp-image-3158\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-13.jpg 950w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-13-300x261.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-13-768x667.jpg 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"608\" height=\"221\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-14.jpg\" alt=\"\" class=\"wp-image-3159\" style=\"width:388px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-14.jpg 608w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/function-14-300x109.jpg 300w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-ab3dcbe1061d17851beae288efa2efcc wp-block-paragraph\" style=\"color:#5c5c5c\">Here ADC use HSI14 RC clock with 14MHz clock frequency. Our logic is very simple, we just call for ADC conversion with interrupt and in ISR (Interrupt Service Routine) we call our main function to get the temperature value.<\/p>\n<\/div>\n<\/div>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nextern ADC_HandleTypeDef hadc;\nvoid HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)\n  {\n    adc_value=HAL_ADC_GetValue(hadc);\n  }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-17550bfc4a2b0ff113ce24ae43f884db wp-block-paragraph\" style=\"color:#5c5c5c\">We learn about digital temperature sensor interface with STm microcontroller, we introduce the sensor here for reference check if the NTC give accurate data or not? We use 0.91OLED for display the two temperature data. Please visit my OLED &amp; DS18B20 section for more details. Here is the changed parameter in main program-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include &quot;ntc.h&quot;\n#include &quot;ssd1306.h&quot;\n#include &quot;DS18B20.h&quot;\nuint8_t temp;\nchar display_digital&#x5B;20],display_analog&#x5B;20];\nvolatile double f_ntc_temp;\ninit_OLED();\nclear_display();\nHAL_ADC_Start_IT(&amp;hadc);\nOLCD_write_string(0,0,&quot;IoT ThingHuB &quot;);\nwhile (1)\n  {\n\tf_ntc_temp=ntc_temperature();\n    temp=DS18B20ReadTemp();\n    HAL_Delay(2000);\n    sprintf(display_digital,&quot;Dig:%.4f C &quot;,temperature);\n    sprintf(display_analog,&quot;NTC:%.4f C  &quot;,ntc_temp);\n    OLCD_write_string(2,0,display_digital);\n    OLCD_write_string(4,0,display_analog);\n    HAL_ADC_Start_IT(&amp;hadc);\n    HAL_Delay(2000);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-9deef5103597a8a25687c1492ff08357 wp-block-paragraph\" style=\"color:#5c5c5c\">Video describe the full details about initialization and procedure. Here we use ADC channel1 >  for NTC value measurement, PB5 > for DS18B20 &amp; I<sup>2<\/sup>C1 for 0.9 &#8221; OLED. Here I used breadboard so some tolerance in temperature measurement.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"NTC sensor vs DS18B20\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/w3EefO8PBbw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\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\">\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 has-background wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/NTC-vs-Digital-Temperature.rar\" style=\"background-color:#1c3755;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--50)\">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\">\n<p class=\"has-text-color has-link-color has-medium-font-size wp-elements-866758b955da72f132e5f356eacfc97e wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/11\/NTC-vs-Digital-Temperature.rar\">NTC vs Digital Temperature.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In ATmel section we learn about ADC (Analog to Digital Converter). In AVR microcontroller we have 10bit ADC: 210 , but in STm microcontroller we have 12bit ADC: 212. In AVR we have 5V as reference voltage that\u2019s work on most sensors, but in STm microcontroller we have ref voltage 3.3V. The voltage 3.3V divide [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3144","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3144","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=3144"}],"version-history":[{"count":19,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions"}],"predecessor-version":[{"id":3175,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions\/3175"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}