arduino 4×4 keypad
This is episode number 11 and this week we’re going to talk about keypads and inputting keypads into an Arduino. However, before I start, I do want to send out kind of a shout out and an apology. At the same time to Dennis Englander – hopefully I said his name correctly. I got an email from him this week and I haven’t had a chance to reply so that’s. My apology, I’m in Dennis’s email he’s, been watching the show and he’s talking about maybe using some of the Arduino stuff. One model railroads so I’m very curious how and what he does with that and Dennis. If you do something like that, get something working. Please send us a video, we love the feedback, moving more feedback, the better and again apologies for not getting back to you quicker than this all right. So this week I mentioned we’re going to talk about keypads on an Arduino and one of the first ones. I wanted to show you already missed, because it does look a little bit like a mess, so don’t be too scared. When you see this, this is the Arduino this week and what we have is we have a serial display that we talked about before so using the same libraries as we did in the previous show and right here you can barely see them. There’S, a red and a green LED which will be used in the second project or a second sketch that we talked about and then here’s this little keypad and it’s very light and you’ll see when I let go of it just wants to swing back to wherever It was so it’s something I have to hold when I use it.
I’Ll show it to you, but it’s, very thin, it’s, a little membrane you see out, then it is this little membrane keyboard and I will talk a little about these keyboards, so you’ll notice on the bottom. There is eight wires and the keyboard is a four by four keyboard. So the way these work is these little runs that are down here in this cable, each one, the four of them go to the to these rows and the other four go to these columns. When you press one of these numbers, two of these pins get connected together, so what’s happening in the Arduino and I’m going to talk about the library as doing this a little bit but what’s happening in the Arduino is it’s random or not random. It’S serially, going through for these pins, make it an Ohio really fast second around in a circle, so maybe doing I don’t know exactly is doing office, doing it by row or by column. But if this is saying you know, this row is hot. There has a high high high and high when you press this. The high goes back down the other side and it looks for that so it’s, basically scanning looking for what’s, been pressed so that’s, the eight pins four for each row and four for each column, and then that comes back into the Arduino. The way I have a set up in the Arduino is I’m starting with pin number 133 for a B pin 5 and it goes to pin 11 that’s the 8 pins that I’m using and right now, I’m running a sketch.
The basically almost one is going to show you what button I press so I’m, going to press button 8. You see it says 8 I’m going to press 6, it says 6, then presently I can do you know C or a or whatever in these buttons are going to come up and show you what button I actually pressed. So what I want to do first is walk through this, because again, this is a library that was taken from the Arduino website it’s in senator code projects. I believe I’ll have a link in the in the actual program itself, as well as the show notes. I will be in there as well, and it helps a lot with this. If you, you could do it yourself and actually I’m tempted to write a program using the Arduino just in regular Arduino language, not to see classes, to show you how simple it really is to use one of these. But the library makes it very quick and simple. So what I want to do is get over here to again this and bring up the Arduino program and go to the computer and there we see the Arduino program, and this is the keypad input that we were just looking at. This is a very simple program to understand so down past my header information, so the first two things you see right here is I’m, including the wire date in liquid crystal dot H, and this is just for the display we talked about this.
I believe episode 6 and we’re not going to go from great detail how, though two wire interface to the LCD works. The reason I use the to our interface is because I’m using so many pins with the keypad, I wouldn’t have been able to use a parallel screen with all the pins that are required. So the next thing that’s new – is this keypad. That H – and this is the library that I download and a scroll back up here a little bit and right here you see the link to the library. So if you go to this link out to playground code, go to Arduino, dot, CC playground code keypad, and you can download this exact library that I’m using so that’s the quickest way to get to started but that’s. What keypad that H includes now to define the actual keypad? Now you can do this a couple different ways. I thought this was probably the simplest way to do this was I define the number of rows in the number of columns which are both 4? In my case, you can get them at their three by fours, so they don’t have a key, a b c and d down the side, so it really depends on which keep keypad you’re using. Then you define a multi dimensional array which of these two dimensions, one with Row 1 with columns and you define what is that each position will need two keypads. So you see my keypad and let’s go.
Look at the keypad again real quick! You can see the keypad is one two, three a and then four five, six B, seven, eight nine C and then star or zero a pound and then D. So if you look at the code, I have defined it exactly the same way: 1. 2. 3. A 4 5, 6 B, 7, 8, 9 C star 0, pound D, okay, so that defines the keyboard basically it’s an array, so I can say based on this position, I know I, when I get that back, it’s been a 5 so now what else to do? Is define what each of the pins is connected to now this after you threw me for a loop, because I didn’t have a schematic for this particular keypad and I had a backwards. Basically, I had everything back when I had a bowl, I thought was rows and columns based on another key pair that I had and then it’s the reverse. I had correct, but I had them like this. 11 10. 9 8 was down at the bottom and then the 7 6 5 4. So this keypad that I’m using this is how we have to have a configure so pin 11 10 9 amp 8 are for each of the rows and the pin 11 is the top row in 10 to the second row and so on, and then 10 7 Is the leftmost column and the 4 is the rightmost column, so actually define the pins and we have everything we need to actually create the class.
So what we do is we define a very well calling a keypad just to keep it simple, so we know we’re. Looking at and it’s a keypad type class type and then on defining the instant of the of the class as being this make key map, and I give it the keys so it’s making a key map from this array and sending that over to this keypad library. I’M telling it how many wrong or tonight which pins are for the rows and which pins are for the columns and then the number of rows and number of columns. Now I could have obviously made rows and columns in here 4 amp 4, and I could have put that right here, 4 and 4. I could have put 4 amp 4 here but I’m, making it a variable. If I want to change my keyboard, I can easily do it. I can go to that 3 to 4. So if I only have 3 columns, I can just take and change this right here to 3 and everything follows on top. So you have to get rid of this column right here too, in the process, but I would have to go through everywhere and make sure I got it so it’s. Why I’m using variables, then you see me defined, look at crystal which we talked about again. In episode 6, if you’re still, I have questions about that. Google watch episode. 6. We covered the LCD to see our LCD in pretty good detail there.
Let me come back and we’re in the setup, so we do our Neutral LCD setup. We turn our backlight on I’m, basically setting the cursor to position through a row, 0 1 position for and typing out the word you pressed colon, which we saw on the screen and we’ll go back and look at that again and then all I’m doing. At this point – and this is the end of the program very said – very simple – is I’m reading keypad input by doing keypad and get key now here’s. The thing in this library returns a 0 or nothing basically null if no key is pressed. So what I’m doing here is I’m saying if the value of the key that was is not equal to zero, which means something was pressed. Then I want to put my cursor at position 17 on the first row, and I want to print out what that key was that was pressed. However, if nothing’s being pressed, I don’t want to do anything because here’s what’s happening. This actually happened to me, and it was confusing to me when I first did it, as I didn’t have this. If statement here, I was just doing said, cursor print and when I’m not pressing, while not pressing a key. It comes back as null. Well, that prints a weird character on the screen, and so I was as soon as I pressed it one time it would tell what’s he’s blinking to go back to the weird character, so I had to put this code in here to look for a key press.
I did was not aware to us are missing: we think that it would return null like it was in it and if you hit the button one time it doesn’t, it does not keep. We just returns nulls in between. I think you will come back and it will tell you the key is still being pressed so that’s. How simple this little program. Is it not all. It does and let’s go back again and look at the actual Arduino and get the keypad here and you see on the screen right there. It says you press. The last thing I pressed will star so I’m going to press 8. You see it says 8 and then there’s five there’s, two right, there’s two, so you see it’s, all that program is doing is just showing you what input was pressed now. That was pretty much what I was going to cover for the evening. However, about a half hour ago, I got this wild hair and decided. I wanted to make something a little bit more, so I was going to basically bring the Arduino in with the keypad in the LCD and show you how to do keyboard input and how simple was, and that was going to be my show. So that would have been like 11 minutes of a show and I just didn’t feel right doing a love a minute show. So I have since gone and created a program, or I sketch and I’m going to load it up here in a second in order to walk through and play with it that basically simulates entering a numeric code to get into a door.
And actually this could be used for that I’m using LEDs to distinguish if it’s open or closed, but those can easily be set up to be on a relay for add or anything like that. So, just as an example, so let me get over and get into that and I’m going to load it up. Okay, the sketch is uploading, all right. It’S done so let me go over to the Arduino whoops start doing now and I’m still doing so. Here’S, our keypad now I’m, going to tell you right now. Our code is one two three four. So if I enter in my code, one two three and four, you see the green light comes on and it says access granted. Welcome, but let’s say I put in one two: three, a alright that’s, an invalid code. Let’S say I put in wallet still there. We go let’s say I put in a 1 for hope indelicate. Maybe you see let’s, try five hope indelicate. So you see it immediately. Tells me in Ravello code, you see the red light, go on invalid code and at LV on the screen and altitude and want to put in a valid code. I see the green light, which is where you’d open relay or whatever open the door, and you see on the display screen access granted. So what I wan na do is I want to walk through this program it’s a little more complicated in the yellow one, just because it’s a little bit of a state table which we talked about in previous episode as well about how state tables work.
That was the one where we talked about stop lights and there is a sort of state table in this one as well, basically it’s a state table that keeps track of what input number you’re on to check your pass code. So let’s go hop over to the computer and we’re going to walk through this code. Okay on this code, it’s called secret key code we’re using the same library, easy right here – the keypad library and I have the link in the header there too, and at the top we’re very similar. We have the LCD stuff we’re going to talk about that in detail again, that’s episode, sick leave on Google go watch that and then we have keypad of age which we had in the previous sketch as well, and then we get into a little more. So I have setup constants for the green and red LEDs again, that’s, just consistency through the code. You can easily modify them by just changing these variables and that would fall through the code. Next, you see me setting the car secret code now I’m, just using one. Two three: four: it could be one two, three a or a the keys that are on the valid keypad, including star or pound, and then I’m defining a variable current position. This is the part of the state table that we just mentioned. Basically, this is going to hold the current location of the key press that you’re you’re entering.
So you start out the very first one, so you’re going to be looking for the key for someone on you, don’t get it you’re, obviously you’re putting in the wrong code and then, as you, if you get one, then it competition will add by one and is Looking for a two so it’s, just how we do our state table as to where we are and you’ll see that more on down on the code. Alright, now we get back into the defining of the keypad which this exactly the same is the first sketch it’s. Four by four it’s exact, same keypad, so everything here was pretty much just copied right out of the other, the other program so let’s come on down and in the setup things are a little bit different, so here’s what you have the LCD in it like you Had before – and you have the turn, the backlight on to all standard stuff, but then I’m going to display the screen that you are sitting at by default, which is the let’s make it and let’s go. Look at this so we’re going to see how this is all working it’s. This screen right here it says let’s, make it secret code project enter a secret code, so let’s go back and look at what this subroutine does so I’m going to call display code entry screen, so let’s go find that down here right here it is display code Entry screen – and the first thing I do is clear screen now.
There is probably a more proper way to do this, but because I only had 20 to 30 minutes to write this, I did it the old fashioned clunky. What I will consider clunky way, which is I set a cursor and a blank out easy level e to the lines probably that’s most efficient, but for an example for this it’s going to work. There is probably a screen blank code I just did not have a chance to go, look up what it was, so I just did it the quick and dirty way so back to our display code. Entry screen here is the clear screen I’m going to set my cursor to the first position of the first line and I’m going to put out let’s, make it secret, go to the second line code project and then go to the third line enter secret code. So that’s the display code entry screen that’s this part of my setup. So I just this pudendal screen now I’m, going to go to the red and green LED pins, make sure that their output and turn them both off all right. So that’s done the setup, and now we start our loop. So here is our loop and I’m going to define an L and il 4 me always means loop and you’ll see that in the for loop down here how I’m going to use that I’m. Also now this should actual probably be on like this, because I’m going to take the keyboard, the key input from keypad get key and, if it’s being pressed, which we just not equal to zero, I am going to first of all blank out an area on the Screen and how you’ll see in a second, what this does and I’ll show you how it this works.
But this area of the display, which is the fourth line starting at the 14th position, is going to be blanked out and then I go back to that same position. And then here what I’m going to do is put out an asterisk for every character. That has been pressed, so this simulates, how many characters have been pressed when the first program, I was actually telling you you’re, showing you which key was pressed, but you wouldn’t want to do that in a secret code, if you like any password. So what I’m doing here is just showing you that, yes, I saw your key press. If I didn’t do this, you could prosecute, you can be wondering, did it take it or did it not take it I’m, getting around it the other option and how I did it originally was. I just did LCD print for each time. You pressed a character and it would just add to the end of it. The problem is it doesn’t always clear properly, so this allows me to make sure that what you’re seeing is the exact number of characters that are actually there. This is probably the better way it just didn’t work reliably the way I had it before. Okay, so now we come down and we, since we have a Val key we’re still in this routine. I printed the asterisk out saying I saw your keyboard press now. I say is that keyboard press equal to our secret code at our current position, so here’s a state table variable again so I’m starting out with zero.
So in our case our code is 1. 2. 3. 4. If I, the key that was pressed, was equal to 1, which is the first character of our secret code, then I’m going to say, ok. My current position is now number 2, because the first one was valid and then I’m going to come down and I’m going to compare the current position to equal to 4 and it was equal to 4. That means that I got all 4 of the valid code. So at that point I can do unlock door and start over again. A current position equals to 0. So let’s go look at the unlock door at this point. We’Re going to scroll down here – and here is the unlocked door routine. So what am I going to do I’m, going to turn on the green LED saying that you got access I’m, going to clear the screen and I’m going to set the cursor to the very first line? The first position put out a row of asterisks and then I’m going to go to the second row and put out access granted I’m going to go to the third row and say: welcome and I’m going to go to the 4th row and put out asterisks. So let’s go look at what this looks like on the actual Arduino itself. Ok, so when the Arduino here is the keypad again and I’m going to enter a valid code. Now, as I do, this we’ll watch down here at the bottom right here and you’re, going to see asterisks one asterisk to a streak that’s because I’m in position number two third asterisk and here comes the write code.
For so there you see the green light comes on access granted, so it did the whole clear screen printed this out at 5 seconds it clear screens and goes back to beginning in turns off the green LED, so let’s go back over here to the computer. So right here we saw us turn the green LED on I clear the screen, which happens fast enough. You couldn’t see it and then the print is out saying a secure access granted welcome. I waited five seconds turn the green LED back off again and then I redisplay the code entry screen because now I’m back to the point where I take another input so that’s, what unlock a door does let’s go back up to here now, so we left here And now, when we come back from unlock door I’m starting over again so I’m, seeing my current position is equal to zero. Our state table is now equal to zero and I’m, going back to my loop again so I’m going to go through that whole loop. Over again but let’s go through this time and say we get to position number one, which is actually number two. Basically second position and somebody enters a darn thing other than two and I set to the end of five and so whatever it is. It’S not going to match, so our key is not going to equal to our current position of our secret code. It’S going to come down here and it’s going to do in valid code.
Let’S go look at the develop code and it’s right here and right here. I am turning the red LED on I’m, going to clear my screen print out the same row of Asterix right out, access denied invalid code and then a row of asterisks I’m going to wait. Five seconds turn off the red LED and then put back the code. Entry screen one more time, go back and start from the beginning, and then I come back and I say current position equals to zero because obviously, if they entered an invalid code, I don’t want them to try and keep trying that same code, because there’s only in This case 16 keys. If I didn’t go back to the beginning, they could reenter the code again. I keep pressing each of the buttons till we found out what it is so once them to have to start over again so that’s current position, and then, when I do that, we’re back to the beginning and we’re, starting from the very first character. All over again. Now I put some code down in here in this unlock door and it’s right here and it’s. This comment so let’s say you wanted to unlock a door. You could actually use the green LED pin instead of turning on a green, LED and go out and control a relay of some sort. That would you open your door and typically five seconds on a door relay is enough that somebody in so I can be easily done.
You can also add in here an additional line. I don’t have any more pins on this particular Arduino, but well actually I do that’s, not that’s, not exactly true cuz. I do have the analog out pins. I could use, but you could come in here and put an additional arm line that would turn on one another pin. That would do the same thing, so you can use both LED and something to open a relay you can also – and this is something that I probably going to work on your next week or the week after is a servos, so you can actually have a servo. Do something – and if you had a small enough item that the servo was powerful enough to actually move up, move the pin to open the door. You could actually have the servo open itself up and let somebody open to open the door up and you can get some pretty good side service that’s, not a a far fetched option. It’S, probably a little bit more expensive and a little Bora elaborate than the typical relay and for the door lock, but you know either one will work just fine alright, so that is pretty much let’s do a very short show that’s, the only 27 minutes. So again, a very short share this week, although we’ve been very short, if I didn’t extend this a little bit bitings of the program, this will all be up on the show notes at Texan TV or you can go to let’s, make it that TV they’ll.
Take you right to the right page inside of the Texan TV website, either way, we’ll get you there, and this would be episode. Number 11 I’ll put all this Jo notes up here and links to everything I probably won’t have links to the display or anything because that was all in a previous episode again episode. Six was the LCD. I don’t know what episode was the state table. I think I’m in nine, eight or nine Episode, eight or nine and it’s the one about stoplights that talks about state tables. We actually build. You know stoplights with car waiting, sensors and so like that. I love the feedback, as I mentioned in getting the show. I got some feedback this week. I didn’t have a chance to answer yet I will get to that very soon. But ah, if you make something you’re getting parts – and you try to play with this – you know make civilian senators we’d love to see that we’re truly trying hard to build a community around all of our shows and the best way to do that is to get People to give us feedback and and send us your examples of things you make. Actually, I would like to have feedback is what you would like us to show you I’m, just going at this point, showing you things that I think are interesting, and my first nine or 10 episodes really was focused on getting you used to the Arduino in the Language, you know, and just getting the basics and there, but now get to the point where um I can go couple different directions and I kind of want some direction from our viewers as to what you would like us to show you.
I actually have some Raspberry Pi projects coming up here. Um the other project dimension like a lot first or second, show I’m ready to start working with that, a little bit more and showing you how to do so. Wireless like with the ZigBee, so I mean a cool things like that next week I said about servos there’s couple things that I can still can do that I have planned before. I would leave the Arduino, but I would I don’t mind sticking with your doing a little bit longer. If you have something that you want to see or wan na, you want to learn, and I love, I love the feedback to concept that direction a little bit and again we’re trying to build the community. So, if you’re watching this like on YouTube, you hit the subscribe button that helps all helps us a lot. Tell your friends about it, hit the like button. Things like that! Just help get the word around, so you can go in the community if you’re watching it’s like when. I saw like that. You know just grabbed there too, and that’s actually a great way to get us because it’s completely automatic download to your devices, your iPhone, your iPad, whatever you can download and watch it on your time, I think that’s part of our whole slogan is technology. On your time and that’s kind of helping to do it, we do like being in the chatroom no tonight I’ll be running in the chat room all by myself and it’s, a little lonely in the chat room right now and we’d love to have you come and Watch us live every Tuesday night at 7 p.
m. today I get started a little bit late. The stream came up about seven and five, because I had some problems with this dream, but Inga started about 720, but the pre show stuff. If somebody’s here I’ll answer questions and talk to them before it at the show, both we also have a ton of other shows. We have a new one. Just came out that um, we think, is we’re going to catch on it’s. Actually, a fan based show as the first one of these we’ve done so it’s a little bit an experiment for us but it’s about American Idol in American Idol fans. And what do they think about how America voted in who they like things like that? So it’s a very fun thing is also very interactive. We invite people in via Google hangout to come in via video and talk with us what they think and that’s one Thursday nights about a half hour before the results. Shall we start talk about the shows of the week and people thought about their performances? We don’t do a lot through the results. Show we talk during the commercials. Just do recaps, but then after we talk again and see what people thought about the results, do they think America got it right or why did they got it wrong? Things like that, so it’s, a very different kind of show for us not for technical, more fun and believe all kinds of technical shows too.
If you’re in a security, we have security 101 for people who are new to computers or aren’t, really internet security savvy. We have security coded, which is for security professionals and IT professionals we have Mac iMac show to show you how to get used to the Mac if you’re moving to Mac from PC, so we’ve all kinds of things like that and we’d love input for new shows And again, spread that around we’re trying again trying to build the community for all of our users and we’d love to have the community start to grow a little bit faster than than what it’s been. So if you see a show and you’d like it on YouTube, click the like button that definitely helps us out and helps get to help get the word around. So we definitely appreciate that that is it for let’s make it this week again. Feedback for a future show love to have it and the sooner the better. So I can research things I don’t know it’s always good to be a little bit more prepared when you get to a show. I said next week if these are going to be ZigBee.
arduino 4×4 keypad Video
arduino 4×4 keypad news
Posted on Wednesday April 15, 2015Simple Keypad Scanning With SPI And Some Hardware Hackaday16-button keypads have a clever method of encoding their data into 8 pins. Pins are mapped to four rows and four columns on the keypad. A user reads the … … Continue Reading »
Posted on Friday August 01, 2014Build Your Own 16-Button MIDI Controller with an Arduino LifehackerDIY musical instruments are their own thing entirely, but if you’re looking for a place to start, Adafruit has a guide to building your own MIDI controller using an … … Continue Reading »
Posted on Monday March 27, 2017Telink adds security inside BLE chip; Releases updated SDK for Apple’s HomeKit EE HeraldTelink Semiconductor has released the updated version of its Bluetooth Low Energy Software Development Kit (BLE SDK) for Apple HomeKit. The single chip … … Continue Reading »
arduino 4×4 keypad Social