.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _4.1.14_py_pi5:
4.1.11 Password Lock
================================
Introduction
-------------
In this project, we will use a keypad and a LCD to make a combination
lock. The LCD will display a corresponding prompt for you to type your
password on the Keypad. If the password is input correctly, “Correct”
will be displayed.
On the basis of this project, we can add additional electronic
components, such as buzzer, LED and so on, to add different experimental
phenomena for password input.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../python_pi5/img/4.1.14_password_lock_list.png
:width: 800
:align: center
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_i2c_lcd`
- |link_i2clcd1602_buy|
* - :ref:`cpn_keypad`
- \-
Schematic Diagram
------------------
============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO18 Pin 12 1 18
GPIO23 Pin 16 4 23
GPIO24 Pin 18 5 24
GPIO25 Pin 22 6 25
GPIO17 Pin 11 0 17
GPIO27 Pin 13 2 27
GPIO22 Pin 15 3 22
SPIMOSI Pin 19 12 10
SDA1 Pin 3
SCL1 Pin 5
============ ======== ======== ===
.. image:: ../python_pi5/img/4.1.14_password_lock_schematic.png
:align: center
Experimental Procedures
-------------------------
**Step 1:** Build the circuit.
.. image:: ../python_pi5/img/4.1.14_password_lock_circuit.png
**Step 2:** Change directory.
.. raw:: html
.. code-block::
cd ~/raphael-kit/python-pi5
**Step 3:** Run.
.. raw:: html
.. code-block::
sudo python3 4.1.14_PasswordLock_zero.py
After the code runs, keypad is used to input password: 1984. If the
“CORRECT” appears on LCD1602, there is no wrong with the password;
otherwise, “WRONG KEY” will appear.
.. note::
* If you get the error ``FileNotFoundError: [Errno 2] No such file or directory: '/dev/i2c-1'``, you need to refer to :ref:`i2c_config` to enable the I2C.
* If you get ``ModuleNotFoundError: No module named 'smbus2'`` error, please run ``sudo apt install python3-smbus2``.
* If the error ``OSError: [Errno 121] Remote I/O error`` appears, it means the module is miswired or the module is broken.
* If the code and wiring are fine, but the LCD still does not display content, you can turn the potentiometer on the back to increase the contrast.
.. warning::
If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc`
**Code**
.. note::
You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python-pi5``. After modifying the code, you can run it directly to see the effect.
.. raw:: html
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import DigitalOutputDevice, Button
from time import sleep
import LCD1602
class Keypad:
def __init__(self, rows_pins, cols_pins, keys):
"""
Initialize the Keypad with specified row and column pins and keys.
:param rows_pins: List of GPIO pins for the rows.
:param cols_pins: List of GPIO pins for the columns.
:param keys: List of keys in the keypad layout.
"""
self.rows = [DigitalOutputDevice(pin) for pin in rows_pins] # Row pins setup
self.cols = [Button(pin, pull_up=False) for pin in cols_pins] # Column pins setup
self.keys = keys # Keypad key layout
def read(self):
"""
Read and return a list of keys that are currently pressed.
:return: List of pressed keys.
"""
pressed_keys = []
for i, row in enumerate(self.rows):
row.on() # Activate current row
for j, col in enumerate(self.cols):
if col.is_pressed:
index = i * len(self.cols) + j
pressed_keys.append(self.keys[index])
row.off() # Deactivate row after checking
return pressed_keys
# Password verification setup
LENS = 4
password = ['1', '9', '8', '4'] # Preset password
testword = ['0', '0', '0', '0'] # User input storage
keyIndex = 0 # Index for input keys
def check():
"""
Check if the entered password matches the preset password.
:return: 1 if match, 0 otherwise.
"""
for i in range(LENS):
if password[i] != testword[i]:
return 0
return 1
def setup():
"""
Setup the keypad and LCD display.
"""
global keypad, last_key_pressed
# Pin configuration for keypad
rows_pins = [18, 23, 24, 25]
cols_pins = [10, 22, 27, 17]
keys = ["1", "2", "3", "A",
"4", "5", "6", "B",
"7", "8", "9", "C",
"*", "0", "#", "D"]
# Initialize keypad and LCD
keypad = Keypad(rows_pins, cols_pins, keys)
last_key_pressed = []
LCD1602.init(0x27, 1) # Initialize LCD
LCD1602.clear()
LCD1602.write(0, 0, 'WELCOME!')
LCD1602.write(2, 1, 'Enter password')
sleep(2)
def loop():
"""
Main loop for handling keypad input and password verification.
"""
global keyIndex, LENS, keypad, last_key_pressed
while True:
pressed_keys = keypad.read()
if pressed_keys and pressed_keys != last_key_pressed:
if keyIndex < LENS:
LCD1602.clear()
LCD1602.write(0, 0, "Enter password:")
LCD1602.write(15 - keyIndex, 1, pressed_keys[0])
testword[keyIndex] = pressed_keys[0]
keyIndex += 1
if keyIndex == LENS:
if check() == 0:
LCD1602.clear()
LCD1602.write(3, 0, "WRONG KEY!")
LCD1602.write(0, 1, "please try again")
else:
LCD1602.clear()
LCD1602.write(4, 0, "CORRECT!")
LCD1602.write(2, 1, "welcome back")
keyIndex = 0 # Reset key index after checking
last_key_pressed = pressed_keys
sleep(0.1)
try:
setup()
loop()
except KeyboardInterrupt:
LCD1602.clear() # Clear LCD display on interrupt
**Code Explanation**
#. The script imports classes for managing digital output devices and buttons from the gpiozero library. It also imports the sleep function from the time module, enabling the addition of delays in the script's execution. Additionally, the LCD1602 library is imported for controlling the LCD1602 display.
.. code-block:: python
#!/usr/bin/env python3
from gpiozero import DigitalOutputDevice, Button
from time import sleep
import LCD1602
#. Defines a custom class for managing the keypad. It initializes the keypad with specified row and column pins and provides a ``read`` method to detect pressed keys.
.. code-block:: python
class Keypad:
def __init__(self, rows_pins, cols_pins, keys):
"""
Initialize the Keypad with specified row and column pins and keys.
:param rows_pins: List of GPIO pins for the rows.
:param cols_pins: List of GPIO pins for the columns.
:param keys: List of keys in the keypad layout.
"""
self.rows = [DigitalOutputDevice(pin) for pin in rows_pins] # Row pins setup
self.cols = [Button(pin, pull_up=False) for pin in cols_pins] # Column pins setup
self.keys = keys # Keypad key layout
def read(self):
"""
Read and return a list of keys that are currently pressed.
:return: List of pressed keys.
"""
pressed_keys = []
for i, row in enumerate(self.rows):
row.on() # Activate current row
for j, col in enumerate(self.cols):
if col.is_pressed:
index = i * len(self.cols) + j
pressed_keys.append(self.keys[index])
row.off() # Deactivate row after checking
return pressed_keys
#. Sets up the password verification system. ``LENS`` defines the length of the password. ``password`` is the preset correct password, while ``testword`` is used to store the user's input. ``keyIndex`` tracks the current position in the user's input.
.. code-block:: python
# Password verification setup
LENS = 4
password = ['1', '9', '8', '4'] # Preset password
testword = ['0', '0', '0', '0'] # User input storage
keyIndex = 0 # Index for input keys
#. Function to compare the entered password (``testword``) with the preset password (``password``) and return the result.
.. code-block:: python
def check():
"""
Check if the entered password matches the preset password.
:return: 1 if match, 0 otherwise.
"""
for i in range(LENS):
if password[i] != testword[i]:
return 0
return 1
#. Initializes the keypad and LCD display. Displays a welcome message and instructions for entering the password.
.. code-block:: python
def setup():
"""
Setup the keypad and LCD display.
"""
global keypad, last_key_pressed
# Pin configuration for keypad
rows_pins = [18, 23, 24, 25]
cols_pins = [10, 22, 27, 17]
keys = ["1", "2", "3", "A",
"4", "5", "6", "B",
"7", "8", "9", "C",
"*", "0", "#", "D"]
# Initialize keypad and LCD
keypad = Keypad(rows_pins, cols_pins, keys)
last_key_pressed = []
LCD1602.init(0x27, 1) # Initialize LCD
LCD1602.clear()
LCD1602.write(0, 0, 'WELCOME!')
LCD1602.write(2, 1, 'Enter password')
sleep(2)
#. The main loop for handling keypad input and password verification. It updates the LCD display based on the entered password and provides feedback if the password is correct or incorrect.
.. code-block:: python
def loop():
"""
Main loop for handling keypad input and password verification.
"""
global keyIndex, LENS, keypad, last_key_pressed
while True:
pressed_keys = keypad.read()
if pressed_keys and pressed_keys != last_key_pressed:
if keyIndex < LENS:
LCD1602.clear()
LCD1602.write(0, 0, "Enter password:")
LCD1602.write(15 - keyIndex, 1, pressed_keys[0])
testword[keyIndex] = pressed_keys[0]
keyIndex += 1
if keyIndex == LENS:
if check() == 0:
LCD1602.clear()
LCD1602.write(3, 0, "WRONG KEY!")
LCD1602.write(0, 1, "please try again")
else:
LCD1602.clear()
LCD1602.write(4, 0, "CORRECT!")
LCD1602.write(2, 1, "welcome back")
keyIndex = 0 # Reset key index after checking
last_key_pressed = pressed_keys
sleep(0.1)
#. Runs the setup and enters the main loop. Allows for a clean exit from the program using a keyboard interrupt (Ctrl+C), clearing the LCD display.
.. code-block:: python
try:
setup()
loop()
except KeyboardInterrupt:
LCD1602.clear() # Clear LCD display on interrupt