
In today’s life everything becomes simple and advanced, previously to lock something we used to have padlocks, combination locks. But due to the increase in theft and technology, new types of locks such as electronic locks, smart locks have been invented and people are widely using them to protect their property.
In this project, we will build a password-based door lock system by interfacing the ESP32 board with a 4×4 keypad to enter the password.
We use a sliding door opening or closing by horizontal translation thanks to a 5V DC motor.
With this project, we can build a security system that works with a password. We must enter a code to close or open the door.
L298N motor board
5V DC motor
connecting wires
We connect the 8 outputs of the keyboard to the 8 pins of the ESP32 card following this order: D2, D4, D5, D18, D19, D16, D15 and D23.
For the SSD1306 display we connect:
the SDA pin to the D21 pin of the ESP32 board
the SCL pin to the D22 pin of the ESP32 board
the GND pin to the GND pin of the ESP32 board
the VCC pin to the 5V pin of the ESP32 board
Connect pin 17 of the ESP32 board to the ENA pin of the L298N module.
Connect pin 3 of the ESP32 board to pin IN1 of the L298N module.
Connect pin 1 of the ESP32 board to pin IN2 of the L298N module.
Connect the GND pin of the ESP32 board to the GND pin of the L298N module.
Connect the GND pin of the ESP32 board to the (-) terminal of the 9V battery
Connect the 12V pin of the L298N module to the (+) terminal of the 9V battery
Connect the two motor terminals to the two pins OUT1 and OUT2 of the L298N module
Here is the micropython program which allows to open or close the door by the ESP32 card.
Note: the following two libraries must be imported: ssd1306 and dcmotor
esp32-door.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
from machine import UART from dcmotor import DCMotor from machine import Pin, PWM,I2C from time import sleep import ssd1306 import time import utime i2c = I2C(scl=Pin(22), sda=Pin(21)) #Init i2c oled=ssd1306.SSD1306_I2C(128,64,i2c,0x3c) oled.fill(0) oled.text('Code',10,10) oled.show() frequency = 15000 pin1 = Pin(3, Pin.OUT) pin2 = Pin(1, Pin.OUT) enable = PWM(Pin(17), frequency) dc_motor = DCMotor(pin1, pin2, enable) dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) KEY_UP = const(0) KEY_DOWN = const(1) keys = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']] # Pin names for keypad cols = [19,16,15,23] rows = [2,4,5,18] # set pins for rows as outputs row_pins = [Pin(pin_name, mode=Pin.OUT) for pin_name in rows] # set pins for cols as inputs col_pins = [Pin(pin_name, mode=Pin.IN, pull=Pin.PULL_DOWN) for pin_name in cols] def init(): for row in range(0,4): for col in range(0,4): row_pins[row].value(0) def scan(row, col): """ scan the keypad """ # set the current column to high row_pins[row].value(1) key = None # check for keypressed events if col_pins[col].value() == KEY_DOWN: key = KEY_DOWN if col_pins[col].value() == KEY_UP: key = KEY_UP row_pins[row].value(0) # return the key state return key print("starting") # set all the columns to low init() code="" while True: oled.fill(0) if (len(code)<4): for row in range(4): for col in range(4): key = scan(row, col) if key == KEY_DOWN: oled.text("code: ",10,0) print("Key Pressed", keys[row][col]) code=code+keys[row][col] oled.text(code, 55, 30) oled.show() last_key_press = keys[row][col] time.sleep_ms(500) if (code=="2886"): time.sleep(1) dc_motor.forward(80) # the motor turns to open the door time.sleep(1) dc_motor.stop() # stop le moteur code="" if (code=="0000"): time.sleep(1) dc_motor.backwards(80) # the motor rotates in the opposite direction to close the door time.sleep(1) dc_motor.stop() # stop le moteur code="" |
Ricardo 09-06-2222
It's in reality a nice and useful piece of info. I am happy that you shared this helpful information with us.