# Using MCP23S17 Port Expander

**URL:** <https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682>\
**Category:** LEDs and Multiplexing\
**Created:** [July 31, 2023, 9:05pm UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682 "2023-07-31T21:05:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![8art-5imson](https://avatars.discourse-cdn.com/v4/letter/8/a6a055/32.png) [@8art-5imson](https://forum.arduino.cc/u/8art-5imson)\
**Post date:** [July 31, 2023, 9:05pm UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/1 "2023-07-31T21:05:00Z")

</div>

I am trying to use a MCP23S17 (SPI) Port expander on a ESP32 Project (Squeezelite Player) but have never got it to work. I thought it was maybe a bad chip, so this evening i connected it to a arduino UNO and loaded the example:- MCP23S17\_test.ino from the library and it works, so the chip is ok!  
The address of the chip is set to 0 (pin 16,17,18 to ground) but in line 13 of the code it seems to be set to 10 but the comment reads 0 ??? it works and if i change it to 0 it dosn't. can anyone please explain this as in the actual application i need to tell it this address is it 10 or 0 ?  
thanks Chris

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [July 31, 2023, 10:30pm UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/3 "2023-07-31T22:30:11Z")

</div>

> [@8art-5imson](#):
>
> MCP23S17 ( **SPI** ) Port expander ... The address of the chip is set to 0

Eh? SPI doesn't have addresses!

_EDIT: Ah, SPI doesn't; but the chip uses them in its Control Byte. But this can be disabled with IOCON.HAEN_

![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/0/9/d0904873cd7a4fc17c8993f9ef922b8cde05ac84.png)  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/8/5/c/85c0c618b98920b534265b80758cf93fe223ce94.png)

> [@8art-5imson](#):
>
> in line 13 of the code

What code? Please post the complete code!

---

<div class="post-metadata">

**Author:** ![8art-5imson](https://avatars.discourse-cdn.com/v4/letter/8/a6a055/32.png) [@8art-5imson](https://forum.arduino.cc/u/8art-5imson)\
**Post date:** [August 1, 2023, 6:47am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/4 "2023-08-01T06:47:40Z")

</div>

```arduino
//
// FILE: MCP23S17_digitalRead.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-12-30
// PUPROSE: test MCP23017 library

#include "MCP23S17.h"
#include "SPI.h"

MCP23S17 MCP(10); // HW SPI address 0x00
int rv = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.print("MCP23S17_test version: ");
  Serial.println(MCP23S17_LIB_VERSION);
  delay(100);

  SPI.begin();
  rv = MCP.begin();
  Serial.println(rv ? "true" : "false");

  rv = MCP.pinMode8(0, 0xFF); // CHECK
  Serial.println(rv);
  rv = MCP.pinMode8(1, 0xFF);
  Serial.println(rv);

  Serial.print("HWSPI: ");
  Serial.println(MCP.usesHWSPI());

  Serial.println("TEST digitalRead(pin)");
  for (int pin = 0; pin < 16; pin++)
  {
    int val = MCP.digitalRead(pin);
    Serial.print(val);
    Serial.print(' ');
    delay(100);
  }
  Serial.println();
}

void loop()
{
  delay(1000);
  Serial.println("TEST digitalRead(pin)");
  for (int pin = 0; pin < 16; pin++)
  {
    int val = MCP.digitalRead(pin);
    Serial.print(val);
    Serial.print(' ');
    delay(10);
  }
  Serial.println();

}

// -- END OF FILE --
[quote="awneil, post:3, topic:1153682, full:true"]
[quote="8art-5imson, post:1, topic:1153682"]
MCP23S17 ( **SPI** ) Port expander ... The address of the chip is set to 0
[/quote]
Eh? SPI doesn't have addresses!

*EDIT: Ah, SPI doesn't; but the chip uses them in its Control Byte. But this can be disabled with IOCON.HAEN*

![image|340x386](upload://tL2rcIld2wMTFUmoDdO1cz7AT52.png) 
![image|307x192](upload://j5ewdcqkvanFmMmnfEE7pg4hcJ6.png)

[quote="8art-5imson, post:1, topic:1153682"]
in line 13 of the code
[/quote]
What code? Please post the complete code!
[/quote]

```

---

<div class="post-metadata">

**Author:** ![8art-5imson](https://avatars.discourse-cdn.com/v4/letter/8/a6a055/32.png) [@8art-5imson](https://forum.arduino.cc/u/8art-5imson)\
**Post date:** [August 1, 2023, 6:55am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/5 "2023-08-01T06:55:11Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/e/4/7/e4771f58e86f940209e5145ffc8b31afa3b87c5f.jpeg)

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [August 1, 2023, 6:56am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/6 "2023-08-01T06:56:08Z")

</div>

> [@8art-5imson](#):
>
> but in line 13 of the code it seems to be set to 10 but the comment reads 0 ???

I believe that the 10 refers to the pin used for the SPI chip select signal for the MCP23S17.

You need to change this for your ESP32.

---

<div class="post-metadata">

**Author:** ![8art-5imson](https://avatars.discourse-cdn.com/v4/letter/8/a6a055/32.png) [@8art-5imson](https://forum.arduino.cc/u/8art-5imson)\
**Post date:** [August 1, 2023, 7:25am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/7 "2023-08-01T07:25:11Z")

</div>

Are now i get it, I was confused as the squeezelite software I have on the esp32 has a GPIO setting for CS, but also a place to put the address. I guess the address is optional as i only have the one MCP connected !!  
Thanks Chris.

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [August 1, 2023, 8:15am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/8 "2023-08-01T08:15:32Z")

</div>

> [@8art-5imson](#):
>
> I guess the address is optional as i only have the one MCP connected !!

I can't recall off the top of my head. Even the SPI variant has a 3-bit address that's setup using those A0, A1 & A2 pins. You could look at the library code and see what address it uses as the default - maybe address 0, and set your pins on the chip accordingly.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [August 1, 2023, 8:21am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/9 "2023-08-01T08:21:21Z")

</div>

> [@markd833](#):
>
> Even the SPI variant has a 3-bit address that's setup using those A0, A1 & A2 pins

Indeed - see post #3, as edited.

As noted, for the SPI chip, the addressing is enabled/disabled by `IOCON.HAEN`;  
The default is **disabled** :

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/4/c/b4c59fd085b387e6e58f27449728f8c6ab781341.png)

When disabled, the chip hardware uses all-zeros for the address:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/0/9/1096a1f476fda5d46ba37b81f2b4eaaed801eff8.png)

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [August 1, 2023, 8:25am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/10 "2023-08-01T08:25:57Z")

</div>

> [@8art-5imson](#):
>
> I guess the address is optional as i only have the one MCP connected !!

No, the address is always required - see post #9

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [August 1, 2023, 9:54am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/11 "2023-08-01T09:54:07Z")

</div>

> [@8art-5imson](#):
>
> I guess the address is optional

As already mentioned, it is required.

Looking at the code for the MCP23S17 library, it defaults to address 0 to match the default setting of the actual chip.

This line of code:

> [@8art-5imson](#):
>
> `MCP23S17 MCP(10); // HW SPI address 0x00`

Sets the SPI CS pin to use but the library behind it auto sets the address to 0, making it look like it's not required but it's still passed in the SPI command to the device.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 28, 2024, 9:54am UTC](https://forum.arduino.cc/t/using-mcp23s17-port-expander/1153682/12 "2024-01-28T09:54:24Z")

</div>

Closed on January 28, 2024, 9:54am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
