Simple Math Calculator and PassCode Relay Switch using Resistive 4×4 Keypad Module

Overview

In this tutorial we are going to Test the 4×4 Resistive Keypad Module using this to make a Simple Math Calculator and Passcode Relay Switch, don't worry because this is an easy project and beginner friendly and I explained this as a very simple and detailed way.

 

Hardware Used

  • 2 – Arduino Uno

  • 1 – 5v Relay
  • 
    
  • 1- 4×4 Resistive Keypad Module
  • 
    
  • 1 – 16×2 LCD module
  • 
    
  • 1 – Potentiometer
  • 
    
    

    You can buy all this Hardware at Createlabz.

    Software Used:

    • Arduino IDE

    Library Used:

    Application Description:

    The first activity that we are going to do is to do a simple math calculator using the keypad buttons.

    Steps:

    1.) Finding the analog value of each buttons

    As you can see in this picture, the analog values were already given but because it is so sensitive you must check if it is really the analog value for each button by uploading this program into your arduino.

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
    
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
      Serial.println(analogRead(A0));
    }

    Upload ang pressed each buttons. You will see the values of every button you press.

    2.) If you have find the values for each buttons and it does not read the same values with mine then just edit my the code “Simple Math Caculator”.

    For the 2nd activity, Code lock using 5v relay switch.
    Steps:

    1.) Just upload the program and do the wiring stuff.

    Problems Encountered:

    As i said earlier that this module is so sensitive. So this is my solution to the problem.

    As you can see I used an electrical tape so that it wont touch my skin and for a constant value of the analogRead of the module.

    Another problem that I encountered that the keypad module should be powered to another arduino. Which means you should isolate the VCC and Ground of the keypad module to another Arduino.

    Set-up the Hardware:

    Output = A0 of the Arduino for LCD

    VCC = VCC of Arduino

    Gnd = Gnd of the Arduino

    NOTE!! You should connect the VCC and Gnd to the other arduino!

    Code:

    For Simple Math Calculator:

     

    #include <LiquidCrystal.h>
    LiquidCrystal lcd(7,8,9,10,11,12);
    
    int a;
    int b;
    char op;
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.clear();
     
    }
    
    void loop() {
    
      
    
      lcd.setCursor(0,0);
      Serial.println("Enter the First number");
      lcd.print("Enter the First");
      lcd.setCursor(0,1);
      lcd.print("Number: ");
      
      while (analogRead(A0) <= 100); // wait till the user has entered something 
      a = keypad();
      Serial.println(a);
      lcd.clear();
      lcd.print(a);
      delay(500);
      lcd.clear();
       // treat what the user has entered as an Integer and read the whole number
      Serial.println("Enter Operation");
      lcd.print("Enter Operation: ");
      while(analogRead(A0) <= 100);
      char op = operations();
      Serial.println(op);
      lcd.clear();
      lcd.print(op);
      delay(500);
      lcd.clear();
      
      Serial.println("Enter the second number: ");
      lcd.print("Enter the second");
      lcd.setCursor(0,1);
      lcd.print("Number");
      while (analogRead(A0) <= 100);
      b = keypad();
      Serial.println(b);
      lcd.clear();
      lcd.print(b);
      delay(500);
      float totalresult = arith(op);
      Serial.println(totalresult);
      lcd.clear();
      lcd.print(totalresult);
      delay(1000);
      
    }
    
    int keypad(){
     int pinValue = analogRead(A0);
    
     if (pinValue >= 1020){
      return 7;
     } else if (pinValue >= 910){
      return 8;
     } else if (pinValue >= 825){
      return 9;
     } else if (pinValue >= 675){
      return 4;
     } else if (pinValue >= 620){
      return 5;
     } else if (pinValue >= 580){
      return 6;
     }else if (pinValue >= 490){
      return 1;
     } else if (pinValue >= 470){
      return 2;
     } else if (pinValue >= 450){
      return 3;
     } else if (pinValue >= 290){
      return 0;
     }
    }
    
    char operations(){
      int pinValue = analogRead(A0);
      
        if (pinValue >= 750){
        return '+';
       }else if (pinValue >= 550){
        return '-';
       }else if (pinValue >= 420){
        return '*';
       }
       else if (pinValue >= 350){
        return '^';
       }else if (pinValue >= 248){
        return '%';
       }else if (pinValue >= 200){
        return '/';
       }
    }
    
    float arith(char perform) {
      float result1;
      switch (perform){
        case '+' :
          return result1 = a + b;
          break; 
        case '-' :
          return result1 = a - b; 
          break; 
        case '*' :
          return result1 = a * b;
          break; 
        case '/' : 
          return result1 = a / b; 
          break; 
        case '^' :
        return result1 = pow(a,b);
        break;
        case '%' :
        return result1 = a % b;
        break;
        default : 
          Serial.println("Invalid Calculation! ");
        }
    }

     

    For Code Lock Relay:

     

    #include <LiquidCrystal.h>
    LiquidCrystal lcd(7,8,9,10,11,12);
    
    //Variable Declaration
    int relay =13;
    int password[4] = {1,2,3,4};
    int tempPassword[4];
    void setup(){
      pinMode(relay,OUTPUT);
      Serial.begin(9600);
      lcd.begin(16,2);
      lcd.clear();
    }
    
    void loop(){
        lcd.setCursor(0,0);
        Serial.println("Select:\nPress 1 for Store Password.\nPress 2 for Verify Password ");
        lcd.print("Press1 StorePass");
        lcd.setCursor(0,1);
        lcd.print("Press2 VerifPass");
        
        while(analogRead(A0) <= 100);
        int choice = keypad(analogRead(A0));
        delay(250);
        if(choice == 1){
          storePassword();
          delay(100);  
        }else if(choice == 2){
         if(verifyPassword() ){
            Serial.println("Password Matched !");
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("PasswordMatched!");
            digitalWrite(relay,HIGH);
            delay(1500);
            digitalWrite(relay,LOW);
            delay(1500);
         }else{
            Serial.println("WrongPassword!");
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("WrongPassword!");
            delay(2000);
        }
        }else{
          Serial.println("Please Select 1 or 2.");
        }
    
        for(int i = 0; i<4; i++){
          Serial.println(password[i]);
          
        }
    
        
    
        
    }
    
    boolean storePassword() {
      delay(100);
      for(int i = 0; i<4; i++){ //4 loops. Stores the password in the array variable password.
        Serial.println("Please Enter Digit " + String(i+1));
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Digit" + String(i+1));
        while(analogRead(A0) <= 100);
        password[i] = keypad(analogRead(A0));
        delay(300);
        Serial.println(password[i]);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(password[i]);
        delay(1000);
      }
      Serial.println("Password has been saved !");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Password Saved!");
      delay(2000);
    }
    
    int verifyPassword(){
      int val = digitalRead(relay);  
      delay(100);
      for(int i = 0; i<4; i++){ //4 loops. Stores the password in the array variable tempPassword.
        Serial.println("Please Enter Digit " + String(i+1));
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Digit" + String(i+1));
        while(analogRead(A0) <= 100);
        tempPassword[i] = keypad(analogRead(A0));
        delay(300);
        Serial.println(tempPassword[i]);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(tempPassword[i]);
        delay(1000);
      }
    
      for(int i = 0; i<4; i++){ //4 loops. Checks whether the password is similar. 
        if(password[i] != tempPassword[i]){
          return false;
        }  
      }
      return true;
    }
    
    //Default Keypad Stuff
    int keypad(int pressedButton){
     int pinValue = analogRead(A0);
    
     if (pinValue >= 1020){
      return 7;
     } else if (pinValue >= 910){
      return 8;
     } else if (pinValue >= 825){
      return 9;
     } else if (pinValue >= 675){
      return 4;
     } else if (pinValue >= 620){
      return 5;
     } else if (pinValue >= 580){
      return 6;
     } else if (pinValue >= 490){
      return 1;
     } else if (pinValue >= 470){
      return 2;
     } else if (pinValue >= 450){
      return 3;
     } else if (pinValue >= 290){
      return 0;
     }
    }

     

    Code Breakdown:

    For the first activity:

     

    int a;
    int b;
    char op;

    declaring that the values for a and be is an integer and the op is a character.

    Serial.begin(9600);

    Initiating the Serial Monitor.

    Serial.begin(9600);

    and the LCD.

    lcd.setCursor(0,0);
    Serial.println("Enter the First number");
    lcd.print("Enter the First");
    lcd.setCursor(0,1);
    lcd.print("Number: ");
    
    while (analogRead(A0) <= 100);

    In this line the Serial Monitor will ask you the first number you want to operate. If you have pressed the button and if the analogRead(A0) will be grater than 100 then it will go to the next loop.

    Serial.println("Enter Operation");
      lcd.print("Enter Operation: ");
      while(analogRead(A0) <= 100);
      char op = operations();
      Serial.println(op);
      lcd.clear();
      lcd.print(op);
      delay(500);
      lcd.clear();

    The Serial Monitor will ask you the operation depending on what you would like. Again if the AnalogRead(A0) will be grater than 100 and if you pressed a button in the keypad it will go to the next loop of the code.

    Serial.println("Enter the second number: ");
      lcd.print("Enter the second");
      lcd.setCursor(0,1);
      lcd.print("Number");
      while (analogRead(A0) <= 100);
      b = keypad();
      Serial.println(b);
      lcd.clear();
      lcd.print(b);
      delay(500);
      float totalresult = arith(op);
      Serial.println(totalresult);
      lcd.clear();
      lcd.print(totalresult);
      delay(1000);

    This code will ask you the second number to be operated. If you pressed the button and analogRead(A0) will be grater than 100 then it will go to the next loop.

    int keypad(){
     int pinValue = analogRead(A0);
    
     if (pinValue >= 1020){
      return 7;
     } else if (pinValue >= 910){
      return 8;
     } else if (pinValue >= 825){
      return 9;
     } else if (pinValue >= 675){
      return 4;
     } else if (pinValue >= 620){
      return 5;
     } else if (pinValue >= 580){
      return 6;
     }else if (pinValue >= 490){
      return 1;
     } else if (pinValue >= 470){
      return 2;
     } else if (pinValue >= 450){
      return 3;
     } else if (pinValue >= 290){
      return 0;
     }
    }
    
    char operations(){
      int pinValue = analogRead(A0);
      
        if (pinValue >= 750){
        return '+';
       }else if (pinValue >= 550){
        return '-';
       }else if (pinValue >= 420){
        return '*';
       }
       else if (pinValue >= 350){
        return '^';
       }else if (pinValue >= 248){
        return '%';
       }else if (pinValue >= 200){
        return '/';
       }
    }
    
    float arith(char perform) {
      float result1;
      switch (perform){
        case '+' :
          return result1 = a + b;
          break; 
        case '-' :
          return result1 = a - b; 
          break; 
        case '*' :
          return result1 = a * b;
          break; 
        case '/' : 
          return result1 = a / b; 
          break; 
        case '^' :
        return result1 = pow(a,b);
        break;
        case '%' :
        return result1 = a % b;
        break;
        default : 
          Serial.println("Invalid Calculation! ");
        }
    }

    This are the function of each buttons. As we did the first activity to find the analog values for each button this is the ouput of that. For example in fourth line of the function if the analogRead(A0) which we assigned as a variable pinValue will be less than or equal to 1020 it will function as “7”.

    For the Code Relay Switch:

     

    int password[4] = {1,2,3,4};
    int tempPassword[4];

    Declaring password[] array and tempPassword[] array as an integer.

    Serial.println("Select:\nPress 1 for Store Password.\nPress 2 for Verify Password ");
        lcd.print("Press1 StorePass");
        lcd.setCursor(0,1);
        lcd.print("Press2 VerifPass");
        
        while(analogRead(A0) <= 100);
        int choice = keypad(analogRead(A0));
        delay(250);
        if(choice == 1){
          storePassword();
          delay(100);  
        }else if(choice == 2){
         if(verifyPassword() ){
            Serial.println("Password Matched !");
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("PasswordMatched!");
            digitalWrite(relay,HIGH);
            delay(1500);
            digitalWrite(relay,LOW);
            delay(1500);
         }else{
            Serial.println("WrongPassword!");
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("WrongPassword!");
            delay(2000);
        }
        }else{
          Serial.println("Please Select 1 or 2.");
        }
    
        for(int i = 0; i<4; i++){
          Serial.println(password[i]);
          
        }

    This will initiate all the process in the program. It will as you to choose between 2 modes. If you pressed the button 1 then it will go to the function storePassword(); and if you pressed the button 2 it will go the function verifyPassword and if verifyPassword is achieve the Serial Monitor will print “Password Match” and Switch the relay on for 2 seconds and off after 2 seconds, if not will print “Wrong Password!”.

    boolean storePassword() {
      delay(100);
      for(int i = 0; i<4; i++){ //4 loops. Stores the password in the array variable password.
        Serial.println("Please Enter Digit " + String(i+1));
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Digit" + String(i+1));
        while(analogRead(A0) <= 100);
        password[i] = keypad(analogRead(A0));
        delay(300);
      }
      Serial.println("Password has been saved !");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Password Saved!");
      delay(2000);
    }

    This is the function storePassword(), as you can see it has an increment. The process will be the Serial monitor will ask to Pressed the button for times because of increment from 0 to 3. Starting from 0,1,2,3 and if goes to four it will stop because of the less than 4.

    Next if you have pressed the button 4 times, what ever you have pressed will be store in the password array because of the line

    password[i] = keypad(analogRead(AO));

    and will print in the Serial Monitor “Password has been saved!”

    int verifyPassword(){
      delay(100);
      for(int i = 0; i<4; i++){ //4 loops. Stores the password in the array variable tempPassword.
        Serial.println("Please Enter Digit " + String(i+1));
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Enter Digit" + String(i+1));
        while(analogRead(A0) <= 100);
        tempPassword[i] = keypad(analogRead(A0));
        delay(300);
      }

    The function verifyPassword(), its process is the same with the function storePassword but on this time it will check if the password[i] array is equal to tempPassword[i] array.

    Conclusion:

    This project was a bit challenging for me because I did not see similar projects with this in the internet. I gave my extra time and extra effort to finish my project and because of that I was able to learned new things. I learned and understand deeply how array works. I’m hoping also that you have learned something from this tutorial. I know that this tutorial is a bit easy but you can use this as a reference to the complicated projects.

     

     

    References:

    https://create.arduino.cc/projecthub/Guitarman1/displaying-sensor-values-on-lcd-c0c44f

    The post Simple Math Calculator and PassCode Relay Switch using Resistive 4×4 Keypad Module appeared first on CreateLabz.

    4x44x4 keypadAnalogAnalog keypadArduinoButtonCalculatorKeypadKnowledgebaseLcdRelayResistive keypad

    Leave a comment

    All comments are moderated before being published