Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:42 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : 4 x 4 matrix keypad

     Page 1 of 2    
Author Message
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 02:39pm 03 Feb 2014
Copy link to clipboard 
Print this post

Hi All,

Did a bit of digging in the forum and could not find any info or code to read and effectively use a 4 x 4 matrix key pad like the typical one attached (picture). This is a 4 row and 4 column matrix, basically using 8 I/O pins. I did find the matrix keyboard to analog pin circuit and code, but does anyone know of some good code to scan and read one of these using the digital pins instead? I would like to interface one of these to micromite and would appreciate any info one may have. Maybe using a tick interrupt routine? Thanks to all!!!



 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 02:54pm 03 Feb 2014
Copy link to clipboard 
Print this post

Perhaps there is something in the MMBasic library available from Geoff's site?

From what I understand, you connect the rows to outputs(or is that inputs?), and the columns to inputs(or is that outputs?), then the code pulses the output lines one after another, looking for one of the inputs to go high, indicating that that button on that row and column has been pressed.

Should not be that hard to code - I might have a go at this as a MM experiment in the next few days.

Others will no doubt chime in with their ideas....

EDIT: I've just had a sniff around in the latest MMBasic library, but can't find anything there. Would be useful. I will play around, and see if I can come up with something, and I am quite sure that other members will add their own ideas too.
Edited by Grogster 2014-02-05
Smoke makes things work. When the smoke gets out, it stops!
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 03:14pm 03 Feb 2014
Copy link to clipboard 
Print this post

Some years ago, I did a I2C version of this same (or at least very similar) key pad.

The first one I did was for a 12 key unit (4x3) but as there were spare pins on the pic, I did a 4x4 version. I thought I posted it on TBS but maybe not the code? Don't remember, but I am more than happy to re-hash it and provide the cct diag and source/hex code. It was for the PIC16F88/819 chips.

The keypad in downwind's solar tracker currently being discussed uses the 4x3 version.
David M.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 03:26pm 03 Feb 2014
Copy link to clipboard 
Print this post

  MOBI said  I thought I posted it on TBS but maybe not the code? Don't remember, but I am more than happy to re-hash it and provide the cct diag and source/hex code.


That would be useful - go right ahead!
Smoke makes things work. When the smoke gets out, it stops!
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 03:35pm 03 Feb 2014
Copy link to clipboard 
Print this post

I found the 4x3 keypad on TBS with a zip file with code etc.
http://www.thebackshed.com/forum/forum_posts.asp?TID=5379

Meanwhile, I'll see if I can scrounge up the data on the 4x4 keys
David M.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 03:37pm 03 Feb 2014
Copy link to clipboard 
Print this post

Hi Early Responders!

I am trying to use just the micromite to read the keypad (using 8 pins) and run a 4 bit lcd (2004) (using 6 pins). This will still leave 7 I/O pins to use for other things. I am trying to keep the part count down to a very minimum so using another chip for a keypad would not work in this instance. As the micromite is relatively cheap for what it can do, this allows for a low cost yet very powerful little controller with minimum components. Thanks again for any and all help on this keypad routine. I did not see anything in the library unless I am going blind...
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 03:47pm 03 Feb 2014
Copy link to clipboard 
Print this post

  Quote  I am trying to use just the micromite to read the keypad (using 8 pins) and run a 4 bit lcd (2004) (using 6 pins). This will still leave 7 I/O pins to use for other things


I understand. Scanning the keypad wasn't difficult. All I did was cycle a "1" through the 4 outputs and read the inputs which gave me a 16 bit count.

There were two options for key value. One was a key number from 1 to 16 and the other was to feed the key value to an array which contained the ascii key values or what ever you like.

Personally, I think it is a lot of wiring which can be reduced to 2 wires using I2C which the MM and microMite (uMite still in beta) do very well. The other thing about I2C is that the coding is very simple, besides, you can "plug and play" additional modules.


David M.
 
akashh
Senior Member

Joined: 19/01/2014
Location: India
Posts: 115
Posted: 03:54pm 03 Feb 2014
Copy link to clipboard 
Print this post

You would set up a matrix like this:


R1 ----- x -- x -- x -- x
| | | |
R2 ----- x -- x -- x -- x
| | | |
R3 ----- x -- x -- x -- x
| | | |
R4 ----- x -- x -- x -- x
| | | |
C1 C2 C3 C4


Connect each button to the row and column at the x. If you have a ready made keypad the chances are it will be wired like this, you can check with a multimeter. Set pins R1 to R4 as outputs, and C1 to C4 as inputs. Now periodically set each Row pin to high and see whether any column inputs are high.
I am not sure whether mmbasic supports this but I would try this: create an interrupt routine when any pin c1 to c4 goes high. Then make all row outputs high. Now whenever any key gets pressed the interrupt will get called, and you can quickly change the row outputs to determine which row was pressed. This would eliminate the need for regular polling.
Edit: sorry the matrix didn't come out as well as I would like it toEdited by akashh 2014-02-05
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 04:01pm 03 Feb 2014
Copy link to clipboard 
Print this post

  MOBI said  Personally, I think it is a lot of wiring which can be reduced to 2 wires using I2C which the MM and microMite (uMite still in beta) do very well. The other thing about I2C is that the coding is very simple, besides, you can "plug and play" additional modules.


Have to agree with MOBI here. If I was doing any kind of keypad and LCD idea, I would be inclined to use I2C keypad, and an I2C LCD module arrangement too, both on the I2C bus with the MicroMite(or other) as the master.

My 2c only.

Nothing wrong with the idea of squeezing it all into the one chip though.
I suppose if you mount the keypad above the PCB, and do the same with the LCD, then the one PCB can have the processor, the LCD and the keypad, and include all of the connections on the one board.

Each to their own.
Smoke makes things work. When the smoke gets out, it stops!
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 04:10pm 03 Feb 2014
Copy link to clipboard 
Print this post

That interrupt thing sounds like a good start. I believe any pin on the micromite can be used to have an interrupt routine occur. I will approach this that way. That way, I won't be polling a lot. I do agree with the I2C thing, but in this particular instance, hopefully just one chip will be used (micromite). I will keep trying to get this going and am searching the web on this subject. Keep the great ideas coming! Maybe this solution can be a library item soon.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6101
Posted: 04:45pm 03 Feb 2014
Copy link to clipboard 
Print this post

You might need pulldown resistors to stop unpressed lines from floating high.

I would make all lines inputs and only change one at a time to a high output.
If you leave some lines as low outputs and two buttons are pressed at the same time, you may have a short circuit between the high and low outputs.

All this is easy to do with the Maximite. I do have one of the keypads shown but have yet to do any code for it.

Jim


VK7JH
MMedit   MMBasic Help
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 05:02pm 03 Feb 2014
Copy link to clipboard 
Print this post

  jim said  You might need pulldown resistors to stop unpressed lines from floating high.


Resistors (10k) were required in my i2c model. As well, I set all pins as inputs and only enabled 1 row at a time (except when looking for "keypress" where all rows were activated). Also, seeing as there were 12 pins available, the keyboard could accommodate 32 keys. So, two of the membrane pads could be connected giving a more powerful keypad. Edited by MOBI 2014-02-05
David M.
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 06:46pm 03 Feb 2014
Copy link to clipboard 
Print this post

There was a scanned keypad + lcd arrangement for the bare-bones MM in the
ideas section of "Silicon Chip" a while ago, I think the basic code was
put on their website (?)
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 07:17pm 03 Feb 2014
Copy link to clipboard 
Print this post

You also have to take care of 'key bounce', especially when using an interrupt.
Instead of registering a single keydown you might have tens.
To debounce it you wait a little (5-10ms) and then read the same column and row again to see if it is still pressed.
Then next scan should run about 50ms later, this will give you about a 20 per second repeat rate or you could wait until the key is released before starting a new scan.
Also the release can have bounce. Some procedure.



Microblocks. Build with logic.
 
elproducts

Senior Member

Joined: 19/06/2011
Location: United States
Posts: 282
Posted: 07:42pm 03 Feb 2014
Copy link to clipboard 
Print this post

If your open to a free compiler and 8-bit part I have a keypad and LCD code examples for Great Cow Basic at this site:

http://www.greatcowbasic.com/sample-projects.html

GCB has a keypad and an LCD command built into its library.



www.elproducts.com
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 08:01pm 03 Feb 2014
Copy link to clipboard 
Print this post

  Quote  You also have to take care of 'key bounce'


The I2c module takes care of all the debounce etc and having registered a key pressed, will not accept another key until the master has read the key and reset the keypad. It is easy to check if a key has been pressed by testing a control byte. The control byte is used to reset the keypad and to test for a key press. If a key is held down, it will be recognised pretty quickly and can be read and the programme redirected as needed other wise the programme drops through and only uses a few milliseconds.

Hope that makes sense.

David M.
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 09:14pm 03 Feb 2014
Copy link to clipboard 
Print this post

If you're open to using a different technique with the 4x4 and 4x3 keypads, do a forum search on "4x4 Matrix board with one analogue input" using "Topics". I posted a method (17 July 2012 - with photos and code) that uses a two-wire approach, rather than the eight needed for the scanning approach. It finds the pressed key by reading one analog input and works pretty well.

There are some drawbacks:
1. Extra components needed, but they're very cheap - resistors, one cap, one LM334Z plastic TO9 package.
2. "Double" keypresses, i.e. Ctrl C etc. can't be used.

Greg
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 09:27pm 03 Feb 2014
Copy link to clipboard 
Print this post

  paceman said  If you're open to using a different technique with the 4x4 and 4x3 keypads, do a forum search on "4x4 Matrix board with one analogue input" using "Topics". I posted a method (17 July 2012 - with photos and code) that uses a two-wire approach, rather than the eight needed for the scanning approach. It finds the pressed key by reading one analog input and works pretty well.


Yes, that was pretty nifty. Just goes to show that there are lots of options and innovations.
An analogue pin on a PIC i2c slave can also be used to select up to 8 possible addresses using a voltage divider and a bit of firmware.
David M.
 
viscomjim
Guru

Joined: 08/01/2014
Location: United States
Posts: 925
Posted: 10:43am 04 Feb 2014
Copy link to clipboard 
Print this post

Looks like the I2C and analog read are pretty popular. Wouldn't you have to scan the keyboard with the I2C the same way you would with the micromite, just using an additional pic? So I'm thinking if this is so, we could just use the micromite to do this and eliminate one chip from the circuit. I will keep digging into the scan method. I found a couple of lines of code doing this with the picaxe, so I will try and port to micromite, shouldn't be too hard. Also, there is an article in the silicon chip mag archives, so I will try to check that out also. I still like AKASHH's idea of implementing micromites ability to be interupted on any pin to start the "which key press" routine without having to constantly poll the keypads lines. Thanks for all your help.
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 11:25am 04 Feb 2014
Copy link to clipboard 
Print this post

  Quote  Wouldn't you have to scan the keyboard with the I2C the same way you would with the micromite, just using an additional pic?


Yes and no. The pic attached to the key pad does all the work i.e. scanning and code production. It is an I2C slave and to access the key pressed information (get a key value), one only needs to issue a single i2c instruction to the i2c bus from say, a MaxiMite and the key pad will respond with the key data.

e.g.

i2c read kpad,0,3,0,key,control


Where kpad is the i2c address of the key pad, key is the key value and control is a handshake byte


David M.
 
     Page 1 of 2    
Print this page
© JAQ Software 2024