{"id":360,"date":"2015-05-31T17:02:34","date_gmt":"2015-05-31T21:02:34","guid":{"rendered":"http:\/\/projectsofdan.com\/?p=360"},"modified":"2015-06-10T14:29:23","modified_gmt":"2015-06-10T18:29:23","slug":"digital-dashboard-dissection-temperature-sensors","status":"publish","type":"post","link":"https:\/\/projectsofdan.com\/?p=360","title":{"rendered":"Digital Dashboard Dissection: Temperature Sensors"},"content":{"rendered":"<p>This is the first of a few posts where I&#8217;ll break down a certain subsystem of the digital dashboard and explain in more detail. First up: temp sensors.<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSblockdiag.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-361\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSblockdiag.png\" alt=\"TSblockdiag\" width=\"600\" height=\"363\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSblockdiag.png 724w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSblockdiag-300x181.png 300w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSblockdiag-624x378.png 624w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>The temperature sensing subsystem consists of a few parts. First is the sensor itself. Then, the circuitry on the PCB consists of a buffer, a filter, and an ADC. Lastly, the digitized input is processed in software on the processor.<\/p>\n<p>For temp sensors, I chose the <a href=\"http:\/\/aemelectronics.com\/files\/instructions\/30-2012%20Water%20Temp%20Sensor%20Kit.pdf\" target=\"_blank\">AEM 30-2012<\/a> temperature sensor.<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/302012.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-362 size-medium\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/302012-300x248.jpg\" alt=\"302012\" width=\"300\" height=\"248\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/302012-300x248.jpg 300w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/302012.jpg 425w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>Physically, it is a 1\/8&#8243; NPT thread sensor and comes with the mating connector and terminals. I chose this sensor because its fairly cheap at around $40, is quite accurate (+\/- 1.5C), and comes with good support and documentation from <a href=\"http:\/\/www.aemelectronics.com\" target=\"_blank\">AEM<\/a>.<\/p>\n<p>The sensor works as a variable resistor whose resistance varies with temperature. The datasheet provides a nice table showing how the resistance changes over a broad temperature range. This table will be discussed later in the software section.<\/p>\n<p>Next up is the electrical design consisting of the\u00a0sense circuit, buffering, filtering, and analog-to-digital conversion.<\/p>\n<p>The easiest way to convert a variable resistor to a voltage signal is to put it into a voltage divider using a pull-up resistor. In this configuration, the output voltage will vary with resistance and thus temperature. The AEM 30-2012 datasheet even providers a reference schematic:<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSdivider.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-364 size-medium\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSdivider-300x246.png\" alt=\"TSdivider\" width=\"300\" height=\"246\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSdivider-300x246.png 300w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSdivider.png 466w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>This is the schematic for this part of my board. I&#8217;m using a 2.2k resistor for the pull-up (R3). I also put a jumper between the sensor input (SENS1_IN) and this pullup so that i can disable it if i&#8217;m using a sensor that provides its own voltage output.<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-363 size-full aligncenter\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png\" alt=\"TScircuit\" width=\"532\" height=\"336\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png 532w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit-300x189.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>Using this configuration, the voltage will scale from 0-5V according to the temperature by:<\/p>\n<p>SENS1_IN\u00a0= 5V * ( Rsensor\/(Rsensor + Rpullup) )<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/tempvoltageplot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-365\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/tempvoltageplot.png\" alt=\"tempvoltageplot\" width=\"600\" height=\"471\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/tempvoltageplot.png 624w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/tempvoltageplot-300x236.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Once we have a sensor voltage signal, it&#8217;s a good idea to buffer it. A buffer provides a high impedance input that won&#8217;t load down the sensor. It also provides a low impedance output that is able to drive the next stage. Basically think of it as a divider between the outside world and your processing circuitry. The easiest way to make a buffer is to use an opamp buffer as shown below, U3A.<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-363 size-full\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png\" alt=\"TScircuit\" width=\"532\" height=\"336\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit.png 532w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TScircuit-300x189.png 300w\" sizes=\"auto, (max-width: 532px) 100vw, 532px\" \/><\/a><\/p>\n<p>The next stage is a basic RC low pass filter using R11 and C5. These values create a low pass filter with a cut off at F = 1\/(2*Pi*R*C) = 723hz. This is plenty high for most sensors (this circuit is used for all 8 input channels and is intended to be flexible), but is low enough to filter out most unwanted noise.<\/p>\n<p>Next up is the ADC. For this project i chose the 8 channel, 12-bit, Microchip MCP3208. It&#8217;s SPI bus makes interfacing with the processor easy. 12 bits provides a max value\u00a0of 2^12 = 4096 for a range of 0-4095. So each bit represents 5.0V\/4096bits = 1.22mV\/bit which is more than adequate resolution for this application.<\/p>\n<p>Lastly, we have the software which processes this digital information. Because the temperature sensor is nonlinear, we have to use a lookup table to convert its output voltage (which represents its resistance which represents the temperature) to a temperature. The datasheet gives us:<\/p>\n<p><a href=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSLU1.png\"><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-367 aligncenter\" src=\"http:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSLU1.png\" alt=\"TSLU\" width=\"734\" height=\"500\" srcset=\"https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSLU1.png 734w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSLU1-300x204.png 300w, https:\/\/projectsofdan.com\/wp-content\/uploads\/2015\/05\/TSLU1-624x425.png 624w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/a><\/p>\n<p>Here is the table implemented in code using the digital representation of the voltage and degrees C:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n\/\/ Temp LUT - digital, degC\r\nshort tempLUT&#x5B;39]&#x5B;39] = {\r\n{ 325, 150 },{ 362, 145 },{ 404, 140 },{ 451, 135 },\r\n{ 505, 130 },{ 565, 125 },{ 634, 120 },{ 710, 115 },\r\n{ 796, 110 },{ 892, 105 },{ 1000, 100 },{ 1118, 95 },\r\n{ 1249, 90 },{ 1391, 85 },{ 1544, 80 },{ 1708, 75 },\r\n{ 1882, 70 },{ 2063, 65 },{ 2248, 60 },{ 2435, 55 },\r\n{ 2621, 50 },{ 2803, 45 },{ 2976, 40 },{ 3138, 35 },\r\n{ 3287, 30 },{ 3421, 25 },{ 3540, 20 },{ 3644, 15 },\r\n{ 3732, 10 },{ 3807, 5 },{ 3869, 0 },{ 3920, -5 },\r\n{ 3961, -10 },{ 3993, -15 },{ 4018, -20 },{ 4038, -25 },\r\n{ 4053, -30 },{ 4065, -35 },{ 4074, -40 }};\r\n\r\n<\/pre>\n<p>The following code converts\u00a0the digitized voltage input to a temperature\u00a0using the lookup table. First it searches the lookup table for the closest match that less than the input value. The input value must then be between this match and the next one higher. It then linearly interpolates between these two table entries\u00a0to find the equivalent\u00a0temperature.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\r\n\/* Temp Sensor Look Up Table\r\n* Returns Temp in C\r\n*\/\r\nfloat ToTemperature(unsigned short adcIn) {\r\n\r\nshort lowerI = 0;\r\nshort upperI = 0;\r\nfor(short i=0; i&amp;amp;amp;lt;39; i++) {\r\n   if(tempLUT&#x5B;i]&#x5B;0] &amp;amp;amp;lt;= adcIn ){\r\n      lowerI = i;\r\n   }\r\n   else{\r\n      upperI = i;\r\n      break;\r\n   }\r\n}\r\nreturn tempLUT&#x5B;lowerI]&#x5B;1] + (float)( tempLUT&#x5B;upperI]&#x5B;1] - tempLUT&#x5B;lowerI]&#x5B;1] )*( adcIn - tempLUT&#x5B;lowerI]&#x5B;0] )\/(float)( tempLUT&#x5B;upperI]&#x5B;0] - tempLUT&#x5B;lowerI]&#x5B;0] );\r\n}\r\n\r\n<\/pre>\n<p>And that&#8217;s about how it works, from sensor to computed value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first of a few posts where I&#8217;ll break down a certain subsystem of the digital dashboard and explain in more detail. First up: temp sensors. The temperature sensing subsystem consists of a few parts. First is the sensor itself. Then, the circuitry on the PCB consists of a buffer, a filter, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-digital-dashboard-electronics-robotics"],"_links":{"self":[{"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=360"}],"version-history":[{"count":14,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=\/wp\/v2\/posts\/360\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/projectsofdan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projectsofdan.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}