Introduction: Arduino Keypad Password Controlled LED Relay

About: Learn. Build. Create.

In this project I combined a couple of other projects I found online which simply turns on an LED with the correct password using a 4x4 keypad, relay and 9v battery. I will post a link to the other projects that inspired this one below.

Supplies

  • Arduino Uno
  • Breadboard
  • 19 Wires Total
  • (Left) 8 for the 4x4 matrix
  • (Middle) 8 standard jumper wires
  • (Right) 3 assorted wires. I modified a 9v battery wire to place the left pin in breadboard and I also modified a jumper wire by connecting 2 wires into one pin for the Arduino.
  • 4x4 Matrix Keypad
  • Relay 5v (SRD-5VDC-SL-C)
  • LCD Screen (QAPASS 16X2 - 4 pin)
  • 9V Battery
  • LED Basic
  • Resistor 330 Ohm

Step 1:


In steps 1 and 2 you will simply connect everything and make sure your keypad inputs are being displayed on the LCD screen.

Here you visually see all the connections above. Now that we have the physical setup, all we need now is the code. Before you can run this, you have to import the Keypad library and then once you import it, then you can enter it into your program. Once it’s entered into your program, you should see the line #include . If you do not see this, that means that the Keypad library has not been successfully put into your code and it won’t work.

This library is available via the Arduino IDE library manager. If you are using a modern IDE (1.6.2 or above), you can simply use the menu:

Sketch->Include Library->Manage Libraries… Then search for Keypad.

Once found, click on its entry and the install button will appear.

Step 2:

We use an I2C enabled LCD on the Arduino, you’ll need to install the LiquidCrystal I2C library . This library is nice because it includes most of the functions available in the standard LiquidCrystal library. To install it, download the ZIP file, then go to Sketch > Include Library > Add .ZIP Library.

The Wire library is needed to add support for I2C communication. It comes packaged with the Arduino IDE, so there’s no need to install it. But if for some reason it’s not installed on your system, go to Sketch > Include Library > Manage Libraries and search for “wire” to install it.

After above operations are completed, connect the Arduino board to your computer using the USB cable.

The green power LED (labelled PWR) should go on.

Open the Arduino IDE and choose corresponding board type and port type for you project. Once everything is prepared, upload this code into the Arduino:


#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>

const byte ROWS = 4;
const byte COLS = 4;

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

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(0x27, 16, 2);  

void setup(){
 lcd.backlight();
 lcd.init();
}

void loop(){
 char customKey = customKeypad.getKey();
 if (customKey){
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print(customKey);
 }
}



(Display Troubleshoot)

Each I2C device has an I2C address that it uses to accept commands or send messages. For Uno board, this address usually is 0x27. But sometimes the address might be changed 0x37,0x24 …., So let’s go and look for the one on your device.

Download ic2_scanner sketch zip file , then unzip and load it into Arduino IDE. By opening up the serial monitor in the upright corner, Arduino will scan the address range looking for a reply. Most Arduino board will show 0x27, however it be other number.

Step 3:

In this step you will now put everything together and upload the new code. After copying the schematic above, connect the Arduino board to your computer using the USB cable.

(NOTE: Picture 2 is a reference I used to connect the LED to the relay. I only copied the output connections from the relay to the LED and 9v battery.)

The green power LED (labelled PWR) should go on. Open the Arduino IDE and choose corresponding board type and port type for you project.

The following code will activate a 5V relay when the password is entered correctly, load it to your Arduino board.


#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>


#define Password_Length 8 


int signalPin = 12;


char Data[Password_Length]; 
char Master[Password_Length] = "123A456"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;


const byte ROWS = 4;
const byte COLS = 4;


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


byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);


LiquidCrystal_I2C lcd(0x27, 16, 2);  


void setup(){
  lcd.init(); 
  lcd.backlight();
  pinMode(signalPin, OUTPUT);
}


void loop(){


  lcd.setCursor(0,0);
  lcd.print("Enter Password:");


  customKey = customKeypad.getKey();
  if (customKey){
    Data[data_count] = customKey; 
    lcd.setCursor(data_count,1); 
    lcd.print(Data[data_count]); 
    data_count++; 
    }


  if(data_count == Password_Length-1){
    lcd.clear();


    if(!strcmp(Data, Master)){
      lcd.print("Correct");
      digitalWrite(signalPin, HIGH); 
      delay(5000);
      digitalWrite(signalPin, LOW);
      }
    else{
      lcd.print("Incorrect");
      delay(1000);
      }
    
    lcd.clear();
    clearData();  
  }
}


void clearData(){
  while(data_count !=0){
    Data[data_count--] = 0; 
  }
  return;
}


You can change the password on line 10 by replacing the 123A456 text with your own password:

char Master[Password_Length] = “123A456”;

The length of the password needs to be set on line 5:

#define Password_Length 8 


The password in the example above is only 7 characters long, but the password length is actually one greater than 7 because there is a null character added to the end of the string. For example, if your password is 5 characters long, you would enter 6 for the password length.

The output pin that activates the relay is defined on line 7:

int signalPin = 12;

Step 4:

That's it! It should look a little like this.

Here are some additional photos that show the connection to the LED, Breadboard, Relay and 9v battery.

*Make sure that you install the required libraries in Arduino IDE , also make sure you are set on the correct board in Tools> Board: "Arduino Uno". Also if your solder isn't sticking to the copper try a bit of Rosin flux paste.*

Step 5:

Original Projects that inspired this one, with more information, found here:

.

.

.

.

https://www.circuitbasics.com/how-to-set-up-a-keypad-on-an-arduino/

https://osoyoo.com/2017/09/13/arduino-lesson-4x4-matrix-keypad/

https://www.hackster.io/diy-hacking/arduino-keyless-door-lock-system-with-keypad-and-lcd-bcad2e