Read n × m contacts with n outputs and m inputs: calculation, scan time and phantom readings
Click the cells to close or open contacts in a matrix of 4 outputs (Q0–Q3) by 4 inputs (I0–I3). Each cell shows what the PLC reads when it drives that row. Close three contacts forming an "L" and remove the diodes to see the fourth one appear.
When many contacts have to be read but are rarely operated at the same time, such as a keypad, a multi-position selector or a push-button panel, you do not need one input per contact. They are arranged in a matrix: each contact joins a row, fed by a PLC output, to a column, wired to an input. The program switches the outputs on one at a time and, while a row is active, the inputs that turn on show which contacts of that row are closed. With n outputs and m inputs you can read up to n · m contacts: 4 outputs and 6 inputs are enough for 24 push buttons instead of 24 inputs.
Each crossing is a contact with its series diode, connected between the row (output) and the column (input). Lines that cross without a dot are not joined; the contact is drawn open.
In a PLC the outputs are written at the end of the cycle and the inputs are read at the start of the next one. On top of that, the output takes time to switch and the input has a filter of a few milliseconds against bounce and noise. So each row must stay active for several cycles before its columns are read, and the full scan takes n times that. A contact that closes and opens in less than one full scan may be missed: with 8 rows, a 3 ms filter and a 10 ms cycle, each row needs 20 ms and the scan 160 ms, too long for a button tapped quickly.
If the active row has a closed contact and another row has a closed contact in the same column, current can flow back through that other row and out through a third contact into another column: the PLC reads a contact nobody pressed. It happens with three closed contacts at the corners of a rectangle. The cure is a diode in series with each contact, pointing from row to column, which blocks the reverse current. With PNP outputs (supplying 24 V) and inputs for PNP sensors, the anode goes towards the output. Common signal or rectifier diodes such as the 1N4148 or 1N4007 work; they drop about 0.7 V, which a 24 V input tolerates easily.
It is worthwhile for keypads, BCD or rotary selectors, multi-position controls and test panels on small machines where an extra input module is expensive or there is no room. When many fast or independent inputs are needed, distributed I/O, IO-Link or an additional input module is usually better. For a few buttons there is also the trick of reading them with one analog input and a resistor ladder, each button giving a different voltage.
See also: 24 V inputs to a microcontroller, Push-button debouncing, Multiplexers and Ladder logic simulator.