Push Button Combinational lock using arduino

Push Button Combination Lock Using Arduino

Introduction

Combination locks have been there for decades and many products such as a briefcase, doors are protected by them. There is keypad module for Arduino that can help us to set combination password for various things, you may also use RFID cards to provide access and in this tutorial, I am going to explain how you can make push-button combination lock.

Working

The project will help us to set combination password and you can set a password by using 1-6 digits, When you will push the buttons, and the combination is matched the green LED will be turned on and if the combination is not matched the Red LED will indicate that the password is wrong.

Push Button Combination Lock Using Arduino

Hardware Required

  • Arduino Uno
  • Push Buttons x 6
  • Red Leds x 7
  • Green Leds x 1
  • Jumper wires
  • Breadboard

Interfacing of push buttons

The push buttons can be interfaced by Arduino using pull-down configuration i.e. button will give 5V when pressed and 0V when released and pull up configuration i.e. when the button is pressed it will give 0V and when released it will give 5V. We are using pull up configuration and we are not using any external resistor instead we are using Arduino internal pull-up resistor.

Pull up v/s pull down configuration
Pull up v/s pull-down configuration

change combination lock password

You can change combination password from Arduino code, open the code and find the following line,

int code[] = {6,5,5,4,3,2};

and change the sequence, for now, you will have to press 6th button followed by 5, again 5, 4, 3 and then 2 and your Green LED should be on.

Make circuit and code

The Circuit diagram and code can be found below, make circuit open Arduino IDE and don’t forget to select port and board and upload the code.

Push Button Combination Lock Using Arduino Circuit diagram
Push Button Combination Lock Using Arduino Circuit diagram

const int button1 = 2; //first button is on pin 8
const int button2 = 3; //second is on pin 9
const int button3 = 4; //third is pin 10
const int button4 = 5; //fourth is pin 11
const int button5 = 6; //third is pin 10
const int button6 = 7; //fourth is pin 11
const int LED[] = {14,15,16,17,18,19};

const int Red = 8; //red LED is on pin 4
const int greenLed = 9; //green LED is pin 12
void checkEntered1(int button);

int code[] = {6,5,5,4,3,2}; //the desired code is entered in this array,
                        //separated by commas

int entered[7]; //create a new empty array for the code entered by
                //the user (has 4 elements)

void setup(){ //run once at sketch startup
  Serial.begin(9600); //begin Serial

  pinMode(button1, INPUT_PULLUP); //button 1 is an input
  pinMode(button2, INPUT_PULLUP); //button 2 is an input
  pinMode(button3, INPUT_PULLUP); //button 3 is an input
  pinMode(button4, INPUT_PULLUP); //button 4 is an input
  pinMode(button5, INPUT_PULLUP); //button 3 is an input
  pinMode(button6, INPUT_PULLUP); //button 4 is an input

  pinMode(Red, OUTPUT); //the red LED is an output
  pinMode(greenLed, OUTPUT); // the green LED is an output
//  setupLights(); //run the setupLights routine
//  setupLights(); //run it again
 // delay(650); //delay (only for effect, :P not needed)
  digitalWrite(Red, LOW); //turn the red LED on
  for (int i = 0; i < 6;i++){ //work through numbers 0-3
    Serial.println(code[i]); //print each digit of the code
    Serial.println(entered[i]); //print each element of the entered[]
                                //array (this was for me to check that it 
                                //started at 0
    pinMode(LED[i],OUTPUT);
  }
}

void loop(){ //run repeatedly
  if (digitalRead(button1) == LOW){ //if button1 is pressed
    checkEntered1(1); //call checkEntered and pass it a 1
    
    delay(250);//wait, needed for correct functioning, otherwise
               //buttons are deemed to be pressed more than once
    
  }
  else if (digitalRead(button2) == LOW){ //if button2 is pressed
    checkEntered1(2); //call checkEntered1 and pass it a 2
    
    delay(250); //wait
    
  }
  else if (digitalRead(button3) == LOW){ //if button3 is pressed
    checkEntered1(3); //call checkEntered1 and pass it a 3
    
    delay(250); //wait
    
  }
  else if (digitalRead(button4) == LOW){ //if button4 is pressed
    checkEntered1(4); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
    else if (digitalRead(button5) == LOW){ //if button4 is pressed
    checkEntered1(5); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
    else if (digitalRead(button6) == LOW){ //if button4 is pressed
    checkEntered1(6); //call checkEntered1 and pass it a 4
    
    delay(250); //wait
    
  }
  

}

void checkEntered1(int button){ //check the first element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[0] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered2(button); //move on to checkEntered2, passing it "button"
  }
  
  else if(entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[0] = button; //set the first element as the button that has been pressed
    Serial.print("1: ");Serial.println(entered[0]); //for debugging
  }
  
}

void checkEntered2(int button){ //check the second element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered3(button); //move on to checkEntered3, passing it "button"
  }
  
  else if(entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[1] = button; //set the second element as the button that has been pressed
    Serial.print("2: ");Serial.println(entered[1]); //for debugging
  }
  
}

void checkEntered3(int button){  //check the third element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered4(button); //move on to checkEntered4, passing it "button"
  }
  
  else if (entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[2] = button; //set the third element as the button that has been pressed
    Serial.print("3: ");Serial.println(entered[2]); //for debugging
  }
  
}

void checkEntered4(int button){  //check the third element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[3] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered5(button); //move on to checkEntered4, passing it "button"
  }
  
  else if (entered[3] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[3] = button; //set the third element as the button that has been pressed
    Serial.print("4: ");Serial.println(entered[3]); //for debugging
  }
  
}


void checkEntered5(int button){  //check the third element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[4] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered6(button); //move on to checkEntered4, passing it "button"
  }
  
  else if (entered[4] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[4] = button; //set the third element as the button that has been pressed
    Serial.print("5: ");Serial.println(entered[4]); //for debugging
  }
  
}

void checkEntered6(int button){ //check the fourth element of the entered[] array
  digitalWrite(LED[button-1],HIGH);
  if (entered[5] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    entered[5] = button; //set the final element as the button that has been pressed
    Serial.print("6: ");Serial.println(entered[5]); //for debugging
    delay(100); //allow time for processing
    compareCode(); //call the compareCode function
  }
}

void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
  for (int i = 0; i<6;i++){ //these three lines are for debugging
    Serial.println(entered[i]);
  }
  if ((entered[0]==code[0]) && (entered[1]==code[1]) && (entered[2]==code[2]) && (entered[3]==code[3]) && (entered[4]==code[4])&& (entered[5]==code[5])){ //if all the elements of each array are equal
    digitalWrite(Red, LOW); // turn the red LED off
    digitalWrite(greenLed, HIGH); //turn the green LED on
    delay(1000); //wait for a bit
    digitalWrite(greenLed, LOW); //turn the green LED off


    
    for (int i = 0; i < 7; i++){ //this next loop is for debugging
      entered[i] = 0;
      
    }
   
    loop(); //return to loop() (not really necessary)
  }
  
  else { //if you (or the intruder) get the code wrong
    
    digitalWrite(Red,HIGH);
    delay(1000);
    digitalWrite(Red,LOW);
    Serial.println("Red OFF");
    for (int i = 0; i < 7; i++){ //this next loop is for debugging
      entered[i] = 0;
     
    }
    
  }
  close_all();
}



void close_all(){
digitalWrite(LED[0],LOW);
digitalWrite(LED[1],LOW);
digitalWrite(LED[2],LOW);
digitalWrite(LED[3],LOW);
digitalWrite(LED[4],LOW);
digitalWrite(LED[5],LOW);
}

    

VIDEO TUTORIAL:

Conclusion

SO that way you can use Arduino and push buttons as combination lock you can use that to secure your door or any other thing. Subscribing us will help you to keep updated with the latest such projects.

SO that way you can use Arduino and push buttons as combination lock you can use that to secure your door or any other thing. If you are interested in such projects please subscribe us and that would be beneficial for you.

if you need any help in this project you can contact us.

Please Subscribe us on youtube: www.youtube.com/c/highvoltages

Facebook: www.facebook.com/highvoltagestech 

Instagram: www.instagram.com/highvoltagestech

2 thoughts on “Push Button Combination Lock Using Arduino”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.