Creating a 1-wire Keyboard
Introduction: Creating a 1-wire Keyboard
Hi, my name’s <PERSON> and this is my first Instructable!
Throughout this Instructable I will show you a simple way to create a keyboard “matrix” with up to 20 buttons that can be read using a single Analog Pin. In this way, you can use it with an Arduino board or any other microcontroller to create more complex interfaces without sacrificing your board’s precious pins.
Step 1: Why Should You Try It?
When developing an Arduino (or any other microcontroller) project, sometimes it is handy to use a keyboard for the user to be able to interact with our invention. This is a simple and easily manageable feature when we only need a few buttons or switches.
When our project requires multiple inputs, it becomes handy the use of keyboard. However, using a conventional keyboard, created by using a 4x3 or a 4x4 button matrix, still requires multiple pins from our board.
After doing some research, I came to a possible turnaround. You could connect multiple resistors across the keyboard’s pins and use one of these as an output, so that you get a different voltage for every button that is pressed.
This seems like a quite clever solution. However, anywhere around these tutorials could I find a specific way to know the value for each button beforehand, and there seems to be no consensus about the resistors’ values for this to work.
That’s when I came up with a simple question: What if you could arrange the buttons and adjust the resistors in such a way that you get exactly the voltage you want?
That my friends, is what we’re about to do.
Step 2: Some Formulas
The proposed layout for our matrix takes advantage of some simple concepts used for analyzing circuits. These are:
Ohm’s Law
This is a fundamental law for dealing with electronics. It states that the voltage “v” across a resistor is directly proportional to the current flowing through the resistor [1], this is
v = iR
Resistors in series
When having multiple resistors connected in series, just like in the first image, the equivalent resistance of the resistors connected in series is the sum of individual resistances. That is:
Req = R1+R2+...+Rn
Resistors in parallel
When having multiple resistors connected in parallel, just like in the second image, the equivalent resistance of the parallel resistors is equal to the product of their resistances divided by their sum. 769
That is
Req = (R1 x R2)/(R1 + R2)
Voltage divider
Without getting into many details, a voltage divider has a topology like the one shown in the third image. In general, if a voltage divider has N resistors in series with the source voltage v, the nth resistor will have a voltage drop of
Vn = V*(Rn)/(R1 + R2 +...+ Rn)
Step 3: The Initial Layout
As I’ve mentioned earlier, we’re creating a button matrix. For this purpose, we start from the normal button matrix layout and teak it a little to reach our goals. So, let’s begin.
In a normal button matrix, each button is connected to those in the same row/column. This layout is shown in the first image.
To start with the modifications, we should put some resistors (the values are not important yet) between each pair of terminals and one extra connected between the top row and Vcc (for this example: +5V).
Additionally, we should add a reference, AKA ground node. In this case, we select the lowermost right button’s terminal, the one where the resistor R8 is attached, to connect our ground. This new layout is shown in the second image.
Step 4: Thinking Just Like an Engineer
Up to this point, it may not be clear for everyone where are we heading. However, let me tell you that from here on, just by following a simple “algorithm” it is quite easy. But first, just as the title for this step reads, let’s think like an engineer.
For this, we must:
specify the desired behavior
For this keyboard, we want the following behavior:
If no button is pressed, the output must read 5V
If a button is pressed, the output must read a proportional value between 0 and Vcc. Additionally, each voltage step should be an equal portion of Vcc divided by the number of buttons plus one (I’ll explain why later). 134