Would caps be helpful with analog keypad?

Hello,

I'm working with this project for quite time, and I'm having issues with the fluctuating of the analog readings, as the writer of the code invested the code with debounce and threshold values.

So, I thought of applying ceramic caps to each pin of the keypad. Would that be helpful?

Here are the picture and code of the project.

Regards,

AKP.h (899 Bytes)

AKP.cpp (1.84 KB)

AKP.ino (851 Bytes)

I don't think that's your problem. That network seems to be poorly designed (unless there is some other design criteria you haven't shared), reading 16-button keypad with the resistor trick is... not hard, per se, but you start to have to be careful, especially with the ~50 ohm resistance in the buttons when pressed (and it varies a bit button to button).

Draw out the equivilent resistor network with each button pressed, and use Ohm's Law to figure out what the expected voltage on the analog pin would be. The steps are too small, largely due to that 10k resistor which confines the values to the upper 1/3rd or so of the voltage range.

I think you want to Remove the 10k resistor, in it's place, put a 220 ohm resistor to the right-most keypad wire. Now the the resistor divider will be 0~4k on one side, and 220~880 on the other. You should be able to see the steps much more clearly now (you'll of course have to adjust the code to the new values), but this should work about three times better. Whether that's good enough, I can't warranty. There may be a better scheme for choosing the resistor values that will spread the values out more evenly, particularly in light of the extra variation from the resistance of the button - I don't care enough to meditate on that, but you should - see how much the resistance of the buttons actually is, and make your code as tolerant as possible of the button resistance increasing as it ages.

If jitter is still a problem, you might have some success with a small cap on the analog pin (to ground). The problem is that doing this results in an RC circuit - you can calculate exactly how long it would take to change states, though - and assess what values of capacitor would be acceptable.

I strongly discourage using "analogue keypad" tricks as this is - in my experience - a particularly common point of failure in the control buttons of video monitors, MP3 players, tablet computers, navigators and such. The problem relates to moisture or other degradation of the lubricant used in the push switches - and contact degradation. Any spurious resistance - either lower or higher - in a contact "ciphers" it to read an incorrect pin press.

While it may seem that a capacitor will help to stabilise a voltage reading, as the capacitor charges or discharges, the ADC will read a different value which - not surprisingly - may be read as an entirely different key! You need software de-bouncing, reading the ADC repeatedly and only considering it valid when many readings within a reasonable period (5 to 10 ms) are (all) in the same threshold band.

And you need to calibrate the code for a given hardware assembly. Write a test sketch that feeds out the ADC values for each key, and then adjust the sketch to make the individual decision thresholds halfway between the measured readings. Do not presume a linear progression.

Yes, first thank you all for support and the wonderful instructions for my work. I feel so cool when I get back from work and see replies to my post.

What I did to calibrate the code which is a work of a smart guy posted this trick on YouTube and his webpage. And I found it very smart to apply only one pin !! That's why I'm very interested for his work.

I put 1uF electrolytic cap on the analog pin. And I think there maybe an improvement.

I also recorded new voltage readings with "ReadAnalogVoltage" example in Arduino IDE, and calibrated the code to get 10-bit ADC readings.

And, I don't get so far when each reading gets interfered with the other one, where I'm watching the values with -+threshold value are not conflicting with another reading range.

Even so, I get interfered readings.

So, I thought if I put ceramic caps on each pin of the keypad, would that work?

Hello,

It's working now :slight_smile:

I added another while loop which is a technique learned it from a very good book of Atmel microcontrollers.

The name of the book "Embedded C Programming and the Atmel AVR, 2nd Edition"

AKP.cpp (1.84 KB)

AKP.ino (1.15 KB)

I was just messing with the keypad today using analogue input, but the main issue with those cheap keypads is that the keypad itself has rather wide resistance on the buttons depending how hard you press. Even if you design your voltage divider very carefully with enough spaces, a light touch will register wrong key from time to time especially in the end. Not to mention that with changes of temperature, moisture and time the whole thing may go out of whack.

I've made some reliable code , but I think for a real-life situation (outside my home) I would never use it. This is as potentially unreliable long term as it is weather dependent.....
In most cases keyboard library just need to be a trigger message on the key-down with optional pulsating repeat but many people design this as a doorbell button in a loop and that of course brings issues with messy code.

Oscarko:
I was just messing with the keypad today using analogue input, but the main issue with those cheap keypads is that the keypad itself has rather wide resistance on the buttons depending how hard you press. Even if you design your voltage divider very carefully with enough spaces, a light touch will register wrong key from time to time. Not to mention that with changes of temperature and time the whole thing may go out of whack.
Also the bounce in is usually not that big deal with forceful press, but I found out the release the keypads with rubber contacts will go through a rather wide cascading resistance until it is disconnected, so you will need to pay attention to that in your code. I've made some nice code to work pretty reliably, but I think for a real-life situation I would never-ever use it. This is as unreliable long term as it can be.

I agree. I considered using this method some time ago, but after some testing I came to the same conclusion, so ended up making an I2C keypad instead, using an ATtiny84 to do all of the hard work using the "Keypad" library, then communicating with the main Arduino using I2C. Much more dependable without wasting more Arduino pins than were necessary. Not a whole lot bigger than doing it with just resistors, either.

OldSteve:
I agree. I considered using this method some time ago, but after some testing I came to the same conclusion, so ended up making an I2C keypad instead, using an ATtiny84 to do all of the hard work using the "Keypad" library, then communicating with the main Arduino using I2C. Much more dependable without wasting more Arduino pins than were necessary. Not a whole lot bigger than doing it with just resistors, either.

Totally agree.

With the price of a clone Nano being just $2-3 it makes sense to build a keypad module and treat it as a black box and let your real Arduino (possibly also a $2 clone!) get on with things. Even matrix-style keypads use an inordinate amount of pins. OK for a MEGA perhaps but then again look at the cost of a MEGA vs a Nano!

Just as an aside to anyone else reading this, Adafruit make several capacitive touch detectors (I2C, SPI and probably telepathy too) that also lets the keypad become a black box - it will be subject of my next video. I'm using a CAP1188 but there are several others in that vein.

Ralph_S_Bacon:
Just as an aside to anyone else reading this, Adafruit make several capacitive touch detectors (I2C, SPI and probably telepathy too) that also lets the keypad become a black box - it will be subject of my next video. I'm using a CAP1188 but there are several others in that vein.

Sounds interesting, and looks pretty easy to use. I might get a couple and have a play sometime.

This is my 'black box':-

@OldSteve I'm using the Sparkfun CAP1188 breakout board just for convenience but with your skill at creating PCBs (that is one of yours?) I guess you won't need that. It's pretty simple anyway if you use I2C.

Ralph_S_Bacon:
@OldSteve I'm using the Sparkfun CAP1188 breakout board just for convenience but with your skill at creating PCBs (that is one of yours?) I guess you won't need that. It's pretty simple anyway if you use I2C.

Those CAP1188s look good for when you don't need 3x4 or 4x4 keys. Also, they'd be good if the key arrangement of a keypad isn't suitable. I noticed that adafruit also sell latching ones. I'll grab a couple of each type to have a play just in case I need them in future. Can't ever have too many toys. :wink:

Yeah, that's one of my DIY boards. It's good for prototyping, then when I make a final project PCB, I can just put an ATtiny84 on the board wherever I can fit it, and mount the actual keypad on the project's case as usual. Then I hardwire the ATtiny to the main '328P or whatever and mount a header for the off-board keypad connections.

I enjoy making my own boards, especially now that I mostly only make one or two boards at a time for hobby purposes. Almost always single-sided, and I always space things out a bit and never use SMD components because the old eyes ain't what they used to be. :smiley:

I like that OldSteve very much. I am not familiar with ATtiny84, is your "black box" on the pic made with ATtiny84 as well?
It would be great if you could describe a bit your project or point to some links. It looks very simple few resistors, capacitor and the IC. I assume you need to program the ATtiny84 through programmer (or use arduino as programmer?)
I make my own quick and dirty pcb on my wood carving router. It is not precise enough to do the nice shaped copper tracks so I do a "negative" or voronoi isolation PCB - use everything as a plate and just let it route single lines between them and mark holes.
It takes about 2 min on my router to do this type of PCB. It is fine for low frequency stuff.

Oscarko:
I like that OldSteve very much. I am not familiar with ATtiny84, is your "black box" on the pic made with ATtiny84 as well?

Yes. It's an ATtiny84. It had just the right number of pins for this project. It's a 14-pin chip, but 2 pins are used for power and 1 for RST, leaving 11 pins. (I used the internal 8MHz oscillator, so didn't need a crystal or resonator). 8 pins go to the keypad, 2 are used for I2C, and the remaining free pin was used for a LED which flashes for 100mS to indicate a keypress.
The firmware is based on the "Keypad" library to do all of the hard work, then I used the "TinyWireS" library for I2C comunication. I didn't support all of the functions of the keypad library, just the basics, so coding was very simple.
Usually, I use an UNO for the main micro when prototyping, with the I2C keypad connected, then switch to an ATMega328P and ATtiny84 on a custom PCB for the finished project.

It would be great if you could describe a bit your project or point to some links. It looks very simple few resistors, capacitor and the IC. I assume you need to program the ATtiny84 through programmer (or use arduino as programmer?)

Sorry, I don't have any links, I didn't publish the project, but I think I'm describing it reasonably well. I use a USBASP ISP programmer to burn a program to the ATtiny84 chips.
This is the schematic diagram:-
I2C Keypad Schematic.JPG

I make my own quick and dirty pcb on my wood carving router. It is not precise enough to do the nice shaped copper tracks so I do a "negative" or voronoi isolation PCB - use everything as a plate and just let it route single lines between them and mark holes.
It takes about 2 min on my router to do this type of PCB. It is fine for low frequency stuff.

That doesn't look too bad at all. I have a Dremel with router table and plunge router attachment myself, but have never tried it for PCB-making.
Your PCB is nice and neat, and if it does the job well, there's absolutely nothing wrong with doing it that way.

I use (very old) PCB layout software, (Protel Advanced PCB), then print a transparency. I use pre-sensitised PCB blanks with a positive-acting photo-resist already on them, (UV tubes to expose), then etch my boards using ferric chloride. It's a bit of an involved process and takes a fair bit longer than your method, but yields good results and once setup, it's a trivial matter to make a number of boards.

This is a bottom view of the I2C Keypad board:-

Thanks OldSteve, I think I've got the idea. I spent some time browsing web and those ATTiny are truly something, and the smaller 85 is amazing on its own.
I think I will try first to do the keypad with i2C expanders as digikey sells them dirt cheap and I don't have yet any USBASP ISP (any recommendation?).

I bought my first arduino last week, although I had been messing with electronics my whole youth and programming hardcore c++ for last two decades. The only thing I don't understand is why I was waiting so long. It is so fun! Oh, the arduino core libraries are so lean and tiny in comparison to what I am used to, it is like a "light reading".

I do the PCB on my cnc router, but it is a woodworking router with V carving bit run by Vectric aspire, not the most typical PCB prototyping setup, but doing the isolation cuts instead of normal lines proved to be a huge, and I mean huge time saver. Literally 2 minutes and simple PCB is done and no more pulling wires from breadboard. I remember etching PCB's this way as well when I was young, oh I would even use carving tool to do it manually. Good old times...

Oscarko:
I don't have yet any USBASP ISP (any recommendation?).

eBay has USBASP programmers very cheap. Mine cost AUD$2.55 each. They have a 10-pin socket on the cable, but if you need one a 10-pin to 6-pin adaptor is also very cheap, about AUD$1.00.

These are the USBASP V2.0 programmers that I use:- 2014 USBASP USBISP AVR Programmer Adapter 10 Pin Cable

Here's a link to a 10-pin to 6-pin adaptor, (AUD$1.00):- 1pc 10 Pin Convert to 6 Pin Adapter Board For ATMEL AVRISP USBASP

Oscarko:
I don't have yet any USBASP ISP (any recommendation?).

Use your Arduino.

[quote author=Coding Badly link=msg=2720165 date=1461136144]
Use your Arduino.[/quote]
I should have mentioned that, shouldn't I? :blush:
A stand-alone ISP programmer is much more convenient though, and a little faster to get set up.

OldSteve:
A stand-alone ISP programmer is much more convenient though, and a little faster to get set up.

I have found a Teensy on a breadboard to be the best choice for ATtiny work.

[quote author=Coding Badly link=msg=2720237 date=1461140973]
I have found a Teensy on a breadboard to be the best choice for ATtiny work.[/quote]
They don't look bad. Considerably dearer than a plain old tiny84 or tiny85, about $20 delivered for a Teensy2.0, but they do look good and are a bit more gutsy.
Still, considering the price, I'll stick with tiny84 and tiny85 chips for my extra-small stuff. I have got some Pro Minis coming, to try out, but I have to wait another week or two.

OldSteve:
eBay has USBASP programmers very cheap. Mine cost AUD$2.55 each. They have a 10-pin socket on the cable, but if you need one a 10-pin to 6-pin adaptor is also very cheap, about AUD$1.00.

Alternatively, if you have already supplied yourself with some "Dupont" female to female ribbon wire plus the 2-by-3 and possibly 2-by-5 (but 2 by 2 is more general; glue them together to make the 2 by 5) shells in a 100 pack, then you can make up the cable.

Why would you have these? Well, having the linear 2-way, 3-way, 4-way and 5-way shells to use on the various ribbon pieces available allows neat cables for I2C (4-way), SPI (5-way) and servo/ sensor (3-way) modules as well as simple 2-way power jumpers.

So some super glue and 3-way makes a 2 by 3, similarly 2 by 5.