# PCA9555

This chapter will set up your port expander as an input device. If you're using at as an output device, you'll find the [documentation here. ](https://dahl-design.gitbook.io/ddc/3.-coding/peripherals/digital-outputs)

Adding the necessary code for PCA9555 requires two steps:

* Setup
* Run&#x20;

### Setup

The setup is done in 2\_Board.ino. Set up your [I2C channels](https://dahl-design.gitbook.io/ddc/3.-coding/peripherals/i2c-devices), then scroll down to this section:

```
//PORT EXPANDER PCA9555
#define USING_PCA9555 0
#define PCA9555_I2C_NUMBER 0
uint8_t PCA9555interruptPins [] = {99};
#define PCA9555_OUTPUT 0
```

<mark style="background-color:purple;">USING\_PCA0555</mark> is to disable/enable support for this IC. Set it to 1 to enable.&#x20;

<mark style="background-color:purple;">PCA9555\_I2C\_NUMBER</mark> lets to chose between I2C channel 0 or 1 on a RP2040 board. If you're not using a RP2040 board, just leave this at 0.

<mark style="background-color:green;">PCA9555interruptPins</mark> is an array where you put in the pin numbers for the pins used for [interrupt.](https://dahl-design.gitbook.io/ddc/2.-wiring/switch-inputs/port-expander#input-pins) If you have several PCA9555 chips, list all the pins here.&#x20;

<mark style="background-color:blue;">PCA9995\_OUTPUT</mark> is to set up the port expander as an output device. We're not going to use this if you're using it for switches.&#x20;

Example: We have two PCA9555 using interrupt pins 10 and 16:

```
//PORT EXPANDER PCA9555
#define USING_PCA9555 1
#define PCA9555_I2C_NUMBER 0
uint8_t PCA9555interruptPins [] = {10,16};
#define PCA9555_OUTPUT 0
```

### Run

You'll need to get the processor to read your PCA9555 and put the input values into the switch table. This is done with a simple function that you place in `30_Switches.ino` together with all your button functions. I recommend placing it at the very top, before anything else.&#x20;

#### PCA9555Run()

The function wants to know a couple of things:

* <mark style="background-color:purple;">address</mark>: The I2C address of this PCA9555 chip.
* <mark style="background-color:purple;">interruptPin</mark>: The pin number of the interrupt pin used for this chip.
* <mark style="background-color:purple;">row</mark>: The row at which this port expander will start placing its input data. It will use the first 8 columns on this row, and the first 8 columns on the next row. Example: setting row to "4" means you'll find the expanders inputs on address 4,1 - 4,8 and 5,1 - 5,8 in the switch table.

Now, lets set up the two PCA955 in the example at the top of the page, and use rows 1 to 4 for these. We've wired 8 push buttons and 4 PEC11 encoders to each of them:

```
  //--------------------------------------
  //---------SWITCHES START HERE----------
  //--------------------------------------
  
  PCA9555Run(0x20,10,1);
  PCA9555Run(0x21,16,3);
  
  pushButton(1,1);
  pushButton(1,2);
  pushButton(1,3);
  pushButton(1,4);
  pushButton(1,5);
  pushButton(1,6);
  pushButton(1,7);
  pushButton(1,8);
  
  PEC11(2,1,false);
  PEC11(2,3,false);
  PEC11(2,5,false);
  PEC11(2,7,false);
  
  pushButton(3,1);
  pushButton(3,2);
  pushButton(3,3);
  pushButton(3,4);
  pushButton(3,5);
  pushButton(3,6);
  pushButton(3,7);
  pushButton(3,8);
  
  PEC11(4,1,false);
  PEC11(4,3,false);
  PEC11(4,5,false);
  PEC11(4,7,false);
  

  //--------------------------------------
  //---------SWITCHES END HERE------------
  //--------------------------------------

	Joystick.setZAxis(rotaryField - 32767);
	Joystick.setYAxis(buttonField - 32767);

	Joystick.sendState();

} //LOOP ENDS HERE

#if (BOARDTYPE == 2)
  void loop()
  {
	#if (LED1COUNT + LED2COUNT + LED3COUNT + LED4COUNT > 0)
		processCommands();
	#endif
  #if(USING_CB1 == 1)
  CB1Oversampling();
  #endif
  }
#endif
```
