Keypad Module question

MrChips

Joined Oct 2, 2009
30,707
1578275878561.png

Your diagram is the same as mine.

What you see is the simplest case of the 4x4 switch matrix interface.
This is what you have to work with.
Tell me how you understand the functioning and coding for this circuit.
 
OK, pretend you have 1 row and 4 swiches. You would need a pull up to +5 on the row. As each column is pushed., you would get a low on that column. So, that's like 4 independent switches. No big deal. right? Now use a port for the row, with a pull-up.

Now if you made two rows, you need a way to select each row or to pull it up to +5. That's an output. Now scan the outputs and check for a value on the input.

You have to turn a row/column into a single "scan code", e.g 0-16.
e.g. Row 1, column 1 returns a 1. Row 2, column 1 returns a 4.

Software debouncing complicates things a bit.

It's nice to have consecutive bits to make programming easier. If they are in the middle somewhere, you have to do shifts when reading the port.

You don;t want bit 3 = column 1, bit 2 = column 4, etc.
You would "like" bit 1 to be column 1, bit 2, column2, bit 3 to be column 3 etc. or
bit 9 to be column 4, bit 8 to be column 3 etc. so a simple shift gets you a 1-3.

Same for the rows.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
View attachment 196190

Your diagram is the same as mine.

What you see is the simplest case of the 4x4 switch matrix interface.
This is what you have to work with.
Tell me how you understand the functioning and coding for this circuit.
There is only two possible conditions

1578403696563.png

C:
void main ()
{
    if ( P1 == High)|| (P2 == High)
        Button pressed
    else
        Switch button not pressed
}
 

jpanhalt

Joined Jan 18, 2008
11,087
Once again, this dialog seems headed off on a tangent.

If one does even a cursory search on "4x4 keypad wiring" or "4x4 keypad circuit," you will find there are various ways to arrange it that use 8 pins and even other ways that use fewer pins, including just one pin of the MCU. We need the TS to post a complete schematic of how he plans to wire the keypad and MCU. It does not have to be something he will actual build, but it needs to be something definite (and correct) as a basis for discussion.

In light of that, trying to write meaningful code to do what you don't know you need to do seems likely to be nonproductive.
 

MrChips

Joined Oct 2, 2009
30,707
You are making the same mistakes again. You are attempting to write code without first understanding the problem.

Here are the steps for computer problem solving:

1) Understand the problem or define the problem.
2) Draw a flow-chart.
3) Write the code.

1578414033940.png

Here you have a switch S1 connected to two GPIO pins, P1 and P2.
When S1 is pressed what can the program detect?

At this time, you have not gone past Step 1.
 

MrChips

Joined Oct 2, 2009
30,707
Ignore your most recent circuit diagram. We are not discussing that.

This is the circuit we are working on:

1578423126624.png

This is your problem. Try to understand how to make this work. This is your challenge.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Ignore your most recent circuit diagram. We are not discussing that.
This is your problem. Try to understand how to make this work. This is your challenge.
Your circuit diagram is incomplete. You did not say which pin is the input and which is the output.

I am assuming P1 is output pin and P2 input pin
1578514173520.png

When P1 is low and if we press the switch, P2 will also low
When P1 is high and if we press the switch, P2 will also high
 

MrChips

Joined Oct 2, 2009
30,707
Your circuit diagram is incomplete. You did not say which pin is the input and which is the output.
Exactly. This is your challenge to figure out.

I am assuming P1 is output pin and P2 input pin
View attachment 196447

When P1 is low and if we press the switch, P2 will also low
When P1 is high and if we press the switch, P2 will also high
Yes. But what happens when S1 is not pressed?
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Yes. But what happens when S1 is not pressed?
When P1 is low and if we do not press the switch, P2 will also low
When P1 is low and if we press the switch, P2 will also low

When P1 is high and if we press the switch, P2 will also high
When P1 is high and if we do not press the switch, P2 will also low

if we check P2 and if it's high it means switch pressed
if we check P2 and if it's low it means switch not pressed
 

MrChips

Joined Oct 2, 2009
30,707
When P1 is low and if we do not press the switch, P2 will also low
When P1 is low and if we press the switch, P2 will also low

When P1 is high and if we press the switch, P2 will also high
When P1 is high and if we do not press the switch, P2 will also low
This is where you make a common assumption that is wrong.
When SW1 is not pressed, there is no place for any charge on P2 to go.
The correct answer is: The voltage at P2 is not known.
How would you correct this?
 
Hint: One is defined as an output port and the other an input port. Look at where you would need pull-up or pull-down resistors. can the processor provide them?

You don't want any unknown inputs.

if you don't enable the switch, no matter how many times you push it, it should not register as pushed.

In the 4x4 Matrix, your enabling only one row of switches at a time.

Make any sense?
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
This is where you make a common assumption that is wrong.
When SW1 is not pressed, there is no place for any charge on P2 to go.
The correct answer is: The voltage at P2 is not known.
How would you correct this?
This is my circuit diagram by using pull up resistor to read input pin

1578536624827.png
 

MrChips

Joined Oct 2, 2009
30,707
NOTE: In your drawing, the input is shorted to GND and will always read as LOW. Remove the connection to GND.

1578538974442.png

You can use an external pull-up or pull-down resistor.
Some MCU will allow you to enable a weak pull-up or pull-down resistor.

Before we go any further, what is a weak pull-up resistor?
To understand the meaning of weak we have to look at the input current of the input pin.
A typical CMOS input will have input currents of about 1μA. Hence we want a pull-up resistor to source much more than 1μA and a pull-down resistor to sink much more than 1μA.

With 5V source voltage, a 5MΩ resistor can only supply 1μA, while a 500kΩ resistor can supply 10μA, and 50kΩ can supply 0.1mA.
A pull-up or pull-down resistor lower than 1kΩ would be considered a strong pull-up or pull-down.
A pull-up or pull-down resistor higher than 20kΩ would be considered a weak pull-up or pull-down.
 

MrChips

Joined Oct 2, 2009
30,707
Ok. Now apply the pull-up resistors to the 4 x 4 keypad matrix.

How would you detect that a key is pressed?
Any key. At this point we are not interested in which key is pressed.

Write out the solution in pseudo-code.
 
Top