{"id":20989859,"url":"https://github.com/robtillaart/mcp23s17","last_synced_at":"2025-05-14T18:32:20.665Z","repository":{"id":41096910,"uuid":"445533665","full_name":"RobTillaart/MCP23S17","owner":"RobTillaart","description":"Arduino library for SPI based MCP23S17 16 channel port expander","archived":false,"fork":false,"pushed_at":"2024-08-02T16:04:13.000Z","size":1884,"stargazers_count":25,"open_issues_count":2,"forks_count":11,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-07T18:27:15.820Z","etag":null,"topics":["arduino","input-output","spi"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobTillaart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"RobTillaart","custom":"https://www.paypal.me/robtillaart"}},"created_at":"2022-01-07T13:39:57.000Z","updated_at":"2024-08-07T17:01:54.000Z","dependencies_parsed_at":"2023-11-13T17:26:14.857Z","dependency_job_id":"0fde3c81-b730-4f14-b650-f32a14225ca1","html_url":"https://github.com/RobTillaart/MCP23S17","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP23S17","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP23S17/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP23S17/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobTillaart%2FMCP23S17/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobTillaart","download_url":"https://codeload.github.com/RobTillaart/MCP23S17/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225305728,"owners_count":17453438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arduino","input-output","spi"],"created_at":"2024-11-19T06:26:28.560Z","updated_at":"2025-05-14T18:32:20.655Z","avatar_url":"https://github.com/RobTillaart.png","language":"C++","funding_links":["https://github.com/sponsors/RobTillaart","https://www.paypal.me/robtillaart"],"categories":[],"sub_categories":[],"readme":"\n[![Arduino CI](https://github.com/RobTillaart/MCP23S17/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)\n[![Arduino-lint](https://github.com/RobTillaart/MCP23S17/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP23S17/actions/workflows/arduino-lint.yml)\n[![JSON check](https://github.com/RobTillaart/MCP23S17/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MCP23S17/actions/workflows/jsoncheck.yml)\n[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/MCP23S17.svg)](https://github.com/RobTillaart/MCP23S17/issues)\n\n[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP23S17/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP23S17.svg?maxAge=3600)](https://github.com/RobTillaart/MCP23S17/releases)\n[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/MCP23S17.svg)](https://registry.platformio.org/libraries/robtillaart/MCP23S17)\n\n\n# MCP23S17\n\nArduino library for MCP23S17 16 channel SPI port expander.\n\n\n## Description\n\nThis library gives easy control over the 16 pins of a (SPI) MCP23S17 chip.\n\nThis IC is strongly related to the MCP23017 I2C port expander - https://github.com/RobTillaart/MCP23017_RT\nProgramming Interface is kept the same as much as possible.\n\nThe **write1(pin, value)** is optimized. \nIf a pin is not changed it will not be written again to save time.\n\n### 0.8.0 Breaking change\n\nFix #55, hardware SPI constructor.\nMakes previous versions obsolete.\n\n### 0.7.0 Breaking change\n\nFix #50, **reverse16ByteOrder(bool)** is added to reverse the byte order of the 16 bit API.\nNot using or setting the parameter to false keeps the library backwards compatible.\n\n### 0.6.0 Breaking change\n\nFix #47, bug in enable interrupt handling.\nPre 0.6.0 versions are obsolete.\n\n### 0.5.0 Breaking change\n\nVersion 0.5.0 introduced a breaking change to improve handling the SPI dependency.\nThe user has to call **SPI.begin()** or equivalent before calling **MCP.begin()**.\nOptionally the user can provide parameters to the **SPI.begin(...)**\n\n### 0.4.0 Breaking change\n\nThe version 0.4.0 has breaking changes in the interface. \nThe rationale is that the programming environment of the **Arduino ESP32 S3** \nboard uses a remapping by means of the include file **io_pin_remap.h**.\nThis file remaps the pins of several core Arduino functions. \nThe remapping is implemented by #define macros and these implement \"hard\" text \nreplacements without considering context. \nThe effect is that methods from this class (and several others) which have the same \nname as those Arduino core functions will be remapped into something not working.\n\nThe following library functions have been renamed:\n\n|  old name        |  new name    |  notes  |\n|:-----------------|:-------------|:--------|\n|  analogRead()    |  read()      |\n|  analogWrite()   |  write()     |\n|  pinMode()       |  pinMode1()  |\n|  digitalRead()   |  read1()     |\n|  digitalWrite()  |  write1()    |\n\n\n### 0.3.0 Breaking change\n\nThe version 0.3.0 has breaking changes in the interface. \nThe essence is removal of ESP32 specific code from the library. \nThis makes it possible to support the ESP32-S3 and other processors in the future. \nAlso it makes the library a bit simpler to maintain.\n\n\n### Related\n\n16 bit port expanders\n\n- https://github.com/RobTillaart/MCP23017_RT  I2C 16 IO lines.\n- https://github.com/RobTillaart/MCP23S17  SPI 16 IO lines.\n- https://github.com/RobTillaart/PCF8575  I2C 16 IO lines.\n- https://github.com/RobTillaart/PCA9671  I2C 16 IO lines. - successor PCF8575\n- https://github.com/RobTillaart/TCA9555  I2C 16 IO lines.\n\n\n8 bit port expanders\n\n- https://github.com/RobTillaart/MCP23008  I2C 8 IO lines.\n- https://github.com/RobTillaart/MCP23S08  SPI 8 IO lines.\n- https://github.com/RobTillaart/PCF8574  I2C 8 IO lines.\n- https://github.com/RobTillaart/TCA9554  I2C 8 IO lines.\n\nKeypad\n\n- https://github.com/AndreKlang/mcp23017-4x4-keypad\n\n\n## Interface\n\n```cpp\n#include \"MCP23S17.h\"\n```\n\n\n### Constructor\n\n- **MCP23S17(uint8_t select, uint8_t dataIn, uint8_t dataOut, uint8_t clock, uint8_t address = 0x00)** constructor SOFTWARE SPI.\n- **MCP23S17(int select, SPIClassRP2040\\* spi)** constructor HARDWARE SPI with explicit SPI interface selected.\n- **MCP23S17(int select, SPIClass\\* spi)** constructor HARDWARE SPI with explicit SPI interface selected.\n- **MCP23S17(int select, int address = 0x00, SPIClassRP2040\\* spi = \u0026SPI)** constructor HARDWARE SPI with optional address pins and SPI interface.\n- **MCP23S17(int select, int address = 0x00, SPIClass\\* spi = \u0026SPI)** constructor HARDWARE SPI with optional address pins and SPI interface.\n- **bool begin(bool pullup = true)** initializes library, returns true if successful. \nDefault sets the pins to INPUT PULLUP.\nReturns false if not connected or a register could not be set.\n- **bool isConnected()** returns true if connected, false otherwise. (dummy for compatibility reasons)\n- **uint8_t getAddress()** returns the address set in the constructor. \nDefault = 0, range = 0..7.\n\n\nThe two hardware constructors allow to call 4 different constructors.\n\n```cpp\n- MCP23S17(10);            // select pin only\n- MCP23S17(10, 7);         // select pin + address pins\n- MCP23S17(10, 7, \u0026SPI2);  // select pin + address pins + SPI port\n- MCP23S17(10, \u0026SPI2);     // select pin + SPI port\n```\n\n\n### Sharing SELECT lines\n\n(verified in #19)  \nTechnically two chips could use the same SELECT pin and a different address. \nSince 0.2.5 the constructors allow to setup such a configuration.\nThe added value is that one can use up to 8 devices (= 128 IO lines) with only \nfour lines (MISO, MOSI, CLOCK, SELECT).\n\nI assume that this configuration is less used and IMHO not recommended.\nNB it is more difficult to detect which device is selected when debugging.\n\nTo use the hardware addresses the Hardware Address ENable register must be set.\nSee datasheet 3.3.2 ADDRESSING SPI DEVICES, need to set IOCON.HAEN.\n\nThe library supports two ways:\n```cpp\nMCP.enableControlRegister(MCP23S17_IOCR_HAEN);  //  or 0x08\nor\nMCP.enableHardwareAddress();  //  0.2.5 version and up\n```\n\nSee also **IO Control Register** section below.\n\n\n### Single pin interface\n\n- **bool pinMode1(uint8_t pin, uint8_t mode)** pin = 0..15, mode = INPUT, OUTPUT or INPUT_PULLUP. \nDo NOT use 0, 1 for mode as the 3 constants are (possibly) defined differently.\nReturns true if successful.\n- **bool write1(uint8_t pin, uint8_t value)** pin = 0..15, value = LOW(0) HIGH (!0). Returns true if successful.\n- **uint8_t read1(uint8_t pin)** pin = 0..15, returns LOW or HIGH, might set the lastError();\n- **bool setPolarity(uint8_t pin, bool reversed)** pin = 0..15, set reversed flag. Returns true if successful.\n- **bool getPolarity(uint8_t pin, bool \u0026reversed)** pin = 0..15, reads reversed flag. Returns true if successful.\n- **bool setPullup(uint8_t pin, bool pullup)** pin = 0..15, set pull-up flag. Returns true if successful.\n- **bool getPullup(uint8_t pin, bool \u0026pullup)** pin = 0..15, reads pull-up flag. Returns true if successful.\n\n\n### 8 pins interface\n\n- **bool pinMode8(uint8_t port, uint8_t mask)** port = 0..1, mask = 0..255. Returns true if successful.\n- **bool write8(uint8_t port, uint8_t value)** port = 0..1, value = 0..255. Returns true if successful.\n- **uint8_t read8(uint8_t port)** port = 0..1, reads 8 pins into one byte.\n- **bool setPolarity8(uint8_t port, uint8_t mask)** port = 0..1, sets polarity for 8 channels at once.\nReturns true if successful.\n- **bool getPolarity8(uint8_t port, uint8_t \u0026mask)** port = 0..1, reads polarity of 8 channels at once.\nReturns true if successful.\n- **bool setPullup8(uint8_t port, uint8_t mask)** port = 0..1, sets pull-up for 8 channels at once.\nReturns true if successful.\n- **bool getPullup8(uint8_t port, uint8_t \u0026mask)** port = 0..1, reads pull-up for 8 channels at once.\nReturns true if successful.\n\n\n### 16 pins interface\n\nNote: since version 0.7.0 a function is added to reverse the byte order of the 16 bit API.\n\n- **void reverse16ByteOrder(bool reverse = false)** reverse the byte order of the 16 bit API.\nNot using this function or setting the parameter to false keeps the library backwards compatible.\n\n\n- **bool pinMode16(uint16_t mask)** mask = 0..0xFFFF, returns true if successful.\n- **bool write16(uint16_t value)** value = 0..0xFFFF, returns true if successful.\n- **uint16_t read16()** reads 16 pins into an uint16_t.\n- **bool setPolarity16(uint16_t mask)** sets polarity for 16 channels.\nReturns true if successful.\n- **bool getPolarity16(uint16_t \u0026mask)** reads polarity of 16 channels.\nReturns true if successful.\n- **bool setPullup16(uint16_t mask)** sets pull-up for 16 channels.\nReturns true if successful.\n- **bool getPullup16(uint16_t \u0026mask)** reads pull-up for 16 channels.\nReturns true if successful.\n\nThe reading and writing to registers have been performance optimized for the 16 bit interface.\nIf there are problems please open an issue.\n\n\n### Interrupts (experimental 0.5.2)\n\nRead the datasheet for the details, page 24,25.  \n\npin = 0..15  \nmode = { RISING, FALLING, CHANGE }  \n- **bool enableInterrupt(uint8_t pin, uint8_t mode)** \nReturns true if successful.\nReturns MCP23S17_PIN_ERROR if pin \u003e 15.\n- **bool disableInterrupt(uint8_t pin)**\nReturns true if successful.\nReturns MCP23S17_PIN_ERROR if pin \u003e 15.\n\n\n8 pin interface, overrides all earlier settings. \nSets all pins to the same interrupt mode { RISING, FALLING, CHANGE }.  \nport = 0, 1  \nmask = 0x00..0xFF.\n- **bool enableInterrupt8(uint8_t port, uint8_t mask, uint8_t mode)**\n- **bool disableInterrupt8(uint8_t port, uint8_t mask)** \n\n\n16 pin interface, overrides all earlier settings. \nSets all pins to the same interrupt mode { RISING, FALLING, CHANGE }.\nmask = 0x0000..0xFFFF.\n- **bool enableInterrupt16(uint16_t mask, uint8_t mode)**\n- **bool disableInterrupt16(uint16_t mask)**\n\n\nDetermine which pins caused the Interrupt. (datasheet).\n- **uint16_t getInterruptFlagRegister()** Reads all 16 pins.\n- **uint16_t getInterruptCaptureRegister()** Reads all 16 pins.\nIs used to detect if multiple pins triggered an interrupt.\n- **uint8_t getInterruptCaptureRegister8(uint8_t port)** reads 8 pins of port.\nPort == 0, 1\n\n- **bool setInterruptPolarity(uint8_t polarity)** polarity: 0 = LOW, 1 = HIGH, 2 = NONE/ODR\n- **uint8_t getInterruptPolarity()** return set value.\n\n\nMerge INTA and INTB into one signal so only one line handles all interrupts.\nThis reduces the number of interrupt lines to handle, however one has \nto read more registers to find the changed ones.\n\n- **bool mirrorInterrupts(bool on)** enables / disables mirror mode.\n- **bool isMirroredInterrupts()** returns set option (0,1 or 2).\n\n\n### SPI\n\n- **void setSPIspeed(uint32_t speed)** set hardware speed (8Mb default).\n- **uint32_t getSPIspeed()** returns set speed.\n\n\n### Debugging\n\n- **bool usesHWSPI()** returns true = hardware SPI, false = software SPI.\n- **int lastError()** idem.\n\n\n### IO Control Register\n\nThe library supports setting bit fields in the IO control register.\nRead the datasheet carefully!\n\n- **bool enableControlRegister(uint8_t mask)** set IOCR bit fields\n- **bool disableControlRegister(uint8_t mask)** clear IOCR bit fields\n\n\n|  constant              |  mask  |  description  |\n|:-----------------------|:------:|:--------------|\n|  MCP23x17_IOCR_BANK    |  0x80  |  Controls how the registers are addressed.\n|  MCP23x17_IOCR_MIRROR  |  0x40  |  INT Pins Mirror bit.\n|  MCP23x17_IOCR_SEQOP   |  0x20  |  Sequential Operation mode bit.\n|  MCP23x17_IOCR_DISSLW  |  0x10  |  Slew Rate control bit for SDA output.\n|  MCP23x17_IOCR_HAEN    |  0x08  |  Hardware Address Enable bit (MCP23S17 only).\n|  MCP23x17_IOCR_ODR     |  0x04  |  Configures the INT pin as an open-drain output.\n|  MCP23x17_IOCR_INTPOL  |  0x02  |  This bit sets the polarity of the INT output pin.\n|  MCP23x17_IOCR_NI      |  0x01  |  Not implemented. \n\n\nTwo dedicated functions are added: (MCP23S17 only)\n\n- **bool enableHardwareAddress()** set IOCR_HAEN  bit.\n- **bool disableHardwareAddress()** clear IOCR_HAEN bit.\n\n\n### Error codes\n\nIf one of the above functions return false, there might be an error.\n\n- **int lastError()** Above functions set an error flag that can be read with this function.  \nReading it will reset the flag to **MCP23S17_OK**.\n\n|  name                     |  value  |  description  |\n|:--------------------------|:-------:|:--------------|\n|  MCP23S17_OK              |  0x00   |  No error     |\n|  MCP23S17_PIN_ERROR       |  0x81   |\n|  MCP23S17_I2C_ERROR       |  0x82   |  (compatibility)\n|  MCP23S17_VALUE_ERROR     |  0x83   |\n|  MCP23S17_PORT_ERROR      |  0x84   |\n|  MCP23S17_REGISTER_ERROR  |  0xFF   |  low level.\n|  MCP23S17_INVALID_READ    |  0xFF   |  low level.\n\n\n## Future\n\n#### Must\n\n- Improve and extend documentation\n- add examples\n\n#### Should\n\n- keep functional in sync\n  - sync error codes to MCP23x17\n- buy additional hardware\n- test with multiple devices.\n  - multi SELECT lines\n- add example with interrupts\n  - test\n  - extend error codes\n- optimize code - squeeze footprint\n- fix TODO's in code\n- IOCON.HAEN, Hardware Address ENable.\n  - should this be enabled in **begin()** by default?  0.3.0\n  - check address range in constructor.\n\n#### Could\n\n- check need for writing in all functions (Polarity / Pull-up)\n  - check if bit mask changes.\n  - what is performance gain vs footprint?\n- investigate and reimplement the INPUT_PULLUP for pinMode() ?\n- initial value (16 bit?) as begin parameter (breaking change)\n  - depends on input output pull-up etc\n- AVR software SPI optimize\n  - dao and clock - see fastShiftOut.\n\n#### Wont\n\n\n## Support\n\nIf you appreciate my libraries, you can support the development and maintenance.\nImprove the quality of the libraries by providing issues and Pull Requests, or\ndonate through PayPal or GitHub sponsors.\n\nThank you,\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobtillaart%2Fmcp23s17","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobtillaart%2Fmcp23s17","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobtillaart%2Fmcp23s17/lists"}