Take a look at this simple matrix circuit. It requires only sqrt(n)*2 GPIOs. Scanning through 'rows' is done by turning the IR LEDs on and off. Only one 'row' is lit at a time, which also saves power. It shows 9 sensors in 3x3 matrix connected to 6 GPIOs. ![cir](https://i.sstatic.net/k8s7m.png) External transistors are needed because with larger number of LEDs, the current draw gets significant. Either N-MOSFET or NPN will do. G[0..2] is input for switching LEDs, C[0..2] is output from phototransistors. Light=logic low, no light=logic high.