Lesson 30 Password Lock

Share for us

Introduction

After having learnt so many modules separately, let’s use these modules together to make some funny interactive works! In this lesson, we will use an I2C LCD1602, a relay module, a potentiometer and a keypad to assemble a simple password lock. It is built based on a SunFounder microcontroller and can be applied in security doors.

Components

– 1* SunFounder Uno board

– 1 * USB data cable

– 1 * Relay module

– 1 * I2C LCD1602

– 1 * Keypad

– Several jumper wires

– 1 * 3-Pin anti-reverse cable

– 1 * Breadboard

Experimental Principle

Set a password (for example 123456) by programming. The I2C LCD1602 displays “Welcome!” after power on. At this point, the normally open contact of the relay is disconnected, and the indicator LED on the relay module keeps off. Press the asterisk “*” key to input the password, and then press “#”.

If the password is correct, the normally open contact of the relay will be closed and the indicator light will be on. The I2C LCD1602 will display “Input Correctly” “Please Come In”. If you input other contents, the I2C LCD1602 will display “Input Error!” “Please Again”, and the relay will keep the initial state. Two seconds later, the I2C LCD1602 displays “Welcome!”.

NOTE: Through programming, we’ve set the four keys in the first row (with pins at the top as shown in the figure above) as 1, 2, 3, 4; those in the second row as 5, 6, 7, 8; in the third as 9, A, B, C; and in the fourth as D, *, 0, #.

Experimental Procedures

Step 1: Build the circuit

The wiring between the I2C LCD1602 and the SunFounder board:

I2C LCD1602SunFounder Uno
GNDGND
VCC5v
SDAA4
SCLA5

The wiring between the relay module and the SunFounder board:

Relay ModuleSunFounder Uno
SIG3
VCC5V
GNDGND

The wiring between the keypad and the SunFounder board:

KeypadSunFounder Uno
X18
X29
X310
X411
Y14
Y25
Y36
Y47

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Note: Here you need to add a library. Refer to the description in Lesson 4 previously in the manual.

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, the I2C LCD1602 will display Welcome! after power on. At this point, the indicator LED on the relay keeps off. When you press “*” key, it will prompt “Input Your Code:”. If you enter “123456” and press “#” key to confirm, the indicator LED on the relay module will light up. The I2C LCD1602 will display “Input Correctly” “Please Come In”. Two seconds later, “Welcome!” will be displayed on the I2C LCD1602. But if you input other contents, it will display “Input Error!” “Please Again”and the relay will keep the initial state. Two seconds later, the I2C LCD1602 displays “Welcome!”.

Code

/*******************************************************
* name:Password Lock
* function: the I2C LCD1602 will display “Welcome!” after power on.
* At this point, the indicator LED on the relay keeps off.
* When you press “*” key, it will prompt “Input Your Code:”. If you enter “123456” and press “#” key to confirm,
* the indicator LED on the relay module will light up. The I2C LCD1602 will display “Input Correctly” “Please Come In”.
* Two seconds later, “Welcome!”
*********************************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27,16,2);
#define relayPin 3 //relay attach to pin 3
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] =
{
{ ‘1’,’2′,’3′,’4′ },
{ ‘5’,’6′,’7′,’8′ },
{ ‘9’,’A’,’B’,’C’ },
{ ‘D’,’*’,’0′,’#’ }
};
byte rowPins[ROWS] = { 4, 5, 6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 8, 9, 10, 11}; //connect to the column pinouts of the keypad
int pos = 0;
char secretCode[6] = { ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’};
char inputCode[6] = { ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’};//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);void setup()
{
lcd.init(); //initialize lcd
lcd.backlight(); //turn the backlight
pinMode(relayPin, OUTPUT); //set the relay as OUTPUT
//Serial.begin(9600);
lcd.setCursor(0,0);
lcd.print(” Welcome! “);
delay(2000);}void loop()
{
readKey();
}void readKey()
{
int correct = 0;
int i;
char customKey = customKeypad.getKey();//get the key value
if (customKey)
{
switch(customKey)
{
case ‘*’: //if press in “*” ,then print “Input Your Code:”
pos = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Input Your Code:”);
break;
case ‘#’: //if press in “#” ,then see the password whether “1 2 3 4 5 6”
for(i = 0; i < 6; i++)
{
if(inputCode[i] == secretCode[i])
{
correct ++;
}
}
if(correct == 6)//if it right
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Input correctly!”);//print “Input correctly!”
lcd.setCursor(0, 1);
lcd.print(” Please Come In “); //print “Please Come In”
digitalWrite(relayPin, LOW); //relay connected
delay(2000);
lcd.clear(); //clear the lcd
lcd.setCursor(0,0);
lcd.print(” Welcome! “);
digitalWrite(relayPin, HIGH);
}
else
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(” Input Error! “);
lcd.setCursor(0, 1);
lcd.print(” Please Again “);
digitalWrite(relayPin, HIGH);
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(” Welcome! “);
}
break;
default:
inputCode[pos] = customKey;
lcd.setCursor(pos,1);
lcd.print(inputCode[pos]);
pos ++;
}
}
}