Introduction: Simple Button Keypad - Microcontroller
Simple method for creating a custom button keypad that doesn't require separate hardware. Just your microcontroller buttons a few resistors.
First take a look at the schematic.
What we have here is 9 buttons and 6 signal lines (3 inputs, 3 outputs). The basic setup is in a grid pattern where the three input lines come down as columns (PIN0, PIN1, and PIN2), and the three output lines come across as rows (PIN3, PIN4, and PIN5).
Each button is a momentary ON button, normally open. So what will happen is when we press the button it will make a connection between that button's input and output, other wise there is no connection.
The key here is in the software.
What the software is going to do is sweep between the inputs and outputs, for every combination very fast. I will outline the steps for 1 input, and then it simply repeats the same steps for each other input.
For input on PIN0
-- PIN0, 1, 2 defined as outputs
-- PIN3, 4, 5 defines as inputs
1. Set PIN0, PIN1, AND PIN2 to LOW
2. Set PIN0 to HIGH
3. Check if PIN3 is HIGH or LOW, IF HIGH go to function SW1
4. Check if PIN4 is HIGH or LOW, IF HIGH go to function SW4
5. Check if PIN5 is HIGH or LOW, IF HIGH go to function SW7
6. Set PIN0 to LOW
This will then repeat with the only differences being which input pin is set to HIGH, and the names of the SW functions its going to.
So you can see the basic concept here. It turns one input high, then checks each of the outputs for a signal. If the signal is there it goes to that switch's function. If it doesn't find a signal at any output pin, it sets that input to LOW and sets the next input to HIGH and re-checks each output. This will continue to sweep like this until a signal is found.
You can then adjust the functions it switches to for your needs. The example sketch simply contains a serial print statement printing what button has been pressed.
You can adjust the code/schematic to however you need. Add buttons, remove buttons. My typical use is placing all of this into one function and then calling that function when the program expects a button to be pressed or is waiting for a button press.
Some key comments:
1. You can only have 1 input HIGH at a time. Not only will the outputs not read correctly, but you can short circuit things if you press the right combinations.
2. I would also suggest placing resistors somewhere on the lines. Whether it be one on each of the inputs or one on each of the outputs, it keeps from burning out your microcontroller
3. You can also place diodes to keep from shorting combinations, but they aren't necessary.
4. You may also need pull-up resistors in the system. sometimes you do, sometimes you dont.
I will work on adding additional circuit options and a sample arduino sketch for this.
We have a be nice policy.
Please be positive and constructive.