# Create binary sequence to use less inputs

Let's say I have 63 buttons, but there are only a few inputs on Arduino. I want to know which one was pressed. To minimize the number of inputs used, I could tell from a binary sequence. If I press button 61, I need to have six inputs returning 1,0,1,1,1,1. How would I convert the button number to binary?

If you know any easier way tell me, it's the only that came to my mind.

• What you describe is called a priority encoder. – MarkU Nov 10 '15 at 21:59
• I would look into an I2C port expander IC. That would be just 2 serial pins from your arduino, and you could use up all the parallel pins on the external IC. – RYS Nov 11 '15 at 5:43

6 pulldown resistors, one to each data line. 1 to 5 diodes from each button, the button connected to vcc.

Button 1 has one diode to pull up the d0 line when pressed.

Button 61 has 5 diodes to the d5, d3, d2, d1, d0 lines as for your example above.

Button 0 not used. When any button is pressed, at least one of the data lines will go up. Can be used for interrupt on button press.

Suitable pulldowns, almost any value, 10k will do.
Suitable diodes, almost any diode, 1N4148 will do, quite cheap by the 100s.

This is a static solution, simple, but uses shed-loads of diodes. There are others.

You could put them into a matrix, scan and decode.
You could put them into a shift register.

As Arduino has ADC inputs, you could use each button to switch various value resistors, to change the voltage into the ADCs. Whether you could get enough resolution to resolve all 61 buttons without calibration or adjustment is another matter, you might want to split it into 4 channels of 16 buttons.

One possible way is to use input shift registers to perform a parallel to serial conversion. Shift registers are covered in great detail on the net (try reading this description), so I won't go too far into it here. You would need several (9 in your case) of them, as typically each one has eight inputs. They will be wired together in a chain to create a single 64-bit number, or several smaller numbers: the choice is yours.

I do this kind of thing all the time, and it is pretty easy. You could create three 24-bit values and read them periodically, then perform an XOR with the previous values in order to detect changes. This will require 3 inputs and one output (for a clock) from your Arduino. If you are limited on inputs or simply prefer it, chain the registers together to make a single 64-bit number and then you'll only need 1 input pin, although you may not be able to store all of those bit in one value on Arduino, so it would take more than one read.

In any scenario above, it is stuff a beginner can do.

• Both answers seem as good but which choice would take the least room to implement – Nicolas Nov 11 '15 at 1:06