Thanks for the comment Andreas, but I could not take credit for the idea. I stumbled into the circuit in Google Images while researching keypads. www.microchip.com/forums/download.axd?file=0;694632&where=&f=1pin%20keymatrix.gif
4:14 wait you said the more resistors the higher the voltage but wouldn't it be the opposite? , the more resistors you put in series the more the voltage should drop between VCC 5v and the GND pin not increase, this makes no sense
After days messing about trying to get this to work I had all but given up. The problem was the voltage out numbers I was getting were nothing like the progression you were demonstrating in your video, they were all over the place. On top of that they kept fluctuating over time so I couldn't program the threshold values with any certainty. Then I had a spare keypad delivered so I thought I would try that as a last resort - SORTED!! The voltage numbers were spot-on and gave consistant results. Anyone else having trouble with this should try different keypads to see what results they get. The first keypad was brand new as well by the way. Thanks for a very useful video Hari.
Grrr... sorry to hear about all the problems. I probably should have included a step to test just the keypad membrane switches themselves. Glad you persevered and got it figured out! Good job!
Cool. It's a very old technique that was (and probably still is) widely used with small passive controllers for professional appliances such as the Alesis LRC which was a tiny controller for the Alesis Adat digital recorders; it has 13 buttons and terminates on a TS jack; I made a PIC18 project to read this controller and use it as a generic MIDI controller; the downside is that you have to apply a debouncing algorithm with a rather long debounce time in order to get a stable voltage before trying to recognize which button was depressed.
zioguido , i didnt know that it is good enough for commercial use. i thought it would not be reliable enough. do you have photos or videos of your hack? I'd love to swe it.
Yes, it's quite a reliable technique as long as the resistor network is done with high precision resistors, the rest is done by the software. The only remark I feel to make based on my experience is that you need to implement a rather long debouncing algorithm, say 200 mS, and it's even better if you have an high resolution ADC, 10 bit should be enough. No, I have no pictures of my project, but it must be somewhere down my basement :) it was only a part of a more complex control system that I made for my old recording studio and no longer use.
The solution using an i2c is hand waving magic that I don't really understand. I like to understand what my circuits are doing and I do know Ohm and Kirchhoff so this is fully comprehensible - thank you for this ingenious solution.
Thank you, this is so complicated, if you don't know how, but as soon as someone pushes your nose on that or you realise it yourself, it is so amazingly simple and logic, I mean the analog pin reads voltage, so what is more logic than adress each button with a different voltage? And as soo you got it, you just think "why I didn't had the idea myself or why didn't it came earlier to me, grrrr, D'oh?", I'm using this already simply with the oneWireKeypad.h library, because it makes it all very simple and most important it isn't using any delays, but still interesting to watch, the way you explain it, the exited voice shows me that you are still amazed about how simple it actually is.
Thanks Călin ! I appreciate the encouraging words. The custom PCB has arrived, but I've been too busy to solder the parts. Hopefully I will have time to do some soldering this weekend. :-)
based on watching this video i pulled apart an old microwave and have made a 15 button keypad. And all it cost was a few resistors. top video and explaination
Yeah, I'm already using a ton of shift registers for the 9x9 Seven-Segment displays, so I wanted to keep the input as simple as possible. Zero pin is not an option, so I had to settle for one pin. lol :-)
Hi Eric! Appreciate the comment. Thank you! I really need to get a better mic. I have to be a few Angstroms from my Blue ICE mic for it to have decent volume, so I often use the mic on my Samsung S7 camera, but of course it sounds different. Grrr. Do you have a lav mic recommendation? Do you use a separate audio recorder?
Hari Wiguna I use a super cheap battery powered lav from Amazon into my H4N recorder. I post process it in Audactiy (There's a whole video on my channel) Cheers Sir
Great explanation of the keypad. Liking your videos and just subscribed to watch more. Like the logical breakdown from start to finish of writing the code and understanding just what is going on. Better than just "copy paste" into Arduino without learning anything, Now I need to buy one of those pads... for no reason other than to tinker! I'm from a PHP background, and still got a lot to learn with Arduino and C... seeing that a 'char' and an 'int' look like arrays is baffling to me!! Steve
Thanks for the comment Stephen! Keys in Arduino arrays are always numeric, but the values could be any type, ie: an array of ints or an array of chars. If you enjoy coding, I think you'd enjoy Arduino. It's like coding with the bonus of physical input outputs (beyond keyboard and screen). Cheers!
Nice work.. I was wondering if I could use this to reverse engineer a keypad from a rack module. Basically my idea is to mimic the key preses in the rack via arduino. So in a first stage I would use the voltage divider to determine the values for each key press as input and then, based on the obtained values, use the analogwrite function to "send" the same key press via arduino. Do you think that is possible? Thanks
Thanks. Unfortunately, the analogwrite function does not actually outputs a variable voltage. Despite having the word "analog" in the command, it actually pulses 5V at varying speed. Also, as far as I know, most commercial keyboards do not use this analog method but instead uses a digital matrix (more pins, but more accurate). www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
I just tried this out. Very neat. I am having a small issue that I was hoping you might have insight into. The wobble/slop on my key values is huge +/- 100. Any idea what would cause so much slop on a key press?
Most likely your keypad uses switches that has high resistance (around 100 ohms as you noticed). The one I use has practically zero ohm resistance when the switch is pressed. All resistance comes from resistors, not the switch pads.
If random noise is added to the output to the 4x4 keypad outputs this method has a failure rate ! noise measurement: 0.01% rms of 5Vdc =5/10,000 v will cause a miniscule error say 1 error / 10^8 keypad outputs . Theoretically: a 5/10,000 v rms includes a few -5v pulses witihin a group of 10^8 noise intervals where 10^8 = 100.000,000 random pulses
6.8nF should work. As I mentioned in the video, the 390 ohm resistor and 0.01uF cap debounces the often noisy switch. You could probably even eliminate the capacitor if your switch is not too noisy and/or you added code to do debouncing in code.
Sir how can I convert the values of keypad[i] to 'int' for me to save it to eeprom. By the way I'm making a lcd menu and I want to retain the values that I pressed even if I'm in another screen.
Int x= (keypad[i]); is OK? (When is by Start defined x (int x;), then is good only x= (keypad[i]); And in x is value of keypad[i]. I think that value can by in EPROM written direcktly, try it: EEPROM.write(x, (keypad [i])); x is Number for byte in Eprom.
I saw this same technique used in an old VCR front panel. Did you know that it can even be done with a digital I/O pin and two additional passive components rather than using an analog input?
@@HariWiguna Yes using an RC circuit with the resistor being the button voltage divider. One end of the RC is Vcc, the other end is connected to the digital pin. Discharge the cap by setting pin to output low. Now set pin to input and time how long it takes to turn on by reaching (Vcc/2). That time is proportional to the resistance value. Then repeat the process.
Thanks Scott! It was a tough finishing the shoot and edit of this video because the PCB arrived from China while I was working on this video. I couldn't wait to try the board! I hope I didn't make any mistakes in the layout...
Video was great . and now i cant wait to see the new board . Like wise , having my printer so close to where i can start dry runs and testing . heck im so excited but trying to slow and methodical as needed so as not to break stuff that i would have to re order. must resist the urge to just mash it together . LOL
Hi hari wiguna help please,I am making my keyboard with spst buttons and If I make several keyboards like it ,will it have same analog values? Like you had 1/2 for number 1 ?
HI Aryan, The analog values depend on the resistors you use. If you get the resistors from the same batch and you adjust the slop in the code to be as large as possible then it should work. But you will have to check your analog values yourself. The chance of my analog values be perfect with your resistors is small.
I make a mistake, because I'm french and not completly bilingual. I should have written less inputs instead of " without using less inputs.". So what I need it is 'digitalRead' individually 4 BCD thumbwheel switches without using 16 inputs. I tried to imagine a matrix like the 4X4 keypad but it does not works because but some rows or columns may have many binary values at the same time.
Ah... I see. That's a cool idea actually. I found this article: www.instructables.com/id/A-simple-4-channel-adjustable-current-source/?ALLSTEPS They're using it to control current, but I think by adding one resistor to positive, then you'll have a voltage divider and be able to read the combined resistors using one Arduino pin just like what I did with the keypad. Keep me updated. I think this is interesting. Thx for the comment.
In theory it should work. In fact simulating the circuit with Spice works well. In practice, the voltage difference between the buttons down to the right is so small that a slight fluctuation in the supply voltage is enough to cause errors in the detection of the pressed keys. I did a lot of tests and in practice I was never able to run the detection well, pressing the D button gave me a #, and pressing the C key would tell me 9. :(
I guess I was just luckier?! I presume you calibrated your threshold arrays instead of just using my values. Also, by watching actual value fluctuations while gathering the values for the array, I was able to guess how much slop is necessary to give the maximum range for each button while not running into the "nearby" buttons. Thanks for sharing your findings Salvatore!
That's a fantastic idea! The ESP does not have many I/O pins, and one Analog pin. I used this calculator: www.raltron.com/cust/tools/voltage_divider.asp I put in 3.3V as the input supply, leave the top resistor blank, retained the 3x390 and 3x1.5K bottom resistors, 1V for the desired output, and it came up with 13041 for the top resistor. So 13K resistor instead of 4.7K for the top resistor. Let me know how it turns out in real life :-)
what will be the values if I used 3.3 V input under ESP32 mcu? int thresholds[16] = {2, 77, 144, 202, 244, 290, 331, 368, 394, 434, 454, 477, 496, 518, 538, 556};
At 3:15 in the video, I shared a short sketch that I used to find those values. Just do the same for your 3.3V circuit. Hmm, I just remembered that the old ESP8266 only measures up to 1V. I'm not sure if the ESP32 suffers from the same limitation. If it does, you would need to adjust the top resistor such that the maximum output of the ladder is 1V instead of 3.3V.
I've built a couple of projects using similar types of matrix keypads. I think you have a great design that I'm going to experiment with, I do have one question having to do with electrostatic discharge. One of my designs had a 4 X 4 matrix keypad that was mounted on top of a metal enclosure that contained all of the electronics . I originally did not have any ESD current limit resistors or RC circuits to help isolate the inputs on the micro-controller from ESD. I ended up having the several micro-controllers fried by static discharge coming from people that came in contact with the keypad and discharged it into the electronics by depressing keys on the keypad. What do you think could be done to your design to prevent this from occurring?
I tried duplicating this in tinkercad and it isnt outputting anything. I copied the code too and thought my wires are correct. only thought could be wrong orientation of the resistors?
I wasn't aware that TinkerCad has an Arduino simulator, so I wired up the circuit. Everything works as I expected except for the first column (1,4,7,*) all acts as if I'm pressing *. When no buttons are pressed it should return 1023 too. I wonder if the keypad component has a bug in it. I'll play more with this later and let you know if I discover anything else. Anyway, thanks for the question. www.tinkercad.com/things/5YWqan9lh8H-neat-esboo/editel?sharecode=o6MB5FGBJq0Q0EjU1emoxm19kzj5LjFq31JzCpDEB50
Curious... is it possible with this method to read multiple simultaneous buttons? Can I get a unique voltage for any combination of multiple button presses? Or it is only for reading one button at a time? Thanks.
No, this trick would NOT work if you press more than one button :-( The resistors have been chosen such that pressing ONE button would generate unique resistor ladder. Pressing multiple buttons would very likely end up generating non-unique resistor ladder, hence two different set of buttons ending up with very similar outputs.
The "for" loop changes the value of i from 0, to 1, 2, 3, etc till 15. The IF statement on line 19 uses "i" to access the threshold values. First it would check against thresholds[0], then threshold[1], etc. If the difference between the real analog value and the threshold is close enough (within 5), THEN it would do the code between lines 21 and 25. Otherwise, the "for" loop tries the next "i" which would test the next threshold[i]. I hope that helps.
I have some questions in mind. 1)Can we use nay resistor values? 2)I am not able to see properly on how you connected the circuit on the breadboard.I am little bit weak on connecting circuit at breadboard.Please help me.
Hello Jeet, 1. You can use other resistor values, but their ratios are critical for it to work. Remember what we're doing is building a voltage divider with variable resistance on the bottom resistor controlled by which button is pressed. The maximum total resistance of the bottom resistor should be approximately the same of the top resistor. As you press the different columns, the resistance value should change gradually, as you press different rows, the resistance value should change in larger increment so we would be able to tell the difference between rows and columns. 2. In the video, when I discuss the schematic diagram, the rows and columns are clearly marked. If you're not yet familiar with how breadboard is wired internally, this video might help: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-6WReFkfrUIk.html I hope that helps.
I agree! However, I cannot take credit for the idea. :-( I stumbled into the circuit in Google Images while researching keypads. www.microchip.com/forums/download.axd?file=0;694632&where=&f=1pin%20keymatrix.gif
There are many ways. I would use two integer variables: temp and result. Upon detecting a digit keypress do this: temp = temp*10 + k where k is the value of that key. When the "enter" key is pressed then copy temp to result and clear temp. We probably also want to clear temp when it becomes larger than 999 such is the case when they tap four or more digits before hitting "enter".
very useful, thank you very much. But th'ere's one thing that i still don't understand. Why do you put a capacitor just befor the A0 pin (between the vcc, and the A0 pin) and why do you connect it to the ground.
Thanks for the comment Ian! The capacitor is actually not at vcc, it is on the output of the voltage regulator. Its purpose is to smooth out the noisy keypad switches.
The capacitor is being used as a hardware implementation of a debounce function. Write a piece of code that constantly displays whether the button is pressed or not. Press the button and watch what gets displayed. It won't just go from pressed to non-pressed, it will bounce around a bit when it changes state. The capacitor helps even this out. If you put too large of a capacitor in it though, it will take longer for the button presses to be recognized.
Nice video ! Would it be possible to incorporate servos into the code to perform certain actions ? I.E. if button '5' is pressed, only 3 out the 5 servos will turn to a position. I'm still fairly new to Arduino and coding so my skills aren't great, thanks !
Thanks Sanchees3! Yes, you certainly can do that. While you cannot use this code directly (it is tied to the whole Sudoku code) you might learn from it while writing your version. Once you are able to detect a key, then what you trigger with that key could be anything. Let me know if you have specific questions, I'd be happy to help. github.com/hwiguna/HariFun_Mega_Sudoku/blob/master/Seven_Segment_Sudoku/e_Keypad.h
Hari SUBSCRIBED! I have some programming experience and new to Arduino. I want to do this project and I understand the general concept but I don't have an electronics background so there's a few things I don't understand. In your drawing: 1. Is R1-R12 just you counting the number of resistors needed in the project or does the number have other meaning? 2. You are using just three different resistors 390 ohms, 1500 ohms, and 4700 ohms I think? 3. As I am color blind I can't tell from the video, what resistor types (carbon film vs metal film etc.) and tolerances do you recommend? 4. Similarly, C1 is a capacitor? What type and value do you recommend? Thanks a bunch for this great video!
Well, I never got a response so I just used metal film resistors and a 103 capacitor (I don't recall the type). Anyway, I just tried it on a 1 column by 7 rows of momentary mechanical push buttons mounted to a plastic plate and hand soldered together. I went with 220 ohm resistors since I had more rows. I will add two more columns as soon as I get more resistors in. Thanks Hari this is really cool and explained well. And should work great for my flight simulator button box which will have a couple dozen buttons, switches, and rotary encoders.
I finally hooked up the keypad as you described. But all my threshold values are totally opposite of yours. The ('1') has 562 instead of 2 and my (''D') has the value of 2 instead of your 556. But the reading of all the keys are accurate. Can you explain this funny result? By the way do you have an idea to interface 4 BCD thumbwheels switches (16 inputs) to Arduino without using less inputs. Thank you
Sounds like you have the columns wired in reverse. It does not matter, just as long as the values are unique. Just make sure the two arrays are in sync. As far as the BCD switches, if you're willing to use 4 pins for each digit, it's pretty straightforward. Something like this should work (untested code ): pinMode(d0b0,INPUT_PULLUP); pinMode(d0b1,INPUT_PULLUP); pinMode(d0b2,INPUT_PULLUP); pinMode(d0b3,INPUT_PULLUP); d0Value = 15 - digitalRead(d0b0) + digitalRead(d0b1)*2 + digitalRead(d0b2)*4 + digitalRead(d0b3)*8; Do the above three more times for the other digits.
i am making a project that involves a tft display and an bunch of buttons which i want to use for a combination. if the combination is correct the tft display will show some photos, but the number of pins that i have free is only one, the analog pin A5. that pin i wanted to use for my project but i cannot find any code that make my buttons like switches
You have the right idea. Using the resistor ladder as shown in the video would be a way to read a grid of switches using one analog pin. You said it recognized the first button, so that meant you have the correct A5 pin wired and configured in the sketch. Next is to check the wiring and parts. Make sure the resistance values are as close as I mention in the video. Sum of the three smaller value resistors (390) must be less than the large value resistor (1.5K). You cannot use just any resistor. And finally, you need to read the value for each button and put it in the array as shown in the video. This is because any variation in the resistors would affect the analog value reading. Hey, I just thought of another possible issue. Have you measure the resistance of the grid switch itself? Maybe yours have a high resistance? This circuit would only work if the switches are basically zero ohm when pressed. Otherwise, it would interfere with the resistor ladder. :-(
Well sort off i connected every button with 390 ohm resistors. i used a count integer for the button press and a memory integer for the sum of the analog value like a condition for showing the photos. My only question is how i can make an else condition for showing random sentences like: "You are wrong", or "try it again" to show it in the tft display. Thank you for your time.
While it is be possible to compute those values, I measured them one at a time using the Arduino. At about minute 3:16 I showed how I obtained a few of the analog values. I just repeat that procedure for all 16 values.
So I guess it would not behave well with multiple keypresses in a keyboard application. By the way, what program did you use for creating those schematics? Thanks.
Hi Jonas, You are correct, pressing multiple key presses probably generate non-unique voltages or too small a change to be detected reliably. :-( I used EasyEDA.com to draw the schematic, use its export to SVG feature, then magnified the SVG at 150% to fill the page.
Hari Wiguna Good thing you have that while loop that makes prevents that from happening. Currently working on a 40 key bluetooth mechanical keyboard, where I'm using 14 I/O pins, and was at first a little blown away by how much you achieved with just one analog input.
Funny you should ask that :-) Take a look at my loop() github.com/hwiguna/HariFun_Mega_Sudoku/blob/master/Seven_Segment_Sudoku/z_Loop.h Unfortunately, I don't think we could trigger an interrupt off analog pin - correct me if i'm wrong. So, the main loop reads the analog once, figure out which button was pressed and call the appropriate routine, then repeat over and over again. The display is being refreshed by an interrupt routine.
I suspect it was for switch debouncing. So the voltage would not fluctuate like crazy as the buttons are pressed. But I end up checking for button release in code anyway, so it is probably no longer necessary.
This method would mess up if you press more than one button. To read multiple simultaneous keypress you will need to dedicate more Arduino pins. To read a 4x4 keypad you will need four outputs and four inputs.
Hello Akang, You are correct, Hari Wiguna is an Indonesian name. I was born in Jakarta, but I've lived in USA since I've I graduated high school. Glad you like this video. Thank you for your kind words.
This is awesome..one pin onlyyyyy.....but the only problem i am getting is my reading are not stable as yours..mine r varying..even i m using a capacitor too as u..even if i dont press any key it fluctuate from 1010 to 10233.. can u help
With no buttons pressed, the bottom part of the voltage divider should be open, and the 4K7 resistor should bring the analog pin to 5V and report 1023. I wonder if one of the membrane switches is stuck?
No..i think its not..bcz when i using same resistor as you i am getting stable readings and if i multiply each resistor by 10 then it becomes unstable..so it looks like high resistance values r the problems...but why?? i don't know..
1. Since this is analog, it is inherently imprecise. That's why I suggested getting the values and then making the code tolerant of the slight variations. 2. Make sure that the input voltage is stable. Are there high current outputs being involved? you may need to power those from a separate supply rather than the Arduino 5V so the input to the resistor ladder would remain stable. 3. This is a hack. It should only be used when you cannot afford the I/O pins. The proper way to read a keypad is to use row column matrix to scan the keypad. Uses more Arduino pins, but 100% reliable.
@@HariWiguna Thanks man.. the values are just too bad.. sometimes im getting same values on different keys.. and im running out of arduino pins(uno)... I'll try to power up the arduino with 9v.. im using a laptop so maybe the input voltage is not stable
@@3000mi_away, I've just thought of another possible cause. Please measure the switch resistances on the keypad. Just try a few of them to make sure they're very close to zero. Some keypad uses carbon pads instead of actual metal to metal connection so it has a relatively high resistance and probably also pressure sensitive. The harder you press the lower resistance. You could try using higher resistor ladder values to make these variations less significant. Good luck!
At 3:44 you can see that pressing each button shows a different value. Press each and write down the value. Your values will be slightly different due to the variations on resistors. That is how I got those numbers.
Hari Wiguna If i press, example for number 1 this value 2043 or number 2 = 1463 int thresholds[16]={2043,1463,7823,23,3713,3343,2943,2483,4813,4563,4293,3993,5603,5423,5223,5013}; while (analogRead(A0)
The maximum value returned by analogRead is 1023. Where are you getting 2043 and 1463 ?! www.arduino.cc/reference/en/language/functions/analog-io/analogread/www.arduino.cc/reference/en/language/functions/analog-io/analogread/
for example I got 180 for “1” the first time, then after checking every value one time I checked it another to be sure and got 170, also some times if I hold a button it shows completely random values
Ah, yes, membrane switches are not perfect, so there will be some variation, on my switch it only varies by about 2 or 3, so I set my tolerance to 4, see line 87 on this listing: github.com/hwiguna/HariFun_Mega_Sudoku/blob/master/Seven_Segment_Sudoku/e_Keypad.h You probably need to set that to 15 to account for the difference between 170 and 180. For each key it would vary, but should NOT be random. Also make sure you do not set that tolerance too large. It should leave enough room between the values in threshold array.
Sorry, I no longer has electronic copy of the circuit, but you can see it clearly in the video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-G14tREsVqz0.html