{"id":8232,"date":"2014-04-20T22:34:31","date_gmt":"2014-04-21T02:34:31","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=8232"},"modified":"2014-04-20T22:34:49","modified_gmt":"2014-04-21T02:34:49","slug":"chipkit-tutorial-6-inter-integrated-circuit-i2c-communication","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/chipkit-tutorial-6-inter-integrated-circuit-i2c-communication\/","title":{"rendered":"chipKIT Tutorial 6: Inter-Integrated Circuit (I2C) communication"},"content":{"rendered":"<p>I<sup>2<\/sup>C or IIC (Inter-Integrated Circuit) is a simple bidirectional serial interface, which requires only 2 signal lines for data transfer. It was originally developed by Philips in 1980\u2032s to provide easy on-board communications between a CPU and various peripheral chips in a TV set. Today, it is widely used in varieties of embedded systems to connect many low speed peripherals, such as external EEPROMs, sensors, LCD drivers, port expanders, real-time clocks, etc, to the host microcontroller. In this tutorial, we will explore the chipKIT Wire Library for establishing an I<sup>2<\/sup>C communication link between the chipKIT Uno32 board and two I<sup>2<\/sup>C sensors. The Uno32 board receives the sensor outputs through the I<sup>2<\/sup>C link and displays the results on the serial monitor window on the computer screen.<\/p>\n<p><div id=\"attachment_8495\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8495\" rel=\"attachment wp-att-8495\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8495\" class=\"size-full wp-image-8495\" alt=\"I2C communication demo\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setuptitle.jpg\" width=\"580\" height=\"398\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setuptitle.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setuptitle-550x377.jpg 550w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-8495\" class=\"wp-caption-text\">I2C communication demo<\/p><\/div><!--more--><br \/>\n<strong>Theory<\/strong><\/p>\n<p>An I<sup>2<\/sup>C bus consists of two signal lines called SDA (data) and SCL (clock). Just line Arduino Uno, the chipKIT Uno32 board offers the SDA and SCL connections via analogue input pins A4 and A5 respectively.\u00a0Each device connected to the I2C\u00a0bus is software addressable by an unique 7-bit address. Multiple I2C devices can be connected to the same I2C bus as long as they have different addresses. A\u00a0simple Master and Slave relationship exist at all times between the communicating devices. The\u00a0Master device initiates a data transfer, generates clock signals and terminates the\u00a0transfer. Any device addressed by the Master at any time is\u00a0considered a slave. Both SDA and SCL are open-drain lines, and therefore, require two pull-up resistors during operation. A more detail look of the I2C Bus protocol and how the exchange of information takes place between Master and Slave has been discussed in one of my previous tutorials, <a href=\"http:\/\/embedded-lab.com\/blog\/?p=2583\" target=\"_blank\"><strong>Inter-Integrated Communication in PIC MCU<\/strong><\/a>. So I am not going to repeat that here again.<\/p>\n<div style=\"width: 630px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2011\/05\/MutipleI2C_Devices.png\" width=\"620\" height=\"392\" \/><p class=\"wp-caption-text\">Source: http:\/\/embedded-lab.com\/blog\/?p=2583<\/p><\/div>\n<p>In this tutorial, we are going to experiment with two digital temperature sensors that support I2C communication. They are <a href=\"http:\/\/datasheets.maximintegrated.com\/en\/ds\/DS1631-DS1731.pdf\" target=\"_blank\"><strong>Maxim&#8217;s DS1631<\/strong><\/a> and <a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/21462D.pdf\" target=\"_blank\"><strong>Microchip&#8217;s TC74<\/strong><\/a>\u00a0devices.<\/p>\n<p>The DS1631 is a digital thermometer that provides configurable 9-, 10-, 11-, or 12-bit temperature\u00a0readings over a -55\u00b0C to +125\u00b0C range. Some of its features are highlighted below. The 7-bit I2C address of DS1631 is configurable by hardwiring the A0, A1, and A2 pins to VCC (1) or ground (0).<\/p>\n<div id=\"attachment_8503\" style=\"width: 766px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8503\" rel=\"attachment wp-att-8503\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8503\" class=\"size-full wp-image-8503\" alt=\"DS1631 features\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/DS1631_spec.jpg\" width=\"756\" height=\"387\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/DS1631_spec.jpg 756w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/DS1631_spec-550x281.jpg 550w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><\/a><p id=\"caption-attachment-8503\" class=\"wp-caption-text\">DS1631 features<\/p><\/div>\n<p>Similarly, the TC74 device is also a serially accessible digital temperature sensor that provides a temperature measurement as an 8-bit word. The resolution is thus 1 degree Celsius. Its features are highlighted below. It should be noted that there are no external pins for configuring the I2C address. This means that the 7-bit I2C address of TC74 is fixed and set by manufacturer. Eight versions of TC74 are available, namely TC74A0 through TC74A7, with different I2C address values. I am using TC74A0, which has the 7-bit address fixed to 1001000 (0x48).<\/p>\n<div id=\"attachment_8504\" style=\"width: 589px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8504\" rel=\"attachment wp-att-8504\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8504\" class=\"size-full wp-image-8504\" alt=\"TC74A0 features\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/TC74A0_spec.jpg\" width=\"579\" height=\"520\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/TC74A0_spec.jpg 579w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/TC74A0_spec-550x493.jpg 550w\" sizes=\"auto, (max-width: 579px) 100vw, 579px\" \/><\/a><p id=\"caption-attachment-8504\" class=\"wp-caption-text\">TC74 features<\/p><\/div>\n<p>Please read the datasheets of\u00a0<a href=\"http:\/\/datasheets.maximintegrated.com\/en\/ds\/DS1631-DS1731.pdf\" target=\"_blank\"><strong>DS1631<\/strong><\/a>\u00a0and\u00a0<a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/21462D.pdf\" target=\"_blank\"><strong>TC74<\/strong><\/a>\u00a0for more information on these devices.<\/p>\n<p><strong>Circuit Setup<\/strong><\/p>\n<p>Perhaps you have noticed that the four most significant bits of the I2C addresses of DS1631 and TC74A0 are same (1001). The remaining address bits of TC74A0 are fixed (000). But the same bits are configurable for DS1631 through its hardware pins. We will ground the pins A1 and A2, and pull-up pin A0 to VCC so that the I2C address of DS1631 would be 1001001 (0x49). We build the following circuit on a breadboard and power it through the 3.3V power supply from the Uno32 board. The SDA and SCL signal lines are connected to analog input pins A4 and A5, respectively. Note that the jumpers JP6 and JP8 must be placed in the RG3 and RG2 position in order to use these pins for I2C operation. See <a href=\"http:\/\/embedded-lab.com\/blog\/?p=4444\" target=\"_blank\"><strong>Exploring chipKIT Uno32<\/strong><\/a>\u00a0for more details on the functions of various shunt jumpers on the Uno32 board.\u00a0The pull-up resistors for the I2C signal lines can be of value anywhere between 1K and 10K, at least for this experiment.<\/p>\n<div id=\"attachment_8507\" style=\"width: 578px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8507\" rel=\"attachment wp-att-8507\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8507\" class=\"size-full wp-image-8507\" alt=\"Circuit setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/T6_Setupcircuit.jpg\" width=\"568\" height=\"513\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/T6_Setupcircuit.jpg 568w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/04\/T6_Setupcircuit-550x496.jpg 550w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/a><p id=\"caption-attachment-8507\" class=\"wp-caption-text\">Circuit diagram for connecting DS1631 and TC74A0 to chipKIT Uno32<\/p><\/div>\n<div id=\"attachment_8493\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8493\" rel=\"attachment wp-att-8493\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8493\" class=\"size-full wp-image-8493\" alt=\"Two I2C devices on a common bus\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup.jpg\" width=\"580\" height=\"467\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup-550x442.jpg 550w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-8493\" class=\"wp-caption-text\">Complete circuit layout on breadboard<\/p><\/div>\n<p><strong>chipKIT Sketch<\/strong><\/p>\n<p>The Arduino&#8217;s standard <a href=\"http:\/\/arduino.cc\/en\/Reference\/Wire\" target=\"_blank\"><strong>Wire library<\/strong><\/a> is also implemented in MPIDE, which allows chipKIT Uno32 to communicate with\u00a0I2C devices. The Wire library uses 7 bit addresses for addressing slave devices on the I2C bus. Following are the main functions available in the Wire library.<\/p>\n<p><em><strong>Wire.begin()<\/strong><\/em> initializes the Wire library and configures analog pins A4 and A5 as I2C\u00a0pins.\u00a0This should normally be called only once.<\/p>\n<p><em><strong>Wire.beginTransmission(address)<\/strong><\/em>\u00a0begins a transmission to the\u00a0I2C\u00a0slave device with the given 7-bit address.<\/p>\n<p><em><strong>Wire.send(data)<\/strong><\/em>\u00a0sends data to the slave device.<\/p>\n<p><em><strong>Wire.requestFrom(address, count)<\/strong><\/em> is used by Master to request a specific number of bytes from a slave device.<\/p>\n<p><em><strong>Wire.receive()<\/strong><\/em> is used by Master to retrieve a byte that was transmitted from a slave device after a call to\u00a0<em><strong>Wire.requestFrom(address, count)<\/strong><\/em><\/p>\n<p><em><strong>Wire.endTransmission()<\/strong><\/em>\u00a0ends the data transmission.<\/p>\n<p><span style=\"font-size: 0.95em; line-height: 1.6em;\"><em><strong>Wire.available()<\/strong><\/em>\u00a0r<\/span><span style=\"font-size: 0.95em; line-height: 1.6em;\">eturns the number of bytes available for retrieval with\u00a0<em><strong>Wire.receive()<\/strong><\/em><\/span><span style=\"font-size: 0.95em; line-height: 1.6em;\">.<\/span><\/p>\n<p><span style=\"font-size: 0.95em; line-height: 1.6em;\">More details on the chipKIT specific Wire library can be found <a href=\"http:\/\/www.eecs.wsu.edu\/~schneidj\/chipKIT-reference\/Wire.html\" target=\"_blank\"><strong>here<\/strong><\/a>.<\/span><\/p>\n<p><span style=\"font-size: 0.95em; line-height: 1.6em;\">Here&#8217;s the sketch written for requesting temperature readings from the DS1631 and TC74A0 devices and display them on the Serial Monitor window.<\/span><\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"c\" style=\"font-family:monospace;\"><span style=\"color: #808080; font-style: italic;\">\/*\n  Tutorial 6:  I2C communication demo \n  Description: Explore the I2C serial communication protocol and interface\n               two I2C slave devices to chipKIT Uno32 using a common I2C bus.\n               Two devices: DS1631 and TC74A0 temperature sensors. \n  Board: chipKIT UNO32\n&nbsp;\n  Connect SDA to Arduino pin A4 (SDA)\n  Connect SCL to Arduino pin A5 (SCL) \n&nbsp;\n  Written by: Raj Bhatt (April 2014)\n  www.embedded-lab.com \n*\/<\/span>\n&nbsp;\n<span style=\"color: #339933;\">#include &lt;Wire.h&gt;<\/span>\n&nbsp;\n<span style=\"color: #993333;\">int<\/span> DS1631_Address <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x49<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ 7-bit I2C address for DS1631<\/span>\n<span style=\"color: #993333;\">int<\/span> TC74A0_Address <span style=\"color: #339933;\">=<\/span> <span style=\"color: #208080;\">0x48<\/span><span style=\"color: #339933;\">;<\/span> <span style=\"color: #666666; font-style: italic;\">\/\/ 7-bit I2C address for TC74A0<\/span>\n&nbsp;\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ setup loop<\/span>\n<span style=\"color: #993333;\">void<\/span> setup<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  Serial.<span style=\"color: #202020;\">begin<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">9600<\/span><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;IIC Test: Temperature sensors&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span> \n  Wire.<span style=\"color: #202020;\">begin<\/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<span style=\"color: #666666; font-style: italic;\">\/\/ Main Loop<\/span>\n<span style=\"color: #993333;\">void<\/span> loop<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #993333;\">float<\/span> temp1 <span style=\"color: #339933;\">=<\/span> Read_Temp_DS1631<span style=\"color: #009900;\">&#40;<\/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><span style=\"color: #ff0000;\">&quot;DS1631 reading: &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>temp1<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">4<\/span><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; degC&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  <span style=\"color: #993333;\">float<\/span> temp2 <span style=\"color: #339933;\">=<\/span> Read_Temp_TC74A0<span style=\"color: #009900;\">&#40;<\/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><span style=\"color: #ff0000;\">&quot;TC74A0 reading: &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>temp2<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">2<\/span><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; degC&quot;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n&nbsp;\n  delay<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>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Function sub-routine for reading temperature from DS1631<\/span>\n<span style=\"color: #993333;\">float<\/span> Read_Temp_DS1631<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #993333;\">float<\/span> tempC1<span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #993333;\">int<\/span> MSbyte1<span style=\"color: #339933;\">,<\/span> LSbyte1<span style=\"color: #339933;\">;<\/span>             \n  Wire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>DS1631_Address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Wire.<span style=\"color: #202020;\">send<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0x51<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Send Start Convert command      <\/span>\n  Wire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  delay<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #0000dd;\">1000<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>         <span style=\"color: #666666; font-style: italic;\">\/\/ Wait until temperature conversion is done<\/span>\n  Wire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>DS1631_Address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Wire.<span style=\"color: #202020;\">send<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #208080;\">0xAA<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>     <span style=\"color: #666666; font-style: italic;\">\/\/ Send Read Temperature Command<\/span>\n  Wire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  \n  Wire.<span style=\"color: #202020;\">requestFrom<\/span><span style=\"color: #009900;\">&#40;<\/span>DS1631_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;\">\/\/ Request two temperature bytes<\/span>\n  <span style=\"color: #b1b100;\">while<\/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: #666666; font-style: italic;\">\/\/<\/span>\n  <span style=\"color: #009900;\">&#123;<\/span>\n    MSbyte1 <span style=\"color: #339933;\">=<\/span> Wire.<span style=\"color: #202020;\">receive<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n    LSbyte1 <span style=\"color: #339933;\">=<\/span> Wire.<span style=\"color: #202020;\">receive<\/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  Wire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  \n  LSbyte1 <span style=\"color: #339933;\">=<\/span> LSbyte1<span style=\"color: #339933;\">&gt;&gt;<\/span><span style=\"color: #0000dd;\">4<\/span><span style=\"color: #339933;\">;<\/span>\n  tempC1 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">float<\/span><span style=\"color: #009900;\">&#41;<\/span>MSbyte1 <span style=\"color: #339933;\">+<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">float<\/span><span style=\"color: #009900;\">&#41;<\/span>LSbyte1<span style=\"color: #339933;\">*<\/span><span style=\"color:#800080;\">0.0625<\/span><span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">return<\/span> tempC1<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span>\n&nbsp;\n<span style=\"color: #666666; font-style: italic;\">\/\/ Function sub-routine for reading temperature from TC74A0<\/span>\n<span style=\"color: #993333;\">float<\/span> Read_Temp_TC74A0<span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #009900;\">&#123;<\/span>\n  <span style=\"color: #993333;\">float<\/span> tempC2<span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #993333;\">int<\/span> MSbyte2<span style=\"color: #339933;\">;<\/span>             \n  Wire.<span style=\"color: #202020;\">beginTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span>TC74A0_Address<span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Wire.<span style=\"color: #202020;\">send<\/span><span style=\"color: #009900;\">&#40;<\/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;\">\/\/ Send Read Temperature command<\/span>\n  Wire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>\n  Wire.<span style=\"color: #202020;\">requestFrom<\/span><span style=\"color: #009900;\">&#40;<\/span>TC74A0_Address<span style=\"color: #339933;\">,<\/span> <span style=\"color: #0000dd;\">1<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  <span style=\"color: #666666; font-style: italic;\">\/\/ Request a temperature byte<\/span>\n  <span style=\"color: #b1b100;\">while<\/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: #666666; font-style: italic;\">\/\/<\/span>\n  <span style=\"color: #009900;\">&#123;<\/span>\n    MSbyte2 <span style=\"color: #339933;\">=<\/span> Wire.<span style=\"color: #202020;\">receive<\/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  Wire.<span style=\"color: #202020;\">endTransmission<\/span><span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #009900;\">&#41;<\/span><span style=\"color: #339933;\">;<\/span>  \n  tempC2 <span style=\"color: #339933;\">=<\/span> <span style=\"color: #009900;\">&#40;<\/span><span style=\"color: #993333;\">float<\/span><span style=\"color: #009900;\">&#41;<\/span>MSbyte2<span style=\"color: #339933;\">;<\/span>\n  <span style=\"color: #b1b100;\">return<\/span> tempC2<span style=\"color: #339933;\">;<\/span>\n<span style=\"color: #009900;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p><a href=\"http:\/\/www.embedded-lab.com\/uploads\/chipKIT\/T9_I2C.zip\" target=\"_blank\"><strong>Download the complete sketch<\/strong><\/a><\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Upload the above sketch to the Uno32 board and then open the Serial Monitor window to display the temperature readings from the two sensors.<\/p>\n<div id=\"attachment_8494\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8494\" rel=\"attachment wp-att-8494\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8494\" class=\"size-full wp-image-8494\" alt=\"Complete experimental setup\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup1.jpg\" width=\"580\" height=\"423\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup1.jpg 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_Setup1-550x401.jpg 550w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><p id=\"caption-attachment-8494\" class=\"wp-caption-text\">Complete experimental setup<\/p><\/div>\n<p>The two temperature sensor readings seem to agree within 2 degree Celsius. DS1631 has better accuracy and resolution than TC74A0.<\/p>\n<div id=\"attachment_8492\" style=\"width: 588px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=8492\" rel=\"attachment wp-att-8492\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-8492\" class=\"size-full wp-image-8492\" alt=\"Temperature outputs from DS1631 and TC74A0\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_IIC_op.png\" width=\"578\" height=\"504\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_IIC_op.png 578w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2014\/01\/T6_IIC_op-550x479.png 550w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><p id=\"caption-attachment-8492\" class=\"wp-caption-text\">Temperature outputs from DS1631 and TC74A0<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I2C or IIC (Inter-Integrated Circuit) is a simple bidirectional serial interface, which requires only 2 signal lines for data transfer. It was originally developed by Philips in 1980\u2032s to provide easy on-board communications between a CPU and various peripheral chips in a TV set. Today, it is widely used in varieties of embedded systems to connect many low speed peripherals, such as external EEPROMs, sensors, LCD drivers, port expanders, real-time clocks, etc, to the host microcontroller. In this tutorial, we will explore the chipKIT Wire Library for establishing an I2C communication link between the chipKIT Uno32 board and two I2C<\/p>\n","protected":false},"author":1,"featured_media":8492,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[401],"tags":[1314,268,480,755],"class_list":["post-8232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chipkit","tag-chipkit","tag-ds1631","tag-i2c","tag-tc74a0"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/8232","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=8232"}],"version-history":[{"count":16,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/8232\/revisions"}],"predecessor-version":[{"id":8513,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/8232\/revisions\/8513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/8492"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=8232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=8232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=8232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}