Save Pic Pins with a 1 pin analog hex keypad

Author
fredfmah
Super Member
  • Total Posts : 243
  • Reward points : 0
  • Joined: 2003/11/07 12:45:42
  • Location: Madrid Spain
  • Status: offline
2012/12/17 17:57:34 (permalink)
0

Save Pic Pins with a 1 pin analog hex keypad



1 Pin hexkeypad, 
.I did mention that  to save on  eight MCU pins  when using a 4x4 hex keypad, an analogue keypad  can do the same job using only one pin.
  I have dug out my circuit and it is appended.
 
  R18,R19...thru ... R32, R33 are I Meg resistors  representing 16 open circuit switches.    
Now your shop bought  hex keypad will need so art work with a cutter and a soldering iron to implant the  17 x 270ohm resistor chain.

A word on resistor tolerances, which at first sight  means 1% resistors.
However we have two choices:
1.  Step through all the voltages and note voltage input values  in a table in the MCU. Note careful with Vdd variations.
Providing  the readings  do not overlap and even though displaced a little, but still  have a guard band of 0.1V then we can compare the input voltage to the table values .
2. Really when we use typical chip resistors, the unit to unit tolerances are often about 0.1%  because they are all from the same mixture before being baked in the resistor fabrication process..  This is the cheapest way to go.

Also fortunately being in a ladder between +5V, or +3.3V and ground  if the supply is not 5V then  the voltages applied to the ADC i/p port  will still be in the same ratio as when the Vdd was exactly 5.0000000. .-))
You may need to be aware of this when writing your switch software routine, although 1023 levels  are produced at any  MCU supply voltage voltage.

Supply voltage limits.
This 18F2550 data pdf  talks about  Vdd max 7.5V...  sounds dangerous  for the PIC.  I usually have a stabilizer between the semi-smoothed input DC and the output to get no worse than 5V+/- 0.1V.

The table on the diagram shows what voltage you can expect when ,  one at a time,  each  1 Megohm switch  is closed.

Debouncing
The typical  dome membrane  hex keypad bouncing settled ( I measured way back in 2006) settled in less  than 5 msec, but use 10msec to cater for worst case.
You can debounce in software or hardware.
 I've tried both but normally debounce in software. Basically when the input changes you sample for a steady reading, as I said before,  you are steady after about 5ms and can take a voltage reading at 10ms.
If you want to go the hardware route, bearing in mind depending which key is pressed, the resistor equivalent for  t= 5RC will vary. The downside is you need a  parallel capacitor for each of the 17 switches.

I hope someone finds the circuit useful.
Bye
PS. I so soo apologise not only   for wrong thread on forum  but somehow I have duplicated  the post??? Ooops As the bard said. The oft laid plans of mice and men aft gang aglae.

Fred

Attached Image(s)

#1

11 Replies Related Threads

    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2012/12/17 20:04:13 (permalink)
    +2 (1)
    Hmm.  the objective is to get 16 distinct voltages + a nothing pressed state.  I think you don't need to hack the matrix at all.

    This looks promising with a minimum of 18 ADC counts between buttons, a clear change from the not pressed state that can be used to trigger a comparator for a key-down interrupt and only 9 passive components (using E12 resistors) + the keypad.  Debouncing can be a single capacitor.
     

     
    and [here] is the analysis in Excel97 format.   It either needs 1% resistors or at least the highest key press ADC count to be used to scale all the others if using 5% parts. 
    post edited by Ian.M - 2012/12/17 20:10:37

    Attached Image(s)

    #2
    Brick
    Super Member
    • Total Posts : 1684
    • Reward points : 0
    • Joined: 2006/05/25 09:10:40
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2012/12/17 20:45:05 (permalink)
    +2 (1)
    Certainly an interesting topic. Though just to throw this in here, the solution mentioned wouldn't allow you to detect multiple keys pressed at the same time (though this isn't always needed of course)
    #3
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2012/12/17 21:03:45 (permalink)
    +2 (1)
    Variants on this were commonly used for the front panel keys of many models (if not most) of consumer entertainment electronics through the '80's and '90's and yes, they only understood one key press at a time from the resistor ladder.  Any sort of 'shift' key needs to be separate.  At least the ladder solution only returns valid key presses.  If you use binary weighted series resistors for each column and  row,  resistors in parallel makes it VERY difficult to predict what reading you will get for even two simultaneous keys.
     
    Beware of matrix keypads with relatively high impedance carbon contacts or printed resistor links.
    #4
    vtrx
    New Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2013/01/04 17:42:57
    • Location: 0
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/01/25 18:48:57 (permalink)
    0
    I know this circuit but what if the button is pressed very fast, the voltage will be stable enough?
    #5
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/01/26 02:37:57 (permalink)
    0
    No.  You reject any ADC input that does not stabilise at the same level (+/- a few counts) for at least three samples.   That prevents buttons briefly jabbed registering as buttons with a higher output voltage.
    #6
    JorgeF
    Super Member
    • Total Posts : 3210
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/01/28 16:52:27 (permalink)
    0
    Hi
     
    This is quite an interesting academic exercise, but ....
    The extra cost in components, PCB real estate and fabrication will probably be higher than moving to a PIC with a few more pins.
     
     
    Just my 2 cents...
     
    Best regards
    Jorge
     
     
     
    #7
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/01/28 17:14:09 (permalink)
    +2 (1)
    Yes. Adding 1K5 resistors to the BOM is hard to justify.  390R is suitable for most single LED driving applications and 4K7 will do for most pull-ups, so those are probably forgivable, but the extra code complexity will cost as well, so one might as well go to a higher pin count PIC and use simple matrix scanning.   The ONLY significant advantage is that a remote keypad can be connected by only two wires without a local keyboard controller and uses effectively DC signalling so will not radiate significant EMI.   That alone may be highly desirable for security applications.
     
    I am NOT in favour of using low pin count devices in complex circuits requiring I/O expanders or other similar tricks but it does seem to be very popular with students and  hobbyists.   Starting with a debug capable 28 or 40 pin device then if its needed, reducing the pin count after a sucessfull proof-of-concept prototype is likely to be both faster and less stressful. wink
     
    Microchip did us all a great disservice by bundling the low pin count board in the PICkit 2 Starter kit . . . . .
    #8
    kelvinu
    Super Member
    • Total Posts : 263
    • Reward points : 0
    • Joined: 2007/06/16 08:07:32
    • Location: 0
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/01/29 12:53:23 (permalink)
    +2 (1)
    I tend to find many of my projects grow as I develop them and add useful features. I recently did a motor control circuit with a 12f675 which worked great until the power supply was changed from lead acid to LIPO . I then had to add supply voltage monitoring. As this project was to be a small run and I had not finalised the board I was able to swap to the 16f688 with no problems. Where as another recent project with a dspic33XXXX It was a "one off" and half already built and two rotary encoder switches were needed and one i/o left.  I managed it with  a 8:4:2:1 resistor network with a constant current supply into the one input left which fortunately was an analogue in put.
    #9
    fredfmah
    Super Member
    • Total Posts : 243
    • Reward points : 0
    • Joined: 2003/11/07 12:45:42
    • Location: Madrid Spain
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/03/10 11:55:36 (permalink)
    0
    At first sight the matrix offers a component saving. Yet we compare the  matrix analog vs  1 pin ladder analog  hex keypad versions  and find they have important differences , more than  just price.

      Though price was the reason why I built the ladder version  for the first time about  seven years ago.
    Building on a  PCB  was / is  a lot cheaper than buying the  16 key 4 x 4 matix  keypad as a commercial product.
    Price today, RS catalogue  from 35 to 50 euros... not exactly  a poor man's dream.

     Technical stuff.


    The ladder has almost equal voltage increments/decrements   when you press a  key  above  or below your present pressed key .  This makes life a lot easier for the PIC decoding  software.

    Whereas the voltages that correspond to the matrix keys become very cramped at the end of the scale...
    useable but cramped.

    The matrix of course has the little problem  called ghosting. But with a  4 x 4 matrix that has a diode across each switch junction then  you lay your ghost to rest.
    That is internally we have added  16 diodes, well we can  try to add,  accessing  those points with a good cutter or maybe we find it is impossible.
    Aha you cry, but you haven't said what happens to the ladder ghost !!

    Well the ladder has  the nice  property that the highest key pressed doesn't allow current to  get to resistors lower down the ladder.
    Not Clear?   We can press 9, 7 and 5  ( using three fingers ) The value shown on our display
    has to be ???? 
    Yes, it is 9 !!! (Well   maybe   the equivalent of key 8.8. But our software hits that on the head as impossible and gives you a single  9 value.)
    So no ghosts.  No diodes,   no commercial product   and  component wise  a lot cheaper.

    Just before I drop out of the exchange of ideas, there is also the little matter of  contact bounce.

    Most manufacturers are coy on giving exact figures  but when you close a switch, a GOOD switch we can safely assume with 2013 products that the bouncing is over  in about 10ms  We then can take a reading of  the voltage that corresponds to pin 3

    The example hex matrix offers a  an RC  390 ohm 10nF .   Taking the end voltage as 5*RC  we get  5 * 3.9e-6 about 20us:
     So obviously  the  RC  LPF does nothing  to debounce.

     Solution1
     increase the R by a factor of a 1000 to 390 kohm. Theoretically yes, as we now have a time of 20ms.But as the PIC  analog input is looking for a source resistance of < 2000 ohm it won't work   by not correctly mating with the pin input charge RC network.

     Solution2   
    increase C by 1000, C then is 10uF.  Not quite sure how the PIC will react to that either. My gut feeling is, MPUs don't like large capacitive loads on pins in general.

    Solution 3. Debounce in software.
    This is a good general solution for when you are using a matrix with unknown  contact bounce times using the infamous cheapo switches: You can play around anticipating the worst and start using a constant input after say 300ms. .
    You read the analog pin i/p , for example every 5ms ... before fine tuning to what cheapo  offers.
    I'm not knocking cheapo, just advising ' buyer beware'
    We also mustn't forget the other side of closing a switch, when we are done   we remove our finger we let it open ... all by itself.
    This can be real fun.  Some measured values   with cheapo can be as long as a second, dare I say longer?

    To look at some gaphs try
    http://softsolder.com/201...apacitors-dont-fix-it/

    There is also  the "too, too big C"   stored joules energy problem, which first time round shows as failures  because    you are  eventually  soldering the switch contacts together, I say  eventually.

     Solution 4
    Use a good quality switch.

    We now know what we can expect  from a switch and here paying about 50 cents  a switch offers a minimal guarantee  of:-

    • bounce to close and
    • bounce to open times symmetry.

     Then we can use an RC circuit  to clean  up the rise as well as the fall times.  Some values ?:
    I have used  when battery life is important about 100k ohm which at worst  is taking about 0.05mA from the power supply.
    To get to the 20msec fully steady state  value  this means a C of 40nF:
    So RC = 100k , 39nF or nearest.

    Just a cautionary word on   real world scenarios
      When and  how you read the input.
    1. Don't take the sample until you are sure the input signal has stopped bouncing. If you are not sure go to a software debounce routine.
    2.  100k is for almost  noise free system environments.
    If you are using motors in your project, there can be some very large voltages floating around on your wiring .
    These may wel,l  and will, falsify your input reading.

    To get the best of all worlds, good quality switch, biggish capacitor ( with a good low  esr )  say sample through 3k3 providing  noise immunity and  for our  20ms steady debounced voltage we now require a C  of about  1uF
    Checking :-  RC = 3300 * 1 e-6 = 3. 3ms  which  x5 to 99% = 16ms ( near enough )
    To get round loading on the  resistive ladder and sourcing to the analog input pin  matching, we can use a suitable   single supply opamp configured as a voltage follower.
    Here microchip offer their  MCP601 made for the impedance matching role.
    Also we have a slow application, we operate at unity gain so input output tracking is guaranteed.  ( famous last words )

    I hope I have clarified the possible issues when using  a  1 pin analogue keypad.

    Bye
    Fred
    #10
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2013/03/11 02:48:32 (permalink)
    0
    However you haven't clarified why one would *WANT* to use a 1 pin analog keyboard.  With the cost of other solutions like the 17 capacitive touch channel PIC16F1512 as low as $1.12 (qty 1, or $0.81 qty 5000), and the cost of a reasonable quality keypad being as high as it is, the whole concept has a very limited application niche.   Adding a lot of extra components or hacking up an existing matrix is very hard to justify, even for a for a one-off.
     
    A couple of small corrections:   If a capacitor of greater than 2(N+1)CHold (where N is the no. of ADC bits) is added at the ADC input, it is easily possible to show that the maximum voltage change during sampling will be less than 1/2 LSB. .
    The older PIC16 devices use a 120pF  CHold (sampling cap) and 330nF at the ADC input will allow correct operation even with very high source impedances. Newer PICs generally have smaller CHold
     
    The limiting factor for source resistance is then the total leakage current which must remain less than VDD/2(N+1)RSource.
     
    However C1 in my original proposal, is not there for debouncing, although it and R8 can be increased in value to provide debouncing.  It is there to improve the circuit's resistance to EMI.   I believe that with seventeen discrete input levels, debouncing is best done in software.  
     
    N.B. R8 is essential if C1 is greater than about 100pF to prevent contact welding of the one button in the matrix that does NOT have any resistors in series, and to prevent very high transient discharge current, which can reset or even destroy (by latchup) a PIC.

    post edited by Ian.M - 2013/03/11 02:49:48
    #11
    Jander Clerix
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2014/04/25 08:25:01
    • Location: 0
    • Status: offline
    Re:Save Pic Pins with a 1 pin analog hex keypad 2014/04/25 08:44:58 (permalink)
    0
    Ian.M
    Hmm.  the objective is to get 16 distinct voltages + a nothing pressed state.  I think you don't need to hack the matrix at all.

    This looks promising with a minimum of 18 ADC counts between buttons, a clear change from the not pressed state that can be used to trigger a comparator for a key-down interrupt and only 9 passive components (using E12 resistors) + the keypad.  Debouncing can be a single capacitor.



    and is the analysis in Excel97 format.   It either needs 1% resistors or at least the highest key press ADC count to be used to scale all the others if using 5% parts. 



     
    Can someone maybe send me the Arduino c program for this specific scheme?
     
    Thankyou
    #12
    Jump to:
    © 2018 APG vNext Commercial Version 4.5