Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to design an OTP-based door lock

By Nikhil Agnihotri May 28, 2024

Today, mobile-based authentication to verify a person’s identity is common. Online service providers often use it for access to services, approval of transactions, or to change a user’s credentials. 

Imagine using a similar mobile-based authentication to enter your home, office, or any private space. It’s possible. In this project, we’ll design a mobile-based access system for access to a private room. This device controls the room’s door using a relay. The door only opens if the one-time password (OTP) sent via a registered mobile number is confirmed. 

This device is built using Arduino Mega. It uses an IR sensor and a SIM900 modem to send an SMS containing a random OTP. The user can enter the OTP through a keypad. The system also has an OLED screen attached that serves as the user interface and guides users in accessing the room. 

Components required

  1. Arduino Mega 2560 x1
  2. SIM900 GSM modem x1
  3. IR sensors x1
  4. SSD1306 OLED x1
  5. Relay/Electronic lock x1
  6. Connecting or Dupont wires

Circuit connections
To build this device, the SIM900 modem, SSD1306 OLED, IR sensor, 4×4 keypad, and relay or electronic lock are interfaced with Arduino Mega — which offers enough GPIOs to work with all these components. 

To interface the SIM900, the modem’s GND, TX, and RX pins are connected to Arduino’s ground, RX1, and TX1 pins. The keypad is connected to Arduino’s GPIO 22, 24, 28, 30, 32, 34, and 36. 

The seven-pin SSD1306 OLED module is used and connected to the Arduino as follows.

The IR sensor is connected to Arduino’s GPIO 8. The relay or electronic lock is interfaced at Arduino’s GPIO 50. The device’s circuit connections are shown below.

Arduino sketch
After making the circuit connections, upload the following sketch to Arduino Mega. Remember to replace the registered mobile number you want to receive the OTP in the sketch.

How it works
When the device is plugged in after installation, it initializes the display screen and sends the AT commands to the SIM900 modem to check network strength. The message “OTP-based Door Lock” is displayed on the OLED screen. 

When a user is within the vicinity of the authenticated door, an OTP is generated, which is a random number between 1000 and 9999. The OTP is sent to the user’s registered mobile number. The OLED’s interface prompts the user to enter the OTP, which can be entered via the 4×4 keypad on the device. 

If the passcode entered by the user is the same as the OTP that was sent to the registered mobile number, access is granted. The relay/electronic lock unlocks the door and keeps it open for ten seconds. Once the relay/electronic lock is operated, the door is locked again. 

If the passcode entered by the user does not match the OTP sent to the registered mobile number, access is denied. The microcontroller does not trigger the relay/electronic lock, and the door remains shut. 

The code
The code begins by importing the Keypad.h, SPI.h, Wire.h, Adafruit_GFX.h, and Adafruit_SSD1306.h libraries. The Keypad.h library handles input from the 4×4 membrane keypad. The SPI.h, Wire.h, Adafruit_GFX.h, and Adafruit_SSD1306.h libraries work with the SSD1306 OLED. This is followed by a declaration of variables for the OLED module’s pin connections and the related constants. 

An object of the Adafruit_SSD1306 class is instantiated to operate the OLED display. The variables for the pin connections and key values of the keypad are defined. An object of the Keypad class is instantiated. The variables defining the pin connections for the relay and IR sensor are declared. The variables to hold the OTP are also declared. 

The user-defined function sendATCommand() is used to send the AT commands to the SIM900 modem, connected to Arduino’s UART1 port. The user-defined function printText() is defined to print text on the OLED screen. The user-defined function SendSMS() is defined to send the generated OTP to the user’s registered mobile number. The user-defined function getOTP() is defined to parse the keypad input and match the passcode of the OTP, which was sent to the registered mobile number. 

In the setup() function, the baud rate for the serial debug is set to 9600 bps. The display is initialized. The pin connecting the IR sensor is set as the input and the pin connecting relay/electronic lock is set as the output. The baud rate for serial communication with the SIM900 modem is set to 9600 bps. The AT commands are sent to check the network’s strength.

In the loop() function, the text “OTP-based Door Lock” is printed on the OLED screen. If the IR sensor detects the user at the door, the controller generates a four-digit OTP, which is a random number between 1000 and 9999. The display screen indicates that an OTP for granting access has been sent to the registered mobile number. The OTP is sent by calling the SendSMS() function. The OLED screen is updated, prompting the user to enter the passcode sent to their registered mobile number. The OTP is verified by calling the getOTP() function. Access is granted if the user-entered passcode matches the OTP.

You may also like:


  • How to design a security system that sounds like a…

  • How to design an IoT-based smart alarm

  • How to design a smartphone-operated door lock

  • How to build a WhatsApp-notifying home security system

  • What are the top open-source software systems for home automation?

Filed Under: Arduino Projects, Electronic Projects, Video
Tagged With: amoled, Arduino, arduinomega, electroniclock, electronicproject, irsensor, mobile, OTP
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe