# Reading hundreds of inputs with a single atmega32

I have 128 inputs (push buttons) in a single row in a project and I want to read them using a single atmega32. Of course there is not enough pins in an atmega 32 for this purpose and I know the solution would be using some kind of multiplexer but I don't know which one suits the best.

What I'm thinking is to have some kind of multiplexer that have 128 pins as input and have 7 pins for output that represents the result of inputs into binary (as an example if all inputs were 1 (UP) and output pins will be 1111111 (all UP)). But I'm not sure if there is any multiplexer doing this ?

I have to mention that I need a cost effective solution. Becasue I have to extend this solution to maybe 1000 or even 10,000 inputs in the future (which we have to use several multiplexers to read all of them)

Any help would be appreciated. I need to know which multiplexer I need to use ? (probably part no. or any other suggestion)

Update:

Only one single button will be pushed at the one given time AND it update rate won't be fast. maybe 10-30 times per second or much less.

• It depends. Can two buttons be pushed at the same time? Can N buttons be pushed at the same time? How fast should it be? Commented Sep 24, 2015 at 19:30
• @EugeneSh. Well, if this matters, the short answer is no. Only one single button will be pushed at the same time. Commented Sep 24, 2015 at 19:31
• @EugeneSh. Oh and it won't be fast, it will be in human speed. Maybe 10-20 buttons per second. (one by one, not at the same time) Commented Sep 24, 2015 at 19:33
• Than an encoder (or Priority encoder) is the circuit that is doing this job. It will compress 2^N inputs into N. The point is that probably you won't find any 128bit-wide encoder on the shelves. So I would look into the direction of semi-analog solution similar to this. Or just use an FPGA (kinda overshoot) Commented Sep 24, 2015 at 19:41
• Another possibility would be to use a PISO shift registers chained, while reading them serially. Commented Sep 24, 2015 at 19:42

An easy approach would be to use hardware SPI to read shift registers serially.

You could daisy-chain as many 8-bit shift registers as required. If you use 74HC165 you could simultaneously sample each switch (1/8 74HC165, 1 resistor required per switch) and then shift the data to the micro at perhaps 10MHz, so you could get 1000 switch states in 100 usec or 10,000 in 1ms. 10,000 inputs would require 1250 shift registers so you would need to buffer the clocks and/or reduce the shift rate.

Physical bus length will become a problem at high clock rates- you have to make sure that datasheet setup and hold times are observed.

• I think this would work for me : arduino.cc/en/Tutorial/ShiftOut ... Because I'm using arduino. What do you think ? Commented Sep 24, 2015 at 20:23
• I think you can do that, but shift in. Note the part number is different for PISO vs. SIPO shift registers. Commented Sep 24, 2015 at 20:26
• Yes, that's the exact idea (using a different shift register). As I said though, some complications will manifest if you try to expand this too much but 128 shouldn't be a problem. Commented Sep 24, 2015 at 20:47
• Yes, probably with modifications to buffer the two clock lines (called clock and latch in the Arduino tutorial). And not too fast or too physically large. The two outputs could drive 8 buffers each which could drive 150 chips (not too fast). Still only 3 microcontroller pins required! Commented Sep 24, 2015 at 20:59
• Yes you can read any combination of switches captured at one instant in time. You get one bit for each switch. Commented Sep 25, 2015 at 3:48

Break the buttons into rows and columns, like is done with a keyboard. 12x12 will give 144 possible buttons, so should work.

Then you'll need to use encoders to translate those lines into binary. 3 of 8-bit encoders will take in all 24 lines and give you 9 bits to connect to your microcontroller.

• Looking at how keyboards work is the right answer, particularly if you're really going up to huge numbers, as your i/o complexity grows as the square-root of the number of switches, rather than linearly.
– user1844
Commented Sep 24, 2015 at 20:40
• Yes, especially with the notation that only one button will be pushed at a time and the necessary polling rate is low, this sounds like an good solution. Commented Sep 24, 2015 at 21:55
• Won't it be 10 bits (8 bit per encoder, plus 2 bit for choice of encoder)? Also, I am a bit confused by this question, because with 12x12 I would expect that you need 12 output lines and 12 input lines, so one 12-bit encoder and one 12-bit decoder? Commented Oct 22, 2015 at 9:14