Code Monkey home page Code Monkey logo

adafruit-mcp23017-arduino-library's Introduction

Adafruit MCP23017 Arduino Library Build StatusDocumentation

This is a library for the MCP23008/17 I2C and MCP23S08/17 SPI Port Expanders.

Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

Written by Carter Nelson for Adafruit Industries. BSD license, check license.txt for more information All text above must be included in any redistribution

To install, use the Arduino IDE Library Manager.

Pin Addressing

When using single pin operations such as pinMode(pinId, dir) or digitalRead(pinId) or digitalWrite(pinId, val) then the pins are addressed using the ID's below. For example, for set the mode of GPB0 then use pinMode(8, ...). NOTE The MCP23008 and MCP23S08 only have GPAx pins.

MCP23x08 Pin # MCP23x17 Pin # Pin Name Pin ID
10 21 GPA0 0
11 22 GPA1 1
12 23 GPA2 2
13 24 GPA3 3
14 25 GPA4 4
15 26 GPA5 5
16 27 GPA6 6
17 28 GPA7 7
-- 1 GPB0 8
-- 2 GPB1 9
-- 3 GPB2 10
-- 4 GPB3 11
-- 5 GPB4 12
-- 6 GPB5 13
-- 7 GPB6 14
-- 8 GPB7 15

Use of HW address pins for SPI device

Library supports MCP23Sxx HW pin addressing (A2, A1, A0 for S17 and A1, A0 for S08) To use it provide HW address to begin_SPI(CS, SPI, HW_ADDR) function, and as a result each SPI message will contain correct chip address.

Example: mcp.begin_SPI(10, &SPI, 0b101);

HW Address recognition must be enabled by enableAddrPins() function. NOTE Calling enableAddrPins() will enable IOCON.HAEN bit for all active (CS low) devices on SPI bus. NOTE There is hardware bug in the MCP23S17 chip, see "MCP23S17 Rev. A Silicon Errata". As a result, if using device with A2 = high, and not using addressing, hw address must be set to 0b1XX In such case, even if not using addressing, initalize your MCP23S17 chip with 0b1XX address, eg: mcp.begin_SPI(10, &SPI, 0b100);.

Warning

Some people have reported an undocumented bug that can potentially corrupt the I2C bus. It occurs if an MCP230XX input pin state changes during I2C readout. This should be very rare. For more information, see this forum post and this knowledge base article.

adafruit-mcp23017-arduino-library's People

Contributors

alexmaurer-madis avatar c0redumb avatar caternuson avatar dhenry7711 avatar donkie avatar driverblock avatar evaherrada avatar hoffmannjan avatar hydra avatar johnlon avatar jvaque avatar ladyada avatar maziu avatar myyearofcode avatar psufan avatar sconaway avatar siddacious avatar sunbox avatar tdicola avatar tyeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit-mcp23017-arduino-library's Issues

Can I use mcp23017-e/ml with this library?

I want to use mcp23017-e/ml with this library

But this library is for mcp23017 .

According to datasheet, the pin numbers are different between mcp23017-e/ml and mcp23017.

So, I'm worry about that if this library can control mcp23017-e/ml.

Can I use mcp23017-e/ml with this library?

I'm poor at English and my sentence is hard to read.Sorry.

setupInterrupts issue with 8 MCP23017

Hello,

I need some help about setupInterrups method.

My HW setup:

Adruino Leonardo
8 x MCP23017 (address : 0x20 to 0x27)

I have a class "Board" where a method BoardInit is defined.
This method is call in "setup" function of Arduino frame (in the main.cpp)

in this BoardInit method, i init the MCP23017 with the method "begin_I2C(address of each MCP)" for each MCP.
until here, all is fine and no error appear.

but when I try to initialise "setupInterrupts(false,false,HIGH)" for all of my modules, Arduino seems to be stucked on the 7th module.
if I comment this ligne, code is runned until the end and it's ok.
if I comment all the setupInterrupts ligne except the concerned, it's ok too !

not ok:
m_ExpIOModule1.setupInterrupts(false, false, HIGH);
m_ExpIOModule2.setupInterrupts(false, false, HIGH);
m_ExpIOModule3.setupInterrupts(false, false, HIGH);
m_ExpIOModule4.setupInterrupts(false, false, HIGH);
m_ExpIOModule5.setupInterrupts(false, false, HIGH);
m_ExpIOModule6.setupInterrupts(false, false, HIGH);
m_ExpIOModule7.setupInterrupts(false, false, HIGH);
m_ExpIOModule8.setupInterrupts(false, false, HIGH);

ok ! :

m_ExpIOModule1.setupInterrupts(false, false, HIGH);
m_ExpIOModule2.setupInterrupts(false, false, HIGH);
m_ExpIOModule3.setupInterrupts(false, false, HIGH); 
m_ExpIOModule4.setupInterrupts(false, false, HIGH); 
m_ExpIOModule5.setupInterrupts(false, false, HIGH);
m_ExpIOModule6.setupInterrupts(false, false, HIGH); 
//m_ExpIOModule7.setupInterrupts(false, false, HIGH);
m_ExpIOModule8.setupInterrupts(false, false, HIGH);

ok ! :
//m_ExpIOModule1.setupInterrupts(false, false, HIGH);
//m_ExpIOModule2.setupInterrupts(false, false, HIGH);
//m_ExpIOModule3.setupInterrupts(false, false, HIGH);
//m_ExpIOModule4.setupInterrupts(false, false, HIGH);
//m_ExpIOModule5.setupInterrupts(false, false, HIGH);
//m_ExpIOModule6.setupInterrupts(false, false, HIGH);
m_ExpIOModule7.setupInterrupts(false, false, HIGH);
//m_ExpIOModule8.setupInterrupts(false, false, HIGH);

any idea ?

-platformIO

thanks a lot !

Error in order of statements in custom_frequency.ino example

  • Arduino board: Custom ATMEGA2560 board
  • Arduino IDE version 1.8.12
  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): LIST REPRO STEPS BELOW

In the Custom Frequency Example, custom_frequency.ino the two statements in the setup() function are out of order:

Wire.setClock(1700000); // set frequency to 1.7mhz

...needs to come AFTER

mcp.begin(&Wire); // use default address 0

...or else the new clock frequency does not take effect.

Does not work with more than 2 MCP23017 on the same bus.

Hi everyone,
I enjoy very much your library as it makes the use of a GPIO extender very friendly.
However, I encounter an issue. I manage to make the things work with up to 2 MCP23017 (with differents adresses), but as soon as I add one or more on the same bus, the things are not working (ie : the input are not going up when needed).
Did somebody already encounter a similar issue ?
Thanks in advance !

mcp23S17 software addressing support

For using couple of mcp23S17 is needet to connect with different CS pin for each expander. Is software addressing like in I2C version will be available? Than will be needed only one CS pin and set different addresses on pins A0,A1,A2.

Adafruit_MCP23XXX::digitalWrite should read/modify/write the output register, not the input register

MCP23XXX_GPIO should be MCP23XXX_OLAT

This bug got us when using this library. The read/modify/write should be performing this operation on the output latch register, not the raw input register. This way tristate or open drain pins don't interfere with level modifications.

Example: A pin (say GPIO0) is open drain, with other external circuitry (AND function common for OK signals, etc). The GPIO is high, so "open" on the GPIO expander, but another part is holding the net low. This is all fine and expected.

Now if another pin is modified, say GPIO4 is set low, digitalWrite(4,0) this should have no effect on GPIO0. But in this case it does: This routine reads the input levels, which GPIO0 is low, then writes the register, thus driving GPIO0 low. Then the external circuitry stops holding GPIO0 low, and now the GPIO expander holds it low.

This is a common mistake when writing routines with I/O that can be tristated.

//
/*!
@brief Write a HIGH or a LOW value to a digital pin.
@param pin the Arduino pin number
@param value HIGH or LOW
*/
/
/
void Adafruit_MCP23XXX::digitalWrite(uint8_t pin, uint8_t value) {
Adafruit_BusIO_Register GPIO(i2c_dev, spi_dev, MCP23XXX_SPIREG,
getRegister(MCP23XXX_GPIO, MCP_PORT(pin)));
Adafruit_BusIO_RegisterBits pin_bit(&GPIO, 1, pin % 8);

pin_bit.write((value == LOW) ? 0 : 1);
}

Won't Compile

  • Arduino board: Uno/ProMini/

  • Arduino IDE version (found in Arduino -> About Arduino menu): **1.8,13/1.8.15

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): *None of the examples in 2.0.2 will compile Previous versions compile correctly

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

Install 2.0.2 using library manager
select any example
compile

In file included from C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23X17.h:8:0,

             from C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23X17.cpp:5:

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23X17.cpp: In member function 'uint16_t Adafruit_MCP23X17::readGPIOAB()':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23X17.cpp: In member function 'void Adafruit_MCP23X17::writeGPIOAB(uint16_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In file included from C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp:23:0:

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::pinMode(uint8_t, uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'uint8_t Adafruit_MCP23XXX::digitalRead(uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::digitalWrite(uint8_t, uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'uint8_t Adafruit_MCP23XXX::readGPIO(uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::writeGPIO(uint8_t, uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::setupInterrupts(bool, bool, uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::setupInterruptPin(uint8_t, uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'void Adafruit_MCP23XXX::disableInterruptPin(uint8_t)':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.cpp: In member function 'uint8_t Adafruit_MCP23XXX::getLastInterruptPin()':

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: error: 'ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE' was not declared in this scope

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^

C:\MyPathDocuments\Arduino\libraries\Adafruit_MCP23017_Arduino_Library\src\Adafruit_MCP23XXX.h:29:3: note: in definition of macro 'MCP23XXX_SPIREG'

ADDRESSED_OPCODE_BIT0_LOW_TO_WRITE //!< SPI register type

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1

Error compiling for board Arduino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

MCP23017: please provide interrupt examples also for Adafruit M0, M4, Arduino Due (M3), ESP8266 and ESP32

re: https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library/tree/master/examples/interrupt

please provide interrupt examples also for Adafruit M0, M4, and Arduino Due (M3) (and possibly also for ESP8266 and ESP32, I just ordered a Feather ESP32, too)!

I tried with my Adafruit Feather M4, but it does not compile for that board!

exit status 1
'EIFR' was not declared in this scope

For ARM and ESP boards it should be as simple and easy to use as the AVR example.

digitalwrite missing endTransmission

Hi,

I guess at the end of "void Adafruit_MCP23017::digitalWrite(uint8_t pin, uint8_t d) {"

Wire.endTransmission(); needs te be added. It solved completely my i2c lockup's on the due.

Thank you

Please recognize INPUT_PULLUP for pinmode

updateRegisterBit(p,(d==INPUT),MCP23017_IODIRA,MCP23017_IODIRB);
-->

Change line to

	updateRegisterBit(p,(d==INPUT || d==INPUT_PULLUP),MCP23017_IODIRA,MCP23017_IODIRB);

INPUT_PULLUP is valid mode for the standard arduino GPIOs, see https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/ .

The MCP23017 automatically activates its internal pull-UP, so nothing more has to be done.

I prefer to use INPUT_PULLUP as pinmode, because it correspond to the actual behaviour, so it is good practice to document this in the code.

Provide access to INTCAP

There's already an existing (but stalled) PR that address this issue here:
#84
See that thread for more background information and discussion.

Opening this so there is a dedicated issue for tracking,

Problem with MCP23017 library

Adafruit MCP23017 library includes

void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { updateRegisterBit(p, (d == INPUT), MCP23017_IODIRA, MCP23017_IODIRB); }

Unfortunately INPUT is in Arduino.h 0, which works correct for lot's of uC's,
but not for ESP32, which comes with its own header file.
There, INPUT is defined as 1, so MCP23017 is not correctly initialized.
The Adafruit header file for MCP23017 should include

#define MCP23017_INPUT 0

so that pinMode can be written as

void Adafruit_MCP23017::pinMode(uint8_t p, uint8_t d) { updateRegisterBit(p, (d == MCP23017_INPUT), MCP23017_IODIRA, MCP23017_IODIRB); }

Missing migration guide/instructions to the major 2.x release

I am using the 1.3.0 version of the lib. With the new 2.x major release you changed the names of the src files, the function calls etc. without even mention it. Result: i have to recode just by compiler warnings or dive deep in the lib.
A little readme update would be very nice on such major changes. THX

Inconsistent addressing

I'm curious why i2cScanner shows two MCP23017 ICs (one with addresses jumpered all high, the other all low) as 0x20 and 0x27, but I have to use 0 and 7 in the "begin()" statements for it to work. Using 0x20 and 0x27 compiled and sort of worked, but I ended up getting readings from the first chip for both addresses. That seems inconsistent, as the other Adafruit libraries I've used seem to use the full i2c address.

Also, actually saying "begin(0)" results in an error; apparently since it's the default it's not allowed to be used?

Interrupt example is bad

I have spend the day with the interrupt example and now i finally know whats the problem is.

In the example the commando is used:
mcp.setupInterruptPin(pin,FALLEN);

This is really bad. The correct mode in this example is "CHANGE".
FALLING or RISING modes will not work with this code without instabilities.

What i see, is that with CHANGE a interrupt goes active with a change of the pin and the interrupt can be reset with readGPIOAB();
With FALLING it is different, The interrupt goes aktive when the pin goes LOW AND the interrupt STAYS active as long as the pin is low.
While the pin is low the interrupt signal can't be reset !!!.

This solved in the code by looping until the pin (or button) is released.

This has as consequent that the code will hang ( no loop () ) until the user releases the button.

It would be nice that the examples have a standard that they always loop().

ESP8266 support?

Hello there. I did not find any information about the problem in your Readme, so can you please state if the library is compatible with ESP8266?

private methods should be protected.

Hello,
it seems that all the methods declared "private" in the Adafruit_MCP23017 should be declared "protected" instead. This way we can easily write derived classes that uses those methods. There are more object oriented ways to secure the base class, but changing the declaration is sufficient for this simple class. Thanks!!

MCP23017 v2.X Not Available?

I'm trying to grab the latest MCP23017 version (2.0.1) via the Arduino IDE (1.8.15 for Mac), and I only see up to 1.3. Is 2.0.1 not available yet? It looks like it should be.

Can this lib be used on ATTiny85 ?

I'd like to use this lib on ATtiny85, but while compiling I've got many errors which seems related to Wire.h lib (see below and here
Arduino: 1.8.10 (Windows 10), Board: "ATtiny25/45/85, ATtiny45, Internal 1 MHz" C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c: In function 'twi_init': C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c:76:16: error: 'SDA' undeclared (first use in this function) digitalWrite(SDA, 1);^~~C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c:76:16: note: each undeclared identifier is reported only once for each function it appears in C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c:77:16: error: 'SCL' undeclared (first use in this function); did you mean 'SPL'? digitalWrite(SCL, 1);^~~SPL In file included from c:\users\seb\appdata\local\arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\avr\io.h:99:0, from C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c:25:C:\Users\seb\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.2\libraries\Wire\src\utility\twi.c:80:7: error: 'TWSR' undeclared (first use in this function); did you mean 'ACSR'? cbi(TWSR, TWPS0);

I tried to replace Wire lib with TinyWire with no more success.

Any help appreciated !
Thx

How to use different device address for MCP23017 so I can use multiple chips?

I want to use more than 3 MCP23017-E/SP DIP28 chips so I can use many digital pins. I tried this solution but only device address with 0x00 is working. The question is what device address should I use for additional chip?

#include "Adafruit_MCP23017.h" 
Adafruit_MCP23017 mcp1; 
#define addr1 0 // tested 1,2,3,4,5, 0x01, 0x02, 0x20, 0x21 but only 0 and 0x00 is working.
void setup() 
  {
    mcp1.begin(addr1); 
    mcp1.pinMode(0, INPUT); //pin 21 on chip
    Serial.begin(9600); 
  } 

  void loop()
  { 
      if(mcp1.digitalRead(0)== HIGH )
      Serial.println("HIGH"); 
  }

How to define the Hardware Adress with I²C?

Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:

  • Do not use GitHub issues for troubleshooting projects and issues. Instead use
    the forums at http://forums.adafruit.com to ask questions and troubleshoot why
    something isn't working as expected. In many cases the problem is a common issue
    that you will more quickly receive help from the forum community. GitHub issues
    are meant for known defects in the code. If you don't know if there is a defect
    in the code then start with troubleshooting on the forum first.

  • If following a tutorial or guide be sure you didn't miss a step. Carefully
    check all of the steps and commands to run have been followed. Consult the
    forum if you're unsure or have questions about steps in a guide/tutorial.

  • For Arduino projects check these very common issues to ensure they don't apply:

    • For uploading sketches or communicating with the board make sure you're using
      a USB data cable and not a USB charge-only cable. It is sometimes
      very hard to tell the difference between a data and charge cable! Try using the
      cable with other devices or swapping to another cable to confirm it is not
      the problem.

    • Be sure you are supplying adequate power to the board. Check the specs of
      your board and plug in an external power supply. In many cases just
      plugging a board into your computer is not enough to power it and other
      peripherals.

    • Double check all soldering joints and connections. Flakey connections
      cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.

    • Ensure you are using an official Arduino or Adafruit board. We can't
      guarantee a clone board will have the same functionality and work as expected
      with this code and don't support them.

If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:

  • Arduino board: INSERT ARDUINO BOARD NAME/TYPE HERE

  • Arduino IDE version (found in Arduino -> About Arduino menu): INSERT ARDUINO
    VERSION HERE

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): LIST REPRO STEPS BELOW

Can this Library be used with stm32

Hi,

platformio shows this library is compatible with the stm32 (STM32F103C8).
It does compile without any errors.
However, my chip doesn't output anything.
I've checked the wiring and tried 3 sets of hardware.
Do I have to change pin definitions for this library to work?
Thanks!

begin as input instead of output

on code i see


	writeRegister(MCP23017_IODIRA,0xff);
	writeRegister(MCP23017_IODIRB,0xff);

it's possible to have a config to enable starting mode as output? like

begin(i2c_addr,STARTING_MODE (input/output))

chaghe the 2 lines with 0x00 should make the trick right?
do you think it could be an useful feature to have?

Interruptions - Method getLastInterruptPin() always returns 0 even if port 0 is not set for interruptions or set up as input

You might want to review the getLastInterruptPin() method. I tried a simple test using port B as input and port A as output modifying your interruptions sample code. Pin 0 (Port A) is set as output and I have an LED connected to it for debugging, and pin 8 (Port B) is set up as input with a button wired to it.

The interruptions correctly trigger when the state of pin 8 changes, however when polling the register for the interruption ports and values, it will always return pin 0 as the one that trigger the interruption. I don't see anything on the method that validates if the pin is set as an interruption pin or event as an input, but I don see the first thing the code checks if for the value of PortA but by bit, which explains why the method always stops at pin 0. I am not sure why the register if the MCP23017 is setting pin 0 as an interruption either since there are at least two places in the sample code that clear the INT register. If I comment out the block that checks for port A, the method correctly returns port 8 as the port that triggered the interruption.

Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:

  • Do not use GitHub issues for troubleshooting projects and issues. Instead use
    the forums at http://forums.adafruit.com to ask questions and troubleshoot why
    something isn't working as expected. In many cases the problem is a common issue
    that you will more quickly receive help from the forum community. GitHub issues
    are meant for known defects in the code. If you don't know if there is a defect
    in the code then start with troubleshooting on the forum first.

  • If following a tutorial or guide be sure you didn't miss a step. Carefully
    check all of the steps and commands to run have been followed. Consult the
    forum if you're unsure or have questions about steps in a guide/tutorial.

  • For Arduino projects check these very common issues to ensure they don't apply:

    • For uploading sketches or communicating with the board make sure you're using
      a USB data cable and not a USB charge-only cable. It is sometimes
      very hard to tell the difference between a data and charge cable! Try using the
      cable with other devices or swapping to another cable to confirm it is not
      the problem.

    • Be sure you are supplying adequate power to the board. Check the specs of
      your board and plug in an external power supply. In many cases just
      plugging a board into your computer is not enough to power it and other
      peripherals.

    • Double check all soldering joints and connections. Flakey connections
      cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.

    • Ensure you are using an official Arduino or Adafruit board. We can't
      guarantee a clone board will have the same functionality and work as expected
      with this code and don't support them.

If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:

  • Arduino board: INSERT ARDUINO BOARD NAME/TYPE HERE

  • Arduino IDE version (found in Arduino -> About Arduino menu): INSERT ARDUINO
    VERSION HERE

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): LIST REPRO STEPS BELOW

get error status

Hello,

it's possible to retrive the current status of OUTPUT pins?
ie: if i do writeGPIOAB(15) can i read pin n*0 and get his value? (or even read back all the status of AB)

if i do readGPIOAB, that's read the actual value on the pin, as INPUT correct? not the prev value set with writeGPIOAB?

MCP23017 not working with Raspberry Pi Pico

First of all: Thanks for a great library that I have used with success on multiple occasions with Arduino Nano.
I'm trying to get this library to work on the Pico without success. I know the MCP23017 works because I have tested it with an Arduino Nano.

  • Arduino board: Raspberry Pi Pico

  • Arduino IDE version: Arduino IDE 1.8.18, Platformio 5.2.4, 3.4.0

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):

MCP23017 is connected via GP0 (SDA) and GP1 (SCL)

When I run the sketch below mcp.begin_I2C() always returns 0 and the sketch doesn't progress further. I suspect it has something to do with the Pico having 2 I2C busses?

#include <Arduino.h>
#include <Adafruit_MCP23X17.h>

Adafruit_MCP23X17 mcp;
int mcpLedPin = 0;

void setup()
{
  delay(2000);
  Serial.begin(9600);

  if (!mcp.begin_I2C())
  {
    Serial.println("Error.");
    while (1)
      ;
  }
  mcp.pinMode(mcpLedPin, OUTPUT);
}

void loop()
{
  mcp.digitalWrite(mcpLedPin, HIGH);
  delay(1000);
  mcp.digitalWrite(mcpLedPin, LOW);
  delay(1000);
}

Unable to use MCP23017 with Adafruit MCP23017 LCD Hat

My code is as follows:
`// Connect pin #12 of the expander to Analog 5 (i2c clock)
// Connect pin #13 of the expander to Analog 4 (i2c data)
// Connect pins #15, 16 and 17 of the expander to 3V3 (address selection)
// Connect pin #9 of the expander to 5V (power)
// Connect pin #10 of the expander to ground (common ground)
// Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

// Input #0 is on pin 21 so connect a button or switch from there to ground

// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
//#include "Adafruit_MCP23017.h"

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Adafruit_MCP23017 mcp;

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
#define addr1 7 // 7 = A1 high, A2 high, A3 high

void setup() {
// Debugging output
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
mcp.begin(addr1);
mcp.pinMode(0, INPUT);
mcp.pullUp(0, HIGH); // turn on a 100K pullup internally

pinMode(13, OUTPUT); // use the p13 LED as debugging

// Print a message to the LCD. We track how long it takes since
// this library has been optimized a bit and we're proud of it :)
int time = millis();
lcd.print("Hello, world!");
time = millis() - time;
Serial.print("Took "); Serial.print(time); Serial.println(" ms");
lcd.setBacklight(RED);
}

uint8_t i=0;
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);

uint8_t buttons = lcd.readButtons();

if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_UP) {
lcd.print("UP ");
}
if (buttons & BUTTON_DOWN) {
lcd.print("DOWN ");
}
if (buttons & BUTTON_LEFT) {
lcd.print("LEFT ");
}
if (buttons & BUTTON_RIGHT) {
lcd.print("RIGHT ");
}
if (buttons & BUTTON_SELECT) {
lcd.print("SELECT ");
}
}
digitalWrite(13, mcp.digitalRead(0));
}`

for some reson the hat is working but the other MCP23017 chip

Interrupt on CHANGE

Hello there,

I am getting pin number as 255 (value also 255) when a pin is released.

pin: 7 val: 0 (when connected to GND)
pin: 255 val: 255 (when same pin disconnected from GND)

What am I doing wrong?

Tried changing the code to detect on "CHANGE" and OpenDrain configuration where it stopped working altogether.Any ideas what's going on?

Thanks in-advance,
PK.

Version 2.0.2 does not compile with Teensy 4.1

  • Arduino board: Teensy 4.1

  • Arduino IDE version: Platformio 5.2.4, 3.4.0

  • List the steps to reproduce the problem below:

Create a new project and add version 2.0.2.
Build the project and get the following output:

> Executing task in folder Teensy 4.1 MCP Error Project: platformio run --environment teensy41 <

Processing teensy41 (platform: teensy; board: teensy41; framework: arduino)
------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy41.html
PLATFORM: Teensy (4.14.0) > Teensy 4.1
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 7.75MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy 1.155.0 (1.55) 
 - tool-teensy 1.155.0 (1.55) 
 - toolchain-gccarmnoneeabi 1.50401.190816 (5.4.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 94 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Adafruit MCP23017 Arduino Library> 2.0.2
Building in release mode
Compiling .pio/build/teensy41/src/main.cpp.o
Compiling .pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23X08.cpp.o
Compiling .pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23X17.cpp.o
Compiling .pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23XXX.cpp.o
Compiling .pio/build/teensy41/FrameworkArduino/AudioStream.cpp.o
Compiling .pio/build/teensy41/FrameworkArduino/Blink.cc.o
Compiling .pio/build/teensy41/FrameworkArduino/CrashReport.cpp.o
Compiling .pio/build/teensy41/FrameworkArduino/DMAChannel.cpp.o
In file included from .pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23XXX.cpp:23:0:
.pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23XXX.h:8:37: fatal error: Adafruit_BusIO_Register.h: No such file or directory

*********************************************************************************
* Looking for Adafruit_BusIO_Register.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:Adafruit_BusIO_Register.h"
* Web  > https://platformio.org/lib/search?query=header:Adafruit_BusIO_Register.h
*
*********************************************************************************

In file included from .pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23X17.h:8:0,
                 from .pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23X17.cpp:5:
.pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23XXX.h:8:37: fatal error: Adafruit_BusIO_Register.h: No such file or directory

*********************************************************************************
* Looking for Adafruit_BusIO_Register.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:Adafruit_BusIO_Register.h"
* Web  > https://platformio.org/lib/search?query=header:Adafruit_BusIO_Register.h
*
*********************************************************************************

compilation terminated.
compilation terminated.
In file included from .pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23X08.h:8:0,
                 from .pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23X08.cpp:5:
.pio/libdeps/teensy41/Adafruit MCP23017 Arduino Library/src/Adafruit_MCP23XXX.h:8:37: fatal error: Adafruit_BusIO_Register.h: No such file or directory

*********************************************************************************
* Looking for Adafruit_BusIO_Register.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:Adafruit_BusIO_Register.h"
* Web  > https://platformio.org/lib/search?query=header:Adafruit_BusIO_Register.h
*
*********************************************************************************

compilation terminated.
*** [.pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23X17.cpp.o] Error 1
*** [.pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23XXX.cpp.o] Error 1
*** [.pio/build/teensy41/libb13/Adafruit MCP23017 Arduino Library/Adafruit_MCP23X08.cpp.o] Error 1
================================================================ [FAILED] Took 1.55 seconds ================================================================
The terminal process "platformio 'run', '--environment', 'teensy41'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

Solution that works:

  • Remove version 2.0.2 and install 1.2 instead.
  • Then make sure to include Wire.h in the project

Modify library

Hello, first of all thank you for this library! Do you think it is possible to modify the library to integrate the output of LED mcp23017 GPB0 as the activity status? I tried several things but it did not work :( Thank you.

MCP23018 compatibility

I've tried out the library with the MCP23018 , and it (not surprisingly) seems to work just fine. I don't know if it's necessary to include in the readme, but now it at least is written somewhere. :-)

Increased program memory occupancy

Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:

  • Do not use GitHub issues for troubleshooting projects and issues. Instead use
    the forums at http://forums.adafruit.com to ask questions and troubleshoot why
    something isn't working as expected. In many cases the problem is a common issue
    that you will more quickly receive help from the forum community. GitHub issues
    are meant for known defects in the code. If you don't know if there is a defect
    in the code then start with troubleshooting on the forum first.

  • If following a tutorial or guide be sure you didn't miss a step. Carefully
    check all of the steps and commands to run have been followed. Consult the
    forum if you're unsure or have questions about steps in a guide/tutorial.

  • For Arduino projects check these very common issues to ensure they don't apply:

    • For uploading sketches or communicating with the board make sure you're using
      a USB data cable and not a USB charge-only cable. It is sometimes
      very hard to tell the difference between a data and charge cable! Try using the
      cable with other devices or swapping to another cable to confirm it is not
      the problem.

    • Be sure you are supplying adequate power to the board. Check the specs of
      your board and plug in an external power supply. In many cases just
      plugging a board into your computer is not enough to power it and other
      peripherals.

    • Double check all soldering joints and connections. Flakey connections
      cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.

    • Ensure you are using an official Arduino or Adafruit board. We can't
      guarantee a clone board will have the same functionality and work as expected
      with this code and don't support them.

If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:

  • Arduino board: ARDUINO UNO

  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.19

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): **In my project I have upgraded the library from 1.3.0 to 2.0.2 and changed the program to upgrade as required example mcp.begin to mcp.begin_I2C and so on ... and compiled.
    I noticed the program memory increase of over 9%, it seems to me a very high value.
    Is there any optimization planned in the future?
    Thanks
    Regards
    Massimo
    **

Fork for interrupts

Hi,

I'm planning to fork in order to implement interrupt control.

Two questions:

  1. would you be interested in a pull request once it's working?
  2. If not, would you prefer I remove the Adafruit prefix from the class?

Cheers,
Chuck

Allow for disabling of Interrupts

There is only a function to enable interrupts per pin but none to disable interrupts.

Please offer functions to disable interrupt of a single pin or all pins altogether.

MCP 23017 e/sp

Thank you for opening an issue on an Adafruit Arduino library repository. To
improve the speed of resolution please review the following guidelines and
common troubleshooting steps below before creating the issue:

  • Do not use GitHub issues for troubleshooting projects and issues. Instead use
    the forums at http://forums.adafruit.com to ask questions and troubleshoot why
    something isn't working as expected. In many cases the problem is a common issue
    that you will more quickly receive help from the forum community. GitHub issues
    are meant for known defects in the code. If you don't know if there is a defect
    in the code then start with troubleshooting on the forum first.

  • If following a tutorial or guide be sure you didn't miss a step. Carefully
    check all of the steps and commands to run have been followed. Consult the
    forum if you're unsure or have questions about steps in a guide/tutorial.

  • For Arduino projects check these very common issues to ensure they don't apply:

    • For uploading sketches or communicating with the board make sure you're using
      a USB data cable and not a USB charge-only cable. It is sometimes
      very hard to tell the difference between a data and charge cable! Try using the
      cable with other devices or swapping to another cable to confirm it is not
      the problem.

    • Be sure you are supplying adequate power to the board. Check the specs of
      your board and plug in an external power supply. In many cases just
      plugging a board into your computer is not enough to power it and other
      peripherals.

    • Double check all soldering joints and connections. Flakey connections
      cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.

    • Ensure you are using an official Arduino or Adafruit board. We can't
      guarantee a clone board will have the same functionality and work as expected
      with this code and don't support them.

If you're sure this issue is a defect in the code and checked the steps above
please fill in the following fields to provide enough troubleshooting information.
You may delete the guideline and text above to just leave the following details:

  • Arduino board: INSERT ARDUINO BOARD NAME/TYPE HERE

  • Arduino IDE version (found in Arduino -> About Arduino menu): INSERT ARDUINO
    VERSION HERE

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too): LIST REPRO STEPS BELOW

Communicating with chips with I2C addresses does not work

  • Arduino board: QTPY

  • Arduino IDE version: 1.8.16

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):

1x QTPY microcontroller, providing 3.3V and GND
1x mcp23017 at I2C address 0x00 (pins A0, A1, A2 connected to GND)
mcp 23017 ~reset line pulled to 3.3V via 100kΩ
0.1 uF and 10uF caps on 3.3V
2.2kΩ pull-ups to 3.3V on SCK and SCL pins from QTPY
One mcp23017 IO pin pulled high to make sure I'm really reading it

When I run the following sketch, this device works as anticipated:


// Create IO expander instance
Adafruit_MCP23X17 mcp0;

void setup() {
    mcp0.begin_I2C();
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO()));
    delay(1000);
}

I get output like this:

Reading mcp0 port: 32

Which corresponds to the one wire I have pulling a pin high. However, this is not suitable for me as I need multiple mcp23017s on the I2C bus for my project. If I try to provide an I2C address in mcp0.begin_I2C(), I only get bogus data:

#include <Adafruit_MCP23X17.h>

// Create IO expander instance
Adafruit_MCP23X17 mcp0;

void setup() {
    mcp0.begin_I2C(0);
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO()));
    delay(1000);
}

Reading mcp0 port: 255

I have tried 0x00, (uint8_t)0, and 0, but they all show the same broken behavior.

After reverting the library to version 1.3 and using the following code, it works as expected:

#include <Adafruit_MCP23017.h>

// Create IO expander instance
Adafruit_MCP23017 mcp0;

void setup() {
    mcp0.begin((uint8_t)0);
    Serial.begin(9600);
}

void loop() {
    Serial.println("Reading mcp0 port: " + String(mcp0.readGPIO(0)));
    delay(1000);
}

MCP23018 support

Hi, good job on the rewrite, looks very good. Is there any plans on supporting the MCP23018 variant? It's pretty much the same as the MCP23017, with minor differences. I could use this library almost out of the box, the only issue I found so far is that this library does not support enabling the internal pull-up resistor for the OUTPUT mode, it only enables when choosing INPUT_PULLUP (which makes sense since MCP23017 only supports input pull-up.

The OUTPUT_OPEN_DRAIN mode should probably be accounted for as well then.

Please include an example for using multiple mcp chips

This is specific to the mcp23017 chip

#include <Adafruit_MCP23X17.h>


Adafruit_MCP23X17 mcp1; //declaration for chip 1
Adafruit_MCP23X17 mcp2; //declaration for chip 2

/*
 use the following table to configure A0-2 on your mcp chip to an I2c address
 
address  A2  A1  A0    i2c address
000     low low low     0x20         <-- we will be using this for chip 1
001     low low high    0x21         <-- and this one for chip 2
010     low high low    0x22             every additional address can be used for up to 8 chips total
011     low high high   0x23
100     high low low    0x24
101     high low high   0x25
110     high high low   0x26
111     high high high  0x27
*/
void setup() {
  Serial.begin(9600);
    mcp1.begin_I2C(0x20);   //call for a connection to chips using addresses above 
    mcp2.begin_I2C(0x21); 

  

  mcp1.pinMode(0, INPUT_PULLUP);  // table for the pin translation of mcp23 chips can be found on the main page of the library
  mcp1.pinMode(8, OUTPUT);
  mcp1.pinMode(9, OUTPUT);
  mcp2.pinMode(9, OUTPUT);
}


void loop() {
    
  Serial.print("pin0 = "); Serial.println(mcp1.digitalRead(0 ));

 if (mcp1.digitalRead(0) == 0){  // check if pin 0 is grounded, if so light up the led on pin 8 
  mcp1.digitalWrite(8, HIGH);
  }
  else{
  mcp1.digitalWrite(8, LOW);
 }
 
  mcp1.digitalWrite(9, HIGH);  // make chip 1 pin 9 led blink on and then off
  delay(500);
  mcp1.digitalWrite(9, LOW);
  delay(500);

  mcp2.digitalWrite(9, HIGH); // make chip 2 pin 9 led blink on and then off 
  delay(500);
  mcp2.digitalWrite(9, LOW);
  delay(500);

}

Not an issue but suggestion for new library

I used two MCP23017 ICs on a project with one for a 36 button keypad and a 32x122 graphic display. The chips worked fine but I wanted to improve the display updates by replacing one MCP23017 with a MCP23S17. I did not want to change my code too much so I rewrote the MCP23017 library for the MCP23S17. The library worked well as a drop in replacement. I would not mind if Adafruit picked up this library or create a new library to handle either IC. I searched the forum to see if I could find a way to submit suggestions for new libraries but did not come across how to submit library suggestions.

I would be willing to create a new library that allows the use of either MCP23017 or MCP23S17 but I would like to know if there is interest in Adafruit picking up the library.

My code is located at https://github.com/richteel/TeelSys-MCP23S17-Arduino-Library. If you take a look at the code, you can see there are only minor changes. It would be possible to combine into one library named MCP23X17.

Please let me know if there is any interest and the best way to help. I'm not looking for anything in return. I'm just hoping that I can help someone else out who starts out with one chip and would like to move to the other one.

i2c pins

Hi,

i suggest to add a constructors parameter for pins for i2c.

it's useful on device like esp8266 where sometimes people need to map pin on different task

void begin(uint8_t addr);
should be
void begin(uint8_t addr, uint8_t sda = 0, uint8_t sclk = 0);

and on begin function
if (sda != sckl)
Wire.begin(sda, sckl);
else
Wire.begin();

of course just a random implementation, but that keep compatibility with the rest of library

Expander stops working after a while

  • Arduino board: UNO / Nano / Teensy 3.5
  • Arduino IDE version: 1.8.5

I'm using following sketch with 2 LEDs on 6 and 7:

#include <Wire.h>
#include "Adafruit_MCP23017.h"

Adafruit_MCP23017 mcp;
  
void setup() {  
  mcp.begin();      // use default address 0
  mcp.pinMode(6, OUTPUT);
  mcp.pinMode(7, OUTPUT);
}

void loop() {
  delay(500);
  mcp.digitalWrite(6, HIGH);
  mcp.digitalWrite(7, LOW);
  delay(500);
  mcp.digitalWrite(6, LOW);
  mcp.digitalWrite(7, HIGH); 
}

After a short while (2s up to 2min) the LEDs stops blinking. I tried it on different boards, also Teensy 3.5 but can't figure out what the problem is.

Interrupts with an ESP32

Has anyone tried interrupts with an ESP32? I can read\write to all pins fine but no luck with interrupts:

Sketch:

#include <Wire.h>
#include <Adafruit_MCP23017.h>

const byte interruptPin = 25;
volatile int interruptCounter = 0;
int numberOfInterrupts = 0;

portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;

Adafruit_MCP23017 mcp;

void IRAM_ATTR handleInterrupt() {
  portENTER_CRITICAL_ISR(&mux);
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&mux);
}

void setup() {

  Serial.begin(115200);
  Serial.println("Monitoring interrupts: ");
  pinMode(interruptPin, INPUT_PULLUP);

  mcp.begin();
  mcp.setupInterrupts(true, false, LOW);
  mcp.pinMode(0, INPUT);
  mcp.pullUp(0, HIGH);  // turn on a 100K pullup internally
  mcp.setupInterruptPin(0, FALLING);

  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
}

void loop() {

  if (interruptCounter > 0) {

    portENTER_CRITICAL(&mux);
    interruptCounter--;
    portEXIT_CRITICAL(&mux);
    numberOfInterrupts++;
    Serial.println("An interrupt has occurred. ");
    Serial.println(mcp.digitalRead(mcp.getLastInterruptPin()));

  }
}

Bulk set gpio direction support

Hello would it be possible to add a function for bulk setting of INPUT/OUTPUT of the GPIO? im currently using 5 of these and 80 lines of code is used up for just setting the direction of the GPIO, i mean its not really a bad thing, just to make the code cleaner to look at. Alternatively having a function to read/write register directly would also be good as i can do the bulk setting of gpio direction there.

Thanks ! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.