{"id":7332,"date":"2013-04-12T23:27:59","date_gmt":"2013-04-13T03:27:59","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=7332"},"modified":"2013-04-12T23:34:17","modified_gmt":"2013-04-13T03:34:17","slug":"mcp9802-temperature-sensor-and-arduino","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/mcp9802-temperature-sensor-and-arduino\/","title":{"rendered":"MCP9802 temperature sensor and Arduino"},"content":{"rendered":"<p><strong><a href=\"http:\/\/www.microchip.com\/wwwproducts\/Devices.aspx?dDocName=en020952\" target=\"_blank\">MCP9802<\/a><\/strong> is a digital temperature sensor from Microchip that measures temperatures between -55\u00b0C and +125\u00b0C to a digital word. It provides an accuracy of \u00b11\u00b0C (maximum) from -10\u00b0C to +85\u00b0C. The MCP9802 sensor comes with user-programmable registers that provide flexibility for temperature sensing applications. The register settings also allow user-selectable 9-bit to 12-bit temperature measurement resolution. This sensor has an industry standard 2-wire I2C compatible serial interface, allowing up to eight devices to be controlled in a single serial bus. In this blog post I am going to write about an Arduino sketch to interface the MCP9802 sensor with an Arduino for temperature sensing application. For illustration, I am using the MCP9802 sensor onboard the <strong><a href=\"http:\/\/embedded-lab.com\/blog\/?p=7201\">I2C EEPROM+Sensor breakout board<\/a><\/strong>.<\/p>\n<p><div id=\"attachment_7352\" style=\"width: 575px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/IMAG1021.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7352\" class=\"size-full wp-image-7352\" title=\"IMAG1021\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/IMAG1021.jpg\" alt=\"\" width=\"565\" height=\"392\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/IMAG1021.jpg 565w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/IMAG1021-550x381.jpg 550w\" sizes=\"auto, (max-width: 565px) 100vw, 565px\" \/><\/a><p id=\"caption-attachment-7352\" class=\"wp-caption-text\">Arduino and MCP9802<\/p><\/div><br \/>\n<!--more--><br \/>\nThe hardware connections between MCP9802 and Arduino is straightforward. Two pull-up resistors are required on the I2C signal lines. On Arduino Uno, the I2C lines are multiplexed with analog pins A4 (SDA) and A5 (SCL).<\/p>\n<div id=\"attachment_7351\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/Arduino_MCP9802.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7351\" class=\"size-full wp-image-7351\" title=\"Arduino_MCP9802\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/Arduino_MCP9802.png\" alt=\"\" width=\"498\" height=\"434\" \/><\/a><p id=\"caption-attachment-7351\" class=\"wp-caption-text\">Hardare connections between MCP9802 and Arduino<\/p><\/div>\n<p>The following Arduino sketch configures the MCP9802 sensor resolution to 12-bit. The Arduino receives the temperature bytes and sends them out to serial port. The values are received and displayed on the serial monitor window on PC. Note that the 7-bit slave addresses of the<br \/>\nMCP9802 sensor on board is 0\u00d748.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #339933;\">#include &lt;Wire.h&gt;<\/span>\n&nbsp;\n<span style=\"color: #339933;\">#define address 0x48   \/\/I2C address of MCP9802<\/span>\n<span style=\"color: #339933;\">#define baudrate 9600  \/\/ Baud rate for serial communication <\/span>\n<span style=\"color: #993333;\">int<\/span> TempByte1<span style=\"color: #339933;\">,<\/span> TempByte2<span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n<span style=\"color: #009900;\">&#123;<\/span>\nWire.<span style=\"color: #202020;\">begin<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\nSerial.<span style=\"color: #202020;\">begin<\/span><span style=\"color: #009900;\">&#40;<\/span>baudrate<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Write to Configuration Register <\/span>\nWire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\nWire.<span style=\"color: #202020;\">write<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x01<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Address to Configuration register<\/span>\nWire.<span style=\"color: #202020;\">write<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x64<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ ADC resolution: 12-bit,<\/span>\nWire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n&nbsp;\n<span style=\"color: #993333;\">void<\/span> loop<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span>\n<span style=\"color: #009900;\">&#123;<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Read temperature <\/span>\nWire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\nWire.<span style=\"color: #202020;\">write<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span>byte<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #208080;\">0x00<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>    <span style=\"color: #666666; font-style: italic;\">\/\/ Pointer to temperature register<\/span>\nWire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\nWire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\nWire.<span style=\"color: #202020;\">requestFrom<\/span><span style=\"color: #009900;\">&#40;<\/span>address<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">2<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Now read two bytes of temperature data<\/span>\n<span style=\"color: #b1b100;\">if<\/span> <span style=\"color: #009900;\">&#40;<\/span>Wire.<span style=\"color: #202020;\">available<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #009900;\">&#123;<\/span>\n  TempByte1 <span style=\"color: #339933;\">=<\/span> Wire.<span style=\"color: #202020;\">read<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  TempByte2 <span style=\"color: #339933;\">=<\/span> Wire.<span style=\"color: #202020;\">read<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #993333;\">int<\/span> Temperature <span style=\"color: #339933;\">=<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#40;<\/span>TempByte1 <span style=\"color: #339933;\">&lt;&lt;<\/span> <span style=\"color: #0000dd;\">8<\/span><span style=\"color: #009900;\">&#41;<\/span> <span style=\"color: #339933;\">|<\/span> TempByte2<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Temperature <span style=\"color: #339933;\">=<\/span> Temperature <span style=\"color: #339933;\">&gt;&gt;<\/span> <span style=\"color: #0000dd;\">4<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #993333;\">float<\/span> TempC <span style=\"color: #339933;\">=<\/span> <span style=\"color:#800080;\">1.0<\/span><span style=\"color: #339933;\">*<\/span>Temperature<span style=\"color: #339933;\">*<\/span><span style=\"color:#800080;\">0.0625<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #993333;\">float<\/span> TempF <span style=\"color: #339933;\">=<\/span> TempC<span style=\"color: #339933;\">*<\/span><span style=\"color:#800080;\">1.8<\/span><span style=\"color: #339933;\">+<\/span><span style=\"color:#800080;\">32.0<\/span><span style=\"color: #339933;\">;<\/span>\n  Serial.<span style=\"color: #202020;\">print<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot; Temperature= &quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Serial.<span style=\"color: #202020;\">print<\/span><span style=\"color: #009900;\">&#40;<\/span>TempC<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Serial.<span style=\"color: #202020;\">print<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot; C, &quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Serial.<span style=\"color: #202020;\">print<\/span><span style=\"color: #009900;\">&#40;<\/span>TempF<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot; F&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n<span style=\"color: #b1b100;\">else<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  Serial.<span style=\"color: #202020;\">println<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #ff0000;\">&quot;No response &quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\nWire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\ndelay<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">1000<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<div id=\"attachment_7353\" style=\"width: 477px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/MCP9802OP.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7353\" class=\"size-full wp-image-7353\" title=\"MCP9802OP\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/04\/MCP9802OP.png\" alt=\"\" width=\"467\" height=\"473\" \/><\/a><p id=\"caption-attachment-7353\" class=\"wp-caption-text\">Temperature readings displayed on Serial Monitor terminal window<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>MCP9802 is a digital temperature sensor from Microchip that measures temperatures between -55\u00b0C and +125\u00b0C to a digital word. It provides an accuracy of \u00b11\u00b0C (maximum) from -10\u00b0C to +85\u00b0C. The MCP9802 sensor comes with user-programmable registers that provide flexibility for temperature sensing applications. The register settings also allow user-selectable 9-bit to 12-bit temperature measurement resolution. This sensor has an industry standard 2-wire I2C compatible serial interface, allowing up to eight devices to be controlled in a single serial bus. In this blog post I am going to write about an Arduino sketch to interface the MCP9802 sensor with an<\/p>\n","protected":false},"author":1,"featured_media":7351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[150,245],"tags":[148,549,102],"class_list":["post-7332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-2","category-embedded-lab-projects","tag-arduino","tag-mcp9802","tag-temperature-sensor"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/7332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=7332"}],"version-history":[{"count":10,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/7332\/revisions"}],"predecessor-version":[{"id":7360,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/7332\/revisions\/7360"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/7351"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=7332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=7332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=7332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}