My task is to use a port extender (MCP23s17) to drive a touch keypad for which I have to define names of pins so that I can call those names in arguments of a function.

I have searched for different methods to define "PORT EXTENDER" pin but could not find any satisfactory results. Kindly help me handle the situation.

i have used the following libraries downloaded from Arduino playground 1. CapacitiveSensor.h 2. MCP23S17.h Now I have to use MCP23S17 Pin in the following function

#define key1 2
#define key2 4
CapacitiveSensor cs_4_2=Capacitivesensor(key2,key1);  //capacitive sensor is connected to pin 4 and 2

key1 and key2 are Arduino's pins. I want some method by which I can refer key1 and key2 to the port extender's (MSP23S17) pin. The above answers are surely helpful but they doesn't fulfill my requirement.

Thanks in Advance Sorry for English.

  • 1
    It all depends which library you are using.
    – Majenko
    Commented Aug 21, 2016 at 11:35
  • It is not clear what your question is about. It makes me wonder if this is a school assignment which you did not understand or you just simply don't know how to describe your problem. In any case, you have to provide more details: wiring, code you have written so far... In particular the sentence "I have to define names of pins so that I can call those names in arguments of a function" is not clear; maybe you could give the prototype of the function as you imagine it, that would give us food for thought.
    – jfpoilpret
    Commented Aug 21, 2016 at 11:43
  • i have to use multiple touch keypads with arduino via a port extender. I have managed to operate both the sensors and the port extender individually but when i came to combine them i faced a problem that when i call the library of touch keypads, i have to pass them the pin numbers on which they are connected. in my case i have connected them with port extender via SPI. i am confused how to refer port extender pin number because if i simply type a pin number, arduino will refer to its own pins.
    – Danyal
    Commented Aug 21, 2016 at 16:38
  • You may have to edit the touch keypad library's code or write your own to use the port expander's pins instead of the Arduino's own pins.
    – Liftyee
    Commented Aug 10, 2022 at 11:00

3 Answers 3


If you use this arduino library:

#define LED_PIN 15

mcp.pinMode(LED_PIN, LOW); //set led pin on the MCP23S17 to an OUTPUT

mcp.digitalWrite(LED_PIN, LOW)

If you are using my library you can do something like:

MCP23S17 expander1(&SPI, 10);

#define LED_PIN expander1, 15

void setPin(MCP23S17 &expander, int pin, bool level) {
    expander.digitalWrite(pin, level);

// ... later ...

setPin(LED_PIN, HIGH);

There is also the Adafruit Library, all do similar things, just in a slightly different way. I use this one, it just depends on which method you are most comfortable with and what your requirements are. Try them all and find out.

void setup() {
  Serial.begin(9600);  // debugging  
  mcp.begin();      // use default address 0

      for(int i = 0; i < 6; i++) {
        mcp.pinMode(trafficLights1[i], OUTPUT);
       for(int i = 0; i < 6; i++) { 
        mcp.pinMode(trafficLights2[i], OUTPUT);
        for(int i = 0; i < 5; i++) {
        mcp.pinMode(trafficLights3[i], OUTPUT);
  for (int P=0;P<16;P++) {
    mcp.pullUp(P, HIGH);  // turn on a 100K pullup internally
      mcp.digitalWrite(11, HIGH);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.