{"id":814,"date":"2020-10-30T20:56:26","date_gmt":"2020-10-30T13:56:26","guid":{"rendered":"https:\/\/www.makeriot2020.com\/?p=814"},"modified":"2022-05-28T15:08:34","modified_gmt":"2022-05-28T08:08:34","slug":"mcp23017-with-adafruit-library","status":"publish","type":"post","link":"https:\/\/www.makeriot2020.com\/index.php\/2020\/10\/30\/mcp23017-with-adafruit-library\/","title":{"rendered":"MCP23017 with Adafruit Library"},"content":{"rendered":"\n<p>In a previous <a href=\"http:\/\/makeriot2020.com\/index.php\/2020\/10\/13\/using-the-mcp23017-to-increase-your-gpios\/\" target=\"_blank\" rel=\"noreferrer noopener\">post<\/a>, I have shown you how to use the MCP23017 16 Port I2C I\/O Port extender with the standard Wire library, as supplied with the Arduino IDE. In this post,<br>I will have a quick look at using Adafruit&#8217;s library for this IC. I believe that this library brings a lot of ease-of-use to the part, making it possible to obscure some of the complexity of I2C.<br><br>I do however prefer to use the native Wire library myself, as it is slightly faster.<br><br>You can download the <a data-type=\"URL\" data-id=\"https:\/\/github.com\/adafruit\/Adafruit-MCP23017-Arduino-Library\" href=\"https:\/\/github.com\/adafruit\/Adafruit-MCP23017-Arduino-Library\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit MCP23017 Library from here<\/a>..<br><br><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Pin Addressing<\/h1>\n\n\n\n<p>When using single pin operations such as&nbsp;<em>pinMode(pinId, dir)<\/em>&nbsp;or&nbsp;<em>digitalRead(pinId)<\/em>&nbsp;or&nbsp;<em>digitalWrite(pinId, val)<\/em>&nbsp;then the pins are addressed using the ID&#8217;s below. For example, for set the mode of&nbsp;<em>GPB0<\/em>&nbsp;then use&nbsp;<em>pinMode(8, &#8230;)<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Physical Pin #<\/th><th>Pin Name<\/th><th>Pin ID<\/th><\/tr><\/thead><tbody><tr><td>21<\/td><td>GPA0<\/td><td>0<\/td><\/tr><tr><td>22<\/td><td>GPA1<\/td><td>1<\/td><\/tr><tr><td>23<\/td><td>GPA2<\/td><td>2<\/td><\/tr><tr><td>24<\/td><td>GPA3<\/td><td>3<\/td><\/tr><tr><td>25<\/td><td>GPA4<\/td><td>4<\/td><\/tr><tr><td>26<\/td><td>GPA5<\/td><td>5<\/td><\/tr><tr><td>27<\/td><td>GPA6<\/td><td>6<\/td><\/tr><tr><td>28<\/td><td>GPA7<\/td><td>7<\/td><\/tr><tr><td>1<\/td><td>GPB0<\/td><td>8<\/td><\/tr><tr><td>2<\/td><td>GPB1<\/td><td>9<\/td><\/tr><tr><td>3<\/td><td>GPB2<\/td><td>10<\/td><\/tr><tr><td>4<\/td><td>GPB3<\/td><td>11<\/td><\/tr><tr><td>5<\/td><td>GPB4<\/td><td>12<\/td><\/tr><tr><td>6<\/td><td>GPB5<\/td><td>13<\/td><\/tr><tr><td>7<\/td><td>GPB6<\/td><td>14<\/td><\/tr><tr><td>8<\/td><td>GPB7<\/td><td>15<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Some examples<\/strong>, directly from the library, all code belongs to Adafruit, and was not written by me.<br><br>1. A Button Example<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h&gt;\n#include \"Adafruit_MCP23017.h\"\n\n\/\/ Basic pin reading and pullup test for the MCP23017 I\/O expander\n\/\/ public domain!\n\n\/\/ Connect pin #12 of the expander to Analog 5 (i2c clock)\n\/\/ Connect pin #13 of the expander to Analog 4 (i2c data)\n\/\/ Connect pins #15, 16 and 17 of the expander to ground (address selection)\n\/\/ Connect pin #9 of the expander to 5V (power)\n\/\/ Connect pin #10 of the expander to ground (common ground)\n\/\/ Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)\n\n\/\/ Input #0 is on pin 21 so connect a button or switch from there to ground\n\nAdafruit_MCP23017 mcp;\n  \nvoid setup() {  \n  mcp.begin();      \/\/ use default address 0\n\n  mcp.pinMode(0, INPUT);\n  mcp.pullUp(0, HIGH);  \/\/ turn on a 100K pullup internally\n\n  pinMode(13, OUTPUT);  \/\/ use the p13 LED as debugging\n}\n\n\n\nvoid loop() {\n  \/\/ The LED will 'echo' the button\n  digitalWrite(13, mcp.digitalRead(0));\n}<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>2. An Interrupt Example<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>\/\/ Install the LowPower library for optional sleeping support.\n\/\/ See loop() function comments for details on usage.\n\/\/#include &lt;LowPower.h&gt;\n\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_MCP23017.h&gt;\n\nAdafruit_MCP23017 mcp;\n\nbyte ledPin=13;\n\n\/\/ Interrupts from the MCP will be handled by this PIN\nbyte arduinoIntPin=3;\n\n\/\/ ... and this interrupt vector\nbyte arduinoInterrupt=1;\n\nvolatile boolean awakenByInterrupt = false;\n\n\/\/ Two pins at the MCP (Ports A\/B where some buttons have been setup.)\n\/\/ Buttons connect the pin to grond, and pins are pulled up.\nbyte mcpPinA=7;\nbyte mcpPinB=15;\n\nvoid setup(){\n\n  Serial.begin(9600);\n  Serial.println(\"MCP23007 Interrupt Test\");\n\n  pinMode(arduinoIntPin,INPUT);\n\n  mcp.begin();      \/\/ use default address 0\n  \n  \/\/ We mirror INTA and INTB, so that only one line is required between MCP and Arduino for int reporting\n  \/\/ The INTA\/B will not be Floating \n  \/\/ INTs will be signaled with a LOW\n  mcp.setupInterrupts(true,false,LOW);\n\n  \/\/ configuration for a button on port A\n  \/\/ interrupt will triger when the pin is taken to ground by a pushbutton\n  mcp.pinMode(mcpPinA, INPUT);\n  mcp.pullUp(mcpPinA, HIGH);  \/\/ turn on a 100K pullup internally\n  mcp.setupInterruptPin(mcpPinA,FALLING); \n\n  \/\/ similar, but on port B.\n  mcp.pinMode(mcpPinB, INPUT);\n  mcp.pullUp(mcpPinB, HIGH);  \/\/ turn on a 100K pullup internall\n  mcp.setupInterruptPin(mcpPinB,FALLING);\n\n  \/\/ We will setup a pin for flashing from the int routine\n  pinMode(ledPin, OUTPUT);  \/\/ use the p13 LED as debugging\n  \n}\n\n\/\/ The int handler will just signal that the int has happen\n\/\/ we will do the work from the main loop.\nvoid intCallBack(){\n  awakenByInterrupt=true;\n}\n\nvoid handleInterrupt(){\n  \n  \/\/ Get more information from the MCP from the INT\n  uint8_t pin=mcp.getLastInterruptPin();\n  uint8_t val=mcp.getLastInterruptPinValue();\n  \n  \/\/ We will flash the led 1 or 2 times depending on the PIN that triggered the Interrupt\n  \/\/ 3 and 4 flases are supposed to be impossible conditions... just for debugging.\n  uint8_t flashes=4; \n  if(pin==mcpPinA) flashes=1;\n  if(pin==mcpPinB) flashes=2;\n  if(val!=LOW) flashes=3;\n\n  \/\/ simulate some output associated to this\n  for(int i=0;i&lt;flashes;i++){  \n    delay(100);\n    digitalWrite(ledPin,HIGH);\n    delay(100);\n    digitalWrite(ledPin,LOW);\n  }\n\n  \/\/ we have to wait for the interrupt condition to finish\n  \/\/ otherwise we might go to sleep with an ongoing condition and never wake up again.\n  \/\/ as, an action is required to clear the INT flag, and allow it to trigger again.\n  \/\/ see datasheet for datails.\n  while( ! (mcp.digitalRead(mcpPinB) &amp;&amp; mcp.digitalRead(mcpPinA) ));\n  \/\/ and clean queued INT signal\n  cleanInterrupts();\n}\n\n\/\/ handy for interrupts triggered by buttons\n\/\/ normally signal a few due to bouncing issues\nvoid cleanInterrupts(){\n  EIFR=0x01;\n  awakenByInterrupt=false;\n}  \n\n\/**\n * main routine: sleep the arduino, and wake up on Interrups.\n * the LowPower library, or similar is required for sleeping, but sleep is simulated here.\n * It is actually posible to get the MCP to draw only 1uA while in standby as the datasheet claims,\n * however there is no stadndby mode. Its all down to seting up each pin in a way that current does not flow.\n * and you can wait for interrupts while waiting.\n *\/\nvoid loop(){\n  \n  \/\/ enable interrupts before going to sleep\/wait\n  \/\/ And we setup a callback for the arduino INT handler.\n  attachInterrupt(arduinoInterrupt,intCallBack,FALLING);\n  \n  \/\/ Simulate a deep sleep\n  while(!awakenByInterrupt);\n  \/\/ Or sleep the arduino, this lib is great, if you have it.\n  \/\/LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);\n  \n  \/\/ disable interrupts while handling them.\n  detachInterrupt(arduinoInterrupt);\n  \n  if(awakenByInterrupt) handleInterrupt();\n}\n\n\nI hope that this shows you another way of using this versatile IC, \nIn a future post, I will show you how to do interrupts, using the native Wire library, as well as point out a few things about why interrrupts sometimes does not seem to be working, as well as a workaround for that.\n<\/code><\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, I have shown you how to use the MCP23017 16 Port I2C I\/O Port extender with the standard Wire library, as supplied with the Arduino IDE. In this post,I will have a quick look at using Adafruit&#8217;s library for this IC. I believe that this library brings a lot of ease-of-use &hellip; <a href=\"https:\/\/www.makeriot2020.com\/index.php\/2020\/10\/30\/mcp23017-with-adafruit-library\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MCP23017 with Adafruit Library&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":775,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,40,51,50,52],"tags":[],"class_list":["post-814","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-esp32","category-i2c-devices","category-io-extender-outputs","category-port-extender-inputs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":0,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media\/775"}],"wp:attachment":[{"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.makeriot2020.com\/index.php\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}