16-key keypad with an Arduino and a PCF8574 I/O expander module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome everyone in this video i'm going to show you how to use a keypad with an arduino and how to process the entered characters and numbers and the reason why i do this is because a lot of people suggested me to improve my control panel for stepper motors by adding a keypad so this guy here because it would be much easier to modify and enter the numbers and different parameters by using a keypad and in fact i already started to work on this so here is the first let's say prototype of the new control panel so this will be presented in an upcoming video but in this video i just want to talk about the details because i was not really sure about these keypads i have never worked with them so i learned how to work with them and then i want to show you uh the same thing that i learned so you will see how to enter characters how to process them and then i will show you the source code and of course you will be able to see everything so the circuit is the following we have an arduino nano here and the only thing that we are using here is the five volt connection and the i squared c connection so we have a 16 by two lcd we will show different information on this basically the entered characters and this is connected by i squared c so data sda goes to a4 pin and clock scl goes to a5 pin and then here we have an io multiplexer module which has eight pins which can be selected as input or output it doesn't matter and this is the pcf 85 74 chip you can also select the address uh by changing the uh jumper pins here and also uh it has four pins so actually you can chain these together uh in multiple times so you can have several i o boards connected together and this is also communicates uh with the microcontroller via i squared c so why i'm using this is because this keypad by default has eight pins and i will explain you why it has eight pins but uh this has eight pins and i did not want to use eight pins from a microcontroller because that's pretty much unnecessary so instead of that it's much easier to just use a high squared c i o module so then we basically simplified those eight pins to two or four pins depends on how you look at it but let's say two pins because the communication goes through the uh i square c pins and you would use the power pins so the plus five volt and the ground anyway if you have something connected to your arduino so we arrived to the final part which is this keypad and how it works is relatively simple so you have four pins just to select a row and you have four pins to select the column so what happens is that you raise one of the pins out of these eight pins uh too high and then that will select a row and then uh that will put everything too high and then you start reading the pins uh then when this row is selected and when you press the button one of these pins where you press the the the key or the button within within a row will jump to ground and this is what you read and this happens very quickly uh after each other so you select a row and read the columns you go to the next row read the columns and so on and so on and this goes on and on and that is what happens uh inside this i o module and i will show you in the code because it's more easy to explain there but technically that's what so we have a matrix connection here so four pins to select for one of the four rows and four pins to select one of the four columns so whenever we press the corrector something uh will will happen to the pins so the output will be changed and that's what we read and that's what we modify as well so i started the system and i show you how this happens in the practice and what i actually implemented so this is started and i really hope that the display is visible so what we have is two lines string and double and of course when we work with this thing uh the most straightforward thing is that to press the character and then combine uh the consecutively pressed characters into a string that that goes quite straight forward in the code and then of course you want to pass this uh set of numbers uh to a variable so for example if you want to set up a speed or you want if we go back to the stepper motor uh example you want to define a certain amount of steps and so on and so on you you want this string to be in some numerical format which is for example double float or integer or something like that so we have to do that in the microcontroller so what happens here is that i press a few buttons so that will be combined into a string and then when i press a certain button on this keypad that string will be converted into a double and it will be shown in the bottom line so that's one part which i implemented and also i implemented the delete function let's say so what happens is that when i press the key d d stands for delete then in the string we chop off the last character from the string so we delete back the from the number and this is to correct the mistakes so i will show you how this works in practice and then of course i show you the source code how to implement this so let's enter some kind of number so let's say 43 point 58 so this is our string now and if i press a so i assigned this process to the key a then the double line should show the same number and again you will see it in the code this is not just copying this same set of characters but it takes the string variable and converts it into double and then the double is printed here and it's the same number so that works uh pretty well so now i show you something else so i start to enter the number and i accidentally enter two decimal points which will not be processed properly when we convert to double so you want to get rid of that decimal point i just enter one more number to make this more spectacular so we have this which is uh which is a bad format so then we can delete this as you can see so it goes back actually i can remove everything and then we can enter our own number so now 12.12 so i press a again and then it's converted and then this works pretty well with any kind of number so 0.16 for example and then sorry then it's converted and then here i just set the format to two decimals so if i enter a bit more decimals than i should this will only show the first two decimal digits and of course it rounds it up but this is how i uh implement it so i will show you how this can be extended if you want more uh decimal digits but this is pretty much enough for most of the purposes so you can see that it works and it works properly so then what i did here is that i implemented these ten numbers zero to nine and then i also implemented the star symbol which will be used as a decimal dot so as you can see if i press this you will see dots and then i implemented the character a but nothing prints with a but that's the yeah conversion uh triggering button and then b and c does nothing so it will not even print here and then d is the delete so that looks like this so i enter a bit too much numbers so then i can delete it back and then it converts so now i will show you the source code how it works and how you can implement this and once again this is a very simple example i know but this is part of a bigger plan so that's why i wanted to show you because this can be very useful for a lot of projects where you want to use a keypad and i cannot promote this thing enough that this is dirt cheap but it makes your life much easier because uh now first of all you don't have to use the very valuable uh i o pins on your arduino or whatever microcontroller you use and it's quite straightforward to code this stuff so i really recommend you to get some of these i o extenders because they are yeah basically just made for these kind of tasks where you need to let's say translate several pins into just a few pins and it works uh very well so it's a very nice product for for this kind of projects so let's go to my computer and i will show you the code and i will explain how this reading and writing of pins works so we are looking at the arduino code now and let's see what is happening inside the microcontroller so for this program we need the fire library because of the i squared c communication and we also need the liquid crystal i squared c library because of the liquid crystal display that we are using and here we create one instance so we are using this address for the for the lcd and then this is a 16x2 lcd and that's all we need to tell about this to the software and then we have a few variables so let's look at them so i'm using some kind of timer and this is just to not allow the software to read the buttons uh quickly after each other because we are pulling these buttons so if i keep the button pressed or if i just don't press quickly enough then the microcontroller will register multiple key presses but we just want to have one key press at a time so this will take care of that and then this button pressed variable is just registers whether there was a button pressed or not because this will determine the further processing steps and then we have this so-called full string which is just the individual characters concatenated it to each other and therefore they will make a string and then we have this pressed character which is basically tells us which character was pressed among these 16 available keys and then we have this string added so this just keeps track of the chaining of the characters and we need this because sometimes we press a button but we don't want it to be included in the uh concatenated string so that that's why we need this variable and then the converted number is just the final number the final result that we have after converting the string into a double so that's what we have here and then the setup is very simple so we just have the wire and we just set some clock for the ice currency that but maybe this is not that necessary and then i just start the serial communication and i print something on the serial port i will of course show you a demonstration of this and this is just to confirm that the microcontroller is running and then we take care of the lcd so we have to start the lcd and then we put the cursor to the top left corner and print something in the first line and then we move to the second line and after that we print another message then we wait two seconds and we print the permanent parts here so this is what you saw when i showed you the display and i did the demonstration so in the first line we have the string and then in the second line we have the double and then the loop is relatively clean so we have two functions one is reading the keypad once again it is pulling the keypad and then the other is checking if something happened to the keypad and then this goes on and on until you turn on turn off the microcontroller so first let's see how we read the keypad because this is the core of the wall strategy that we are using here so we are here in the read keypad function and first we look at this part so if the button pressed is true so recently there was a button press we have to see if 300 milliseconds has elapsed or not if it hasn't elapsed so this is not true then we do not do anything otherwise we reset the timer and we say that the button pressed is false so we can look for another button press and why this is important is i will i will show you in the latter part of this code so what we do here is that we use this i o expander and therefore we have to communicate with it via the i squared c so we reach out to it and it is placed to the 0x20 address and then we have to send a message to it first so what happens here is i explain that we activate a row and then we read the columns and there will be a change if one of the buttons is pressed within a column or within a row and that will be reflected in the message which is sent back to the microcontroller from the i o board so let's see how that happens so i send this message to the i o board and that means that the pins are activated in a certain way so what happens is that between the p7 and p4 pin of this io expander the p4 is zero and that means that the first row is activated so whenever you press something in the first row then something will happen but if you if if you are standing at this part of the code and you press other keys then the first draw keys and nothing will happen so then we activated the first row we listen to that and then we set all the pins high within this row so then we request from the same address that we reached one byte and we see how the value of the the reply uh changed as compared to the send value and then what you want to look for is that the changes in the first four numbers if we look from p0 to p3 and then that means that the sum button was pressed and how this looks like is that for example if p0 becomes zero uh the p0 pin went low so that means that the number one key number one was pressed and if we translate this binary number into a decimal number then we can yeah build up this kind of condition so if we received this number from the uh i o board then we know that we press press the the key number one so what we do here is that the pressed character will be one and then we know that a button was pressed so that is true and since this is a numerical value it's a number we want to add it to the string so we make this true and then we move on and we can see that if the answer has this kind of pattern which means that the p1 became zero here then we know that the button number two was pressed in the first row and then we do the same practice or same exercise but with number two and then we look for this so if this was the reply uh that means that the p2 became zero then we know that the key number three was pressed and then this binary number corresponds to this decimal number so if this decimal number came back then we do these kind of changes and finally we see if that if the p3 became 0 this one here then we know that the last key or less button in this row was pressed which is the a button so therefore we do something and here as you can see i don't have the string added equals true part because we don't want to add a letter to the string because that would not be translated into the double so we skip this but we will do something with this character later and then we read the first row so what we have to do is we have to move to the second row and read it so what we do is then we send another message and if you see uh this uh pattern carefully then the zero moved towards the left so towards the p7 by one character so if i jump back you can see that the zero is located here at p4 but now it is at p5 so this activates the second row in the in the keypad and then we do the same exercise with the columns so everything is set to high and then we check the same things so for example we know that the second row is activated and then we get this answer so this means that the p0 went low so in the second row we pressed the first column which is the key number four and then we do the same exercise and then this goes on and on with the same pattern as i explained for the first row so this is checked and then we move to the third line so you can see that this zero moved towards the p7 and now it's at p6 so that means we activated the third line and then we set everything to high so basically these four and we see which of these four selected pins went to low because that will indicate that one of these numbers are uh pressed so then we check which was it so this is just the conversion of the answer in binary to decimal number and we check these numbers if one of these are the cases then we know that one of these buttons were pressed and finally you can see that here uh p7 became zero so we activated the last row or last line in the keypad which is the star zero hash d and then we do the same uh exercise so everything is high and then we check which one of those became zero and here i did a tiny change so i want to have a decimal point or decimal separator so when i press the star then i register a dot so that will be our decimal separator and this is all so then let's see how these are treated and how these are handled and they are handled in the check input function so then if a button was pressed so if you remember this is set to true here or wherever we press the button so if a button was pressed and if we add that button to the string then we just do this linking so the full string becomes the full string plus the pressed character so we just place the character at the end of this string and then of course this is printed on the display and and that's all and we also delete the content of the second line uh where we printed the number so this just removes some confusion and then after we did this once we have to make sure that the string added is false and it stays false until we don't press a button again so we don't enter this part and we don't keep [Music] the string expanding basically and then we see that what happens if the pressed character was a so of course when we press a letter character on our keypad we do not say that the string edit is true and i show you how this looks like so for example here as i said earlier we do not add this string added line here because we don't want it to be registered uh as a number as part of the number so then this is skipped now but if the a was pressed then we just print the full string and this is now still a string and now we convert this string into a double and then we put it on the display but of course as a check we print it on the serial port and after this all happens we have to reset the full string so we have to sort of empty it and then also we do the same with the converted number so we don't have any confusion and the pressed character also has to be empty otherwise it will keep otherwise the code will keep entering this part and that's not so good for us and then we clear the first line when we did the conversion so when we start entering a new number uh in in the first line then it will show up as a new clear number so then we can also enter this function if the pressed character was d and d stands for delete so what happens here is that we chop off the last character of the string so what happens in this substring function is that we take the full string which is the wall number or in this case it is still the wall string and then we start from the zero part of the full string so the first character basically and we move until the length minus one so that means that we only consider the new full string as the old full string minus the last character so that that happens here in this first line and as a confirmation i print this on the serial port and what happens on the lcd is the following so we put the cursor in the first line and then we put the cursor to the 8 plus the length of the full string which we just created up here and why 8 is because if you remember we printed the exact same thing here on our lcd as well and the length of this with the space is eight so i just show you with the cursor so 0 1 2 3 4 5 6 7 8. so the printing starts from here after the white space and then we also add the length of the of the full string and then we print a space there because we do not need to redraw the wall line we could do that maybe that's more simple but here i just wanted to chop off the last character because that will be not printed again and that happens here and then we move back to the very beginning of the number so just basically here and then then we print the full string so then let's say instead of two decimals now it will be just one decimal printed and if we don't do this then the second decimal digit will be still there but with this we remove it and then we do this but since we reprint the full string we could actually simplify this part but yeah just i just don't do it here and after we printed everything on the display we have to reset the pressed character so in the next iteration we will not come back here and basically this was the raw code so i explained both of these functions so let's see how it works with the serial port so i just opened this and we got the same message as we have here and then now i keep pressing some numbers uh on the keypad and i will explain what happens on the display so i just i will enter 14.57 so 14.57 and now i press a so here we just got basically these numbers so serial.print and then one character and you see that i use the serial.print and not the print ln so that's why the numbers came after each other and then the a was also printed because obviously i printed that but here when i press a or when i send a to the terminal i will add the line break with the with the usage of this serial.println so that happens and since i pressed a we entered this part of the function and then that initiated the conversion so then first of all we printed the string which is the full string and then we also printed the conversion so the converted number is the same so this is just this lot of serial.print and println just to use just used to yeah prove everything here and now if i start typing numbers again you can see that it starts from zero so i have this 254 dot 31 for example and then i press d as delete so now the code enter this part and what happens is that here of course we saw that we press the d but then the string here is the let's say shortened string so now we got back this without the character one and now i can press a and then the recent string is now converted into a double so this was the string that we kept in our string variable and then as i pressed a it was also converted into a double and this is the double and since by default the serial that println shows two digits then of course we did not have anything here so that was zero so basically this was all uh i hope that this will be helpful for you especially with this i o expander i think it's a very useful thing and it makes the life much more easier as you can see the coding is not so difficult maybe it can be further simplified i'm not a programmer so i might have missed a few things but at least i could make this keypad work and it works in in the intended way so what i wanted to have here is basically i wanted to be able to enter numbers and i wanted to be able to pass these numbers to some other variables and i also wanted to be able to delete numbers if i mess up the typing so all of these are realized and as i mentioned previously this let's say project will be carried over to my stepper motor control panel so you will see that project being realized quite soon so this was the whole video don't forget to check my website especially the tools part because you will see all these parts that i work with and you can buy them if you want via affiliate links and you can find other interesting tools as well so i hope this video was useful to you i hope you learned something and see you in the next video
Info
Channel: Curious Scientist
Views: 13,424
Rating: undefined out of 5
Keywords: keypad, 16-key, numpad, numerical, keyboard, arduino, arduino nano, pcf8574, io expander, i2c, lcd, 16x2, arduino programming, electronics, engineering, demonstration, input
Id: Gi7199Vj_uc
Channel Id: undefined
Length: 32min 45sec (1965 seconds)
Published: Sun May 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.