No products in the cart.
Keypad Tutorial

14
Jul
Overview:
let’s talk about keypads, and most importantly matrix keypads. These are extremely easy to set up and you only need a pin for each row and each column. So if you have a 4×4 keypad (shown below) you need 4+4 totaling 8 pins. 8 pins for 16 buttons? Seems like a good deal to me!
For this one I’m going to show you how to set up the key pad and code so that when the end user has to put in a ‘password’ . If the password is right: a green light comes one. If the password is wrong: a red light blinks 4 times.
Now these LEDs can be pretty much anything. You could use this as the basic code to open and close a door, turn on a light, or even turn off an alarm
Components:
1X Arduino Compatible Board
1X 4×4 Key Pad
1X Male – Male jumper cables
2X LEDs
2X 220Ω Resistors
#include <Keypad.h> #define greenLED 11 #define redLED 10 const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; String passWord ="1234"; //manually define password String passInput; byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); void setup(){ Serial.begin(9600); pinMode(greenLED,OUTPUT); pinMode(redLED,OUTPUT); } void loop(){ char key = keypad.getKey();//gets the character pressed if (key != NO_KEY){ if (key=='#'){//checks to see if "enter" is pressed if(passInput==passWord){ //is the password entered the same as the one defined? Serial.println("congratulations you have unlocked the safe");//yes digitalWrite(greenLED,HIGH); }else{//no Serial.println("Sorry... Wrong Password!"); digitalWrite(greenLED,LOW); for (int i=0;i<4;i++){ //blink light 4 times digitalWrite(redLED,HIGH); delay(200); digitalWrite(redLED,LOW); delay(200); } } Serial.println("cleared"); passInput="";//clear password string }else{//if "enter" is not pressed add the key char to the nd of the string passInput=passInput+key; Serial.println(passInput); } } }