Creating a 1-wire Keyboard

by Cesosas in Circuits > Electronics

2583 Views, 21 Favorites, 0 Comments

Creating a 1-wire Keyboard

FK9UKWIKVNTIR8T.png

Hi, my name’s Carlos 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.

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. 

Some Formulas

Resistors_Series.JPG
Resistors_Parallel.JPG
Voltage_Divider.JPG

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. 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)

The Initial Layout

Matrix_Simple.JPG
Matrix_Resistors.JPG

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. 


Thinking Just Like an Engineer

Desired Behavior.JPG

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). In this case, for 20 buttons, the increment between any two successive buttons is equal to:

V=Vcc/(20+1)

V = 5/21=0.238V

The graph shown in the first image gives a better explanation of this behavior. 

Restrictions simplify problems

To continue with the solution, we must specify at least one value for any resistor. In this case, I selected the uppermost resistor, the one connected to Vcc, as a known value and started deriving equations for every button in the array.

Let’s suppose that this resistor has a value of 1kΩ. With this restriction, it is now possible to calculate the remaining values. 

As another restriction, we do not want to read 0V at any moment, therefore we divide the voltage by the number of buttons plus one.

The Final Layout

FInal_solution.JPG

After some failed attempts to resolve for the desired behavior, I encountered some problems when trying to solve the resulting equations for the keyboard. That's why, after some experimentation, I came with the solution layout presented in the image above.

In this layout, there are some extra resistors between the columns’ terminals, between rows also, and some of these are now connected to ground. Also, the matrix pads are “broken” as the connections between columns have been omitted.  

With this new layout, the resistors connected from the columns' terminals to ground have the same value.

Finding the Resistors’ Equations

Button _1_2.JPG
Button _3_4.JPG
Button _5_resistors.JPG
Resistors_Column_1.JPG
Resistors_Column_2.JPG

From the diagram presented in the previous step we can obtain a set of independent equations that will allow us to determine the exact value for every single resistor. For this, we’re going to use the formulas presented earlier. We’ll derive equations for every button in the uppermost row and repeat the process afterwards for the remaining ones. 

The equations are presented in the images above.

The first three images show a direct method for obtaining the corresponding values for resistors R1 through R5.

In the latter photos, the equation systems for finding the respective Rb to Rd for every column are shown. I’ve derived the equations for the first two columns and noticed a pattern in them. By sticking to the same idea, the resting systems can be obtained. 

Finding the Resistors’ Values.

Resistor_values.JPG

Once the equations are obtained, we can solve them by using an online tool for this purpose. I decided to use this one. This task is rather cumbersome. That's why, rather than solving every system, I'll just give you the important results. These are described in the table above.

Using Commercial Values

Resistor_values_Comercial.JPG

Even though it would be great to have resistors with any possible value, reality is harsh sometimes. Given that most of the resulting values for the resistors are unconventional, it is necessary to use the closest ones to those commercially available. For this task, you can use this site

We can calculate the error for any given resistor by using the following formula:

e = 100 *(Rt-Rr)/Rt

Verifying the Results

simulation.bmp
Simulation results.JPG
Simulation Diagram.JPG

Even though I’m writing this Instructable right now, I haven’t been able to implement my idea. However, it is now possible to verify it by simply creating a simulation of the circuit we’re interested in. For this purpose, I created a circuit design using TinkerCad, wich you can find here. This simulation contains resistors with the commercial values connected to the purposed layout for our matrix.

After running the simulation and measuring the voltage of interest, wich you can see in the video above, the results are shown in the table above. 

Coming Next…

So, that’s all for now my friends.

At this moment, I'm not able to physically test my creation. Nonetheless, I can assure you that when implementing it in real life, it should work just fine.

I’m looking forward to creating a physical keyboard by creating a custom PCB and verifying my initial results. If you’re interested in this, just let me know and I’ll show you in a further Instructable.

Sincerely,

Carlos. 



P.S.

This same idea could be used to create smaller or even bigger matrices. In this case, I decided to design the circuit as part of a bigger project, which requires 20 buttons. However, it is feasible to modify it so that you could use any number of buttons.