Tuesday, 23 April 2013 12:17

Password access with Arduino using keypad

Written by
Arduino and Keypad
This example demonstrates how to use a 4x4 matrix keypad access password from an Arduino. Sketch below required to install password and keypad library, both available at Arduino Playground. Below list the functions of password library:

set(password)
Set the target password equal to password.

is(password)
Is the target password equal to password

append(character)
Append a character to the currently guessed password

reset()
Reset the currently guessed password

evaluate()
Is the guessed password equal to the target password?

Download

 

How it works

  • The secret password is initialize to 1234, you can change this latter by using password.set() command 
  • The maximum of password length is set to 6, any password input length exceed 6 characters will force to execute checkPassword 
  • Waiting for input, valid key for password is 0 to 9.
  • checkPassword is executed if user press #, it compare input password (guess password) and secret password using password.evaluate()
  • If user press the D key, the secret password will reset to 123. Of course you should use your codes to change secret password with keypad.

Read 38311 times Last modified on Sunday, 12 June 2016 10:59

11 comments

  • Comment Link admin Saturday, 24 June 2017 13:11 posted by admin

    The change password function is to change an old password to a new password. The new password is a String type, you must convert it to character array before you can call the password.set function.

  • Comment Link Javid_HPF Wednesday, 21 June 2017 16:33 posted by Javid_HPF

    And learn another way to ask old passcode, How can we do this with using an if condition?

  • Comment Link Javid_HPF Wednesday, 21 June 2017 16:23 posted by Javid_HPF

    Hi, please explain more about the change password function!

  • Comment Link admin Thursday, 25 May 2017 12:21 posted by admin

    Did you solved the problem?

  • Comment Link Siam Al Huq Monday, 15 May 2017 16:13 posted by Siam Al Huq

    //**heres my code n some codes i have tried but couldn't change the password newPasswordString remains blank the way i have tried please let me know how can i solve this.TIA. **//
    #include
    #include

    String newPasswordString;//hold the new password
    char newPassword[6]; //charater string of newPasswordString

    //initialize password to 1234
    //you can use password.set(newPassword) to overwrite it
    Password password = Password( "1234" );

    byte maxPasswordLength = 6;
    byte currentPasswordLength = 0;
    const byte ROWS = 4; // Four rows
    const byte COLS = 4; // Four columns

    //Define the keymap
    char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
    };

    //// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
    byte rowPins[ROWS] = {6,7,8,9}; //connect to row pinouts

    // Connect keypad COL0, COL1, COL2 and COL3 to these Arduino pins.
    byte colPins[COLS] = {2,3,4,5}; //connect to column pinouts

    // Create the Keypad
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

    void setup(){
    Serial.begin(9600);
    Serial.println("Arduino Keypad Secuirity Lock");
    Serial.println("Enter Your Password");
    Serial.println("To Unlock");
    delay(1000);
    }

    void loop(){
    char key = keypad.getKey();
    if (key != NO_KEY){
    delay(60);
    switch (key){
    case 'A': Serial.print("Entered");break;
    case 'B': break;
    case 'C': checkPassword(); break;
    case 'D': changePassword( key ); break;
    case '#': break;
    case '*': resetPassword(); break;
    default: processNumberKey(key);
    }
    }
    }

    void processNumberKey(char key) {
    Serial.print(key);
    currentPasswordLength++;
    password.append(key);
    if (currentPasswordLength == maxPasswordLength) {
    checkPassword();
    }
    }

    void checkPassword() {
    if (password.evaluate()){
    Serial.println(" Password Match.");
    Serial.println(" Access Granted.");
    } else {
    Serial.println(" Wrong passwowrd!");
    Serial.println(" Please Try Again");
    }
    resetPassword();
    }

    void resetPassword() {
    password.reset();
    currentPasswordLength = 0;
    }

    void changePassword( char key ) {

    //readString( );
    byte currentPasswordLength = 0;
    Serial.print("Enter password: ");
    int i =0;
    while(true)
    {
    Serial.print(key);

    currentPasswordLength++;
    password.append(key);
    newPasswordString = keypad.getKey();
    if(i==5){
    break;
    }
    i++;
    }

    //Print some feedback.

    //for(int i=0 ; i

  • Comment Link Tarciso Ferreira Friday, 07 April 2017 08:15 posted by Tarciso Ferreira

    Tanks, very very nice.

  • Comment Link Bong Tuesday, 07 February 2017 13:25 posted by Bong

    i copied the library into libraries, error found, invalid library foundC:\Users\Bonggicious\Documents\Arduino\libraries\Password

  • Comment Link admin Monday, 25 April 2016 11:10 posted by admin

    I have an electronic code lock project which is written in Chinese. On the top left of the webpage, there is a combox use to translate the article to other languages.

    http://ediy.com.my/2012-10-21-15-15-03/2013-04-14-05-06-50/item/79-arduino

  • Comment Link Shiva Monday, 25 April 2016 06:33 posted by Shiva

    Good tutorial, just one doubt that i have,
    How to ask user to enter old password, verify and change password inside changePassword() function.??

  • Comment Link admin Sunday, 06 March 2016 13:14 posted by admin

    1. use your keyboard to change the password and store it in EEPROM
    2. Read the password from EEPROM on startup
    3. Use password.set(EEPROM_password) to initialize your password

  • Comment Link don sim Saturday, 05 March 2016 23:25 posted by don sim

    How do you reset pass to another # without scanning? Is there a way to set it by the keyboard ie user defined?

Leave a comment

Back to Top