{"id":2159,"date":"2024-07-15T13:46:06","date_gmt":"2024-07-15T13:46:06","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2159"},"modified":"2024-08-11T17:46:52","modified_gmt":"2024-08-11T17:46:52","slug":"io-port-expender-mcp23017","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2159","title":{"rendered":"IO Port Expender MCP23017"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">In SPI communication we use MCP23S17 with SPI transmission. In this article we use same IC MCP23017 with I<sup>2<\/sup>C transmission. Let\u2019s look at the connection diagram.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"332\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23017-1024x332.jpg\" alt=\"\" class=\"wp-image-2161\" style=\"width:740px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23017-1024x332.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23017-300x97.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23017-768x249.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23017.jpg 1380w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">The I<sup>2<\/sup>C protocol for MCP23017 is as follow-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"62\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1-1024x62.jpg\" alt=\"\" class=\"wp-image-2164\" style=\"width:743px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1-1024x62.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1-300x18.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1-768x47.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1.jpg 1399w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Here the first 4 bits is the logical address and last 3 bits A<sub>2<\/sub>: A<sub>0 <\/sub>are the physical address. Since we have 2<sup>3<\/sup> = 8 combination, we can connect up to 8 MCP23017 IC at the same time. If A<sub>2 <\/sub>= 0, A<sub>1<\/sub> = 0 and A<sub>0 <\/sub>= 0 then the device has-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"65\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-1-1024x65.jpg\" alt=\"\" class=\"wp-image-2167\" style=\"width:693px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-1-1024x65.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-1-300x19.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-1-768x49.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-1.jpg 1352w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">For write operation the I<sup>2<\/sup>C protocol is-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"46\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1-1024x46.jpg\" alt=\"\" class=\"wp-image-2170\" style=\"width:757px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1-1024x46.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1-300x13.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1-768x34.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1.jpg 1369w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-5 wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Write_IO(char add,char data) -&gt; Write Data<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define IOEXPENDER_Write_Addess\t\t0x40 \/\/0b0100A2A1A00\nuint8_t I2C_Write_IO(char add,char data)\n{\n\tI2C_Start();\n\tI2C_Write(IOEXPENDER_Write_Addess);\n\tI2C_Write(add);\n\tI2C_Write(data);\n\tI2C_Stop();\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">For read operation the I<sup>2<\/sup>C protocol is-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"73\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1-1024x73.jpg\" alt=\"\" class=\"wp-image-2174\" style=\"width:772px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1-1024x73.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1-300x22.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1-768x55.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1.jpg 1366w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-7 wp-block-paragraph\" style=\"color:#6c8a97\">uint8_t I2C_Read_IO(char add) -&gt; Read Data<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define IOEXPENDER_Read_Addess\t\t0x41 \/\/0b0100A2A1A01\nuint8_t I2C_Read_IO(char add)\n{\n    uint8_t value;\n\tI2C_Start();\n\tI2C_Write(IOEXPENDER_Write_Addess);\n\tI2C_Write(add);\n\tI2C_Start();\n\tI2C_Write(IOEXPENDER_Read_Addess);\n\tvalue=I2C_Read_Nack();\n\tI2C_Stop();\n\treturn value;\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">We develop the read and write the function. Let\u2019s modify the program of I\/O port Expender in SPI section program with I<sup>2<\/sup>C protocol.<\/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;avr\/io.h&gt;\n  #include&lt;util\/delay.h&gt;\n  #include&lt;util\/twi.h&gt;\n  #include&quot;IO_Expender.h&quot;\nint main(void)\n {\n  twi_init();\n  uint8_t pushButton,shift=1;\n  \/******************** initialize MCP23017 *******************\/\n  \/\/ I\/O Control Register: BANK=0, SEQOP=1, HAEN=1(Enable Addressing)\n  I2C_Write_IO(IOCONA,0x28);\n  I2C_Write_IO(IODIRA,0x00); \/\/GPIOA as output\n  I2C_Write_IO(IODIRB,0xff); \/\/GPIOB as input\n  I2C_Write_IO(GPPUB,0xff);  \/\/enable pull-up resister for GPB\n  _delay_ms(5);\n  \/*************************************************************\/\n  while(1)\n\t{\n\t pushButton=I2C_Read_IO(GPIOB);\n\t if(pushButton == 0b11111011)\n\t {\n\t\tfor(int i=7;i&gt;=0;i--)\n\t\t{\n\t\t\tI2C_Write_IO(GPIOA,(1&lt;&lt;i));\n\t\t\t_delay_ms(500);\n\t\t}\n\t }else \n\t\tI2C_Write_IO(GPIOA,shift);\n\t\t_delay_ms(500);\n\t\tshift=(shift&lt;&lt;1);\n\t\tif(shift == 0)\n\t\tshift=1;\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\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/IO-Expender-1.rar\" style=\"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-text-color has-link-color has-upper-heading-font-size wp-elements-9 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/IO-Expender-1.rar\">IO Expender.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In SPI communication we use MCP23S17 with SPI transmission. In this article we use same IC MCP23017 with I2C transmission. Let\u2019s look at the connection diagram. The I2C protocol for MCP23017 is as follow- Here the first 4 bits is the logical address and last 3 bits A2: A0 are the physical address. Since we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-2159","post","type-post","status-publish","format-standard","hentry","category-i2c"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2159","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=2159"}],"version-history":[{"count":9,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2159\/revisions"}],"predecessor-version":[{"id":2642,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2159\/revisions\/2642"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}