Touch Screen Solenoid Door Lock system using Arduino

Touch Screen Solenoid Door Lock project using Arduino
An Arduino based Touch Screen Door Lock project that contains a 2.4″ TFT Screen for entering the Personal Identification Number (PIN) and for visual display of the entered PIN. It also contains a 12V Solenoid Lock and Relay Module. The solenoid lock is used for locking and unlocking the door.
When a user enters a PIN using the TFT Display, the Arduino captures the PIN and compares it with the PIN stored in EEPROM for a match. If the entered PIN matches with the stored PIN, Arduino opens the door, and ‘Welcome’ is displayed on the display; otherwise, ‘Access Denied’ is displayed on the TFT and the door remains closed. The PIN can be changed by long-pressing the ‘Cancel’ button and then selecting ‘Yes’ on the next screen. The system then asks for the current PIN and matches it with the stored PIN for a match. If a match is found, then the user will be asked to enter a new PIN. This Door lock system can be mounted to any of your existing doors to secure them with a digital password.

Components Required:
1. Arduino UNO.
2. 2.4” TFT LCD Display Shield.
3. Solenoid Lock.
4. Relay Module.
5. Buzzer.

Introduction-LCD Display Shield:
The 2.4” TFT LCD Touch screen is one of the most common RGB color display modules. The LCD has excellent vivid color contrast and comes with a built-in microSD card connection. This TFT display consists of a bright backlight (4 white-LED backlights) and a colorful 240X320 pixels display. It also features individual RGB pixel control giving a much better resolution than the black and white 128×64 displays. We have used this display earlier in our IoT Menu Ordering System and Biometric voting system projects. Before using it with Arduino, let us take a look at the pinout of this 2.4” TFT LCD screen module. A small classification of these pins is given in the table below.
As you can see, 28 pins will perfectly fit into any Arduino Uno / Arduino Mega Board. The TFT display pins are classified into four main parts i.e. Command Pins, Data Pins, SD Card Pins, and Power Pins.

Circuit Diagram:
The complete schematic to build a Smart Door Lock using Arduino is given below. The circuit was drawn using fritzing, since there was no solenoid lock available, I have used a motor to represent it. Both the solenoid lock and this DC motor do not have any polarity and hence can be connected as such.

With a TFT display mounted on Arduino, we only need to connect a relay module and buzzer to Arduino. The Vcc and GND pins of the Relay Module are connected to 5V and GND of Arduino while the Input pin is connected to the 11th pin of Arduino. The positive pin of Buzzer is connected to digital pin 12 of Arduino and the negative pin is connected to the GND pin of Arduino. The entire setup is powered by the 12V Adapter. The onboard 7805 voltage regulator is used to get the regulated 5V to power the Arduino Uno.

Programming with Arduino:
Here we are using SPFD5408 Library that is the modified version of the original Adafruit Library. Complete code is given at the end of the page. Here we are explaining the important sections of the code. There are four sections involved in this code. The first section is creating a UI of a pin lock. The second is detecting the buttons based on the user’s touch, the third is checking the user-entered password and the final section is resetting the password.

PROGRAMMING WITH ARDUINO:

#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>
#include <EEPROM.h>
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define WHITE   0x0000 //Black->White
#define YELLOW    0x001F //Blue->Yellow
#define CYAN     0xF800 //Red->Cyan
#define PINK   0x07E0 //Green-> Pink
#define RED    0x07FF //Cyan -> Red
#define GREEN 0xF81F //Pink -> Green
#define BLUE  0xFFE0 //Yellow->Blue
#define BLACK   0xFFFF //White-> Black
#define MINPRESSURE 10
#define MAXPRESSURE 1000
/*_______Assigned______*/
/*____Calibrate TFT LCD_____*/
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
/*______End of Calibration______*/
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); //300 is the sensitivity
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Start communication with LCD
String symbol[4][3] = {
{ "1", "2", "3"},
{ "4", "5", "6"},
{ "7", "8", "9"},
{ "x", "0", "OK"}
};
int X,Y;
int Number;
int relayPin = 11;// set pin 11 for relay output
int buzzer = 12;
int pass, currentpage;
int password;
int addr = 0;
int addr1 = 1;
unsigned long interval = 2000;
unsigned long previousMillis = 0;
unsigned long currentMillis;
void setup()
{
Serial.begin(9600); //Use serial monitor for debugging
tft.reset(); //Always reset at start
tft.begin(0x9341); // My LCD uses LIL9341 Interface driver IC
tft.setRotation(2); //
tft.fillScreen(WHITE);
draw_BoxNButtons();
pinMode(relayPin, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(relayPin, LOW);// set relay pin to LOW
currentpage = 0;
pass = 0;
}
void loop() {
currentMillis = millis(); // grab current time
TSPoint p = waitTouch();
X = p.y; Y = p.x;
//  Serial.print(X); Serial.print(','); Serial.println(Y);// + " " + Y);
DetectButtons();
delay(300);
}
TSPoint waitTouch() {
TSPoint p;
do {
p = ts.getPoint();
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
} while((p.z < MINPRESSURE )|| (p.z > MAXPRESSURE));
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);;
return p;
}
void DetectButtons()
{
if (currentpage == 0)
{
tft.fillRect(0, 0, 240, 80, CYAN);  //clear result box
tft.setCursor(10, 20);
tft.setTextSize(4);
tft.setTextColor(BLACK);
if (X<80 && X>0) //Detecting Buttons on Column 1
{
if (Y>0 && Y<80) //If cancel Button is pressed
{Serial.println ("Cancel");
Number=0;
if ((unsigned long)(currentMillis - previousMillis) >= interval) {
Serial.print("Detected");
currentpage = 1;
previousMillis = millis();
}
}
if (Y>80 && Y<140) //If Button 1 is pressed
{Serial.println ("Button 7");
if (Number==0)
Number=7;
else
Number = (Number*10) + 7; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<200) //If Button 4 is pressed
{Serial.println ("Button 4");
if (Number==0)
Number=4;
else
Number = (Number*10) + 4; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>200 && Y<260) //If Button 1 is pressed
{Serial.println ("Button 1");
if (Number==0)
Number=1;
else
Number = (Number*10) + 1; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
if (X<160 && X>80) //Detecting Buttons on Column 2
{
if (Y>0 && Y<85)
{Serial.println ("Button 0"); //Button 0 is Pressed
if (Number==0)
Number=0;
else
Number = (Number*10) + 0; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>85 && Y<140)
{Serial.println ("Button 8");
if (Number==0)
Number=8;
else
Number = (Number*10) + 8; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<192)
{Serial.println ("Button 5");
if (Number==0)
Number=5;
else
Number = (Number*10) + 5; //Pressed twic
tft.println(Number); //update new value
delay(150);
}
if (Y>192 && Y<245)
{Serial.println ("Button 2");
if (Number==0)
Number=2;
else
Number = (Number*10) + 2; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
if (X<240 && X>160) //Detecting Buttons on Column 3
{
if (Y>0 && Y<85)
{Serial.println ("OK");
checkPassword();
Number = 0;
delay(150);
}
if (Y>85 && Y<140)
{Serial.println ("Button 9");
if (Number==0)
Number=9;
else
Number = (Number*10) + 9; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<192)
{Serial.println ("Button 6");
if (Number==0)
Number=6;
else
Number = (Number*10) + 6; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>192 && Y<245)
{Serial.println ("Button 3");
if (Number==0)
Number=3;
else
Number = (Number*10) + 3; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
}
if (currentpage == 1) {
tft.fillScreen(BLACK);
tft.setCursor(30, 55);
tft.setTextSize(3);
tft.setTextColor(BLUE);
tft.println("Reset PIN?");
tft.fillRect  (35,140,60,50,BLUE);
tft.fillRect  (140,140,60,50,RED);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(45, 157);
tft.println("Yes");
tft.setCursor(157, 157);
tft.println("No");
if (Y>140 && Y<190)
{
if (X>35 && X<95){
Serial.print("Yes");
setup();
pass = 1;
checkPassword();
}
if (X>140 && X<200){
Serial.print("No");
setup();
}
}
}
if (currentpage == 2)
{
tft.fillRect(0, 0, 240, 80, CYAN);  //clear result box
tft.setCursor(10, 20);
tft.setTextSize(4);
tft.setTextColor(BLACK);
if (X<80 && X>0) //Detecting Buttons on Column 1
{
if (Y>0 && Y<80) //If cancel Button is pressed
{Serial.println ("Set");
Serial.println(Number);
int password1 = (Number / 100); //12 First two digit of entered number
int password2 = (Number % 100); //34 Last two digit of entered number
Serial.println(password1);
Serial.println(password2);
EEPROM.write(addr, password1);
EEPROM.write(addr1, password2);
setup();
Number = 0;
}
if (Y>80 && Y<140) //If Button 1 is pressed
{Serial.println ("Button 7");
if (Number==0)
Number=7;
else
Number = (Number*10) + 7; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<200) //If Button 4 is pressed
{Serial.println ("Button 4");
if (Number==0)
Number=4;
else
Number = (Number*10) + 4; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>200 && Y<260) //If Button 1 is pressed
{Serial.println ("Button 1");
if (Number==0)
Number=1;
else
Number = (Number*10) + 1; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
if (X<160 && X>80) //Detecting Buttons on Column 2
{
if (Y>0 && Y<85)
{Serial.println ("Button 0"); //Button 0 is Pressed
if (Number==0)
Number=0;
else
Number = (Number*10) + 0; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>85 && Y<140)
{Serial.println ("Button 8");
if (Number==0)
Number=8;
else
Number = (Number*10) + 8; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<192)
{Serial.println ("Button 5");
if (Number==0)
Number=5;
else
Number = (Number*10) + 5; //Pressed twic
tft.println(Number); //update new value
delay(150);
}
if (Y>192 && Y<245)
{Serial.println ("Button 2");
if (Number==0)
Number=2;
else
Number = (Number*10) + 2; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
if (X<240 && X>160) //Detecting Buttons on Column 3
{
// if (Y>0 && Y<85)
//{Serial.println ("OK");
//delay(150);
//}
if (Y>85 && Y<140)
{Serial.println ("Button 9");
if (Number==0)
Number=9;
else
Number = (Number*10) + 9; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>140 && Y<192)
{Serial.println ("Button 6");
if (Number==0)
Number=6;
else
Number = (Number*10) + 6; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
if (Y>192 && Y<245)
{Serial.println ("Button 3");
if (Number==0)
Number=3;
else
Number = (Number*10) + 3; //Pressed twice
tft.println(Number); //update new value
delay(150);
}
}
}
}
void draw_BoxNButtons()
{
//Draw the Result Box
tft.fillRect(0, 0, 240, 80, CYAN);
tft.setCursor(37, 27);
tft.setTextSize(3);
tft.setTextColor(BLACK);
tft.println("Enter PIN");
//Draw First Column
tft.fillRect  (0,260,80,60,RED);
tft.fillRect  (0,200,80,60,BLACK);
tft.fillRect  (0,140,80,60,BLACK);
tft.fillRect  (0,80,80,60,BLACK);
//Draw SEcond Column
tft.fillRect  (80,260,80,60,BLACK);
tft.fillRect  (80,200,80,60,BLACK);
tft.fillRect  (80,140,80,60,BLACK);
tft.fillRect  (80,80,80,60,BLACK);
//Draw Third Column
tft.fillRect  (160,260,80,60,BLUE);
tft.fillRect  (160,200,80,60,BLACK);
tft.fillRect  (160,140,80,60,BLACK);
tft.fillRect  (160,80,80,60,BLACK);
//Draw Horizontal Lines
for (int h=80; h<=320; h+=60)
tft.drawFastHLine(0, h, 240, WHITE);
//Draw Vertical Lines
for (int v=0; v<=240; v+=80)
tft.drawFastVLine(v, 80, 240, WHITE);
//Display keypad lables
for (int j=0;j<4;j++) {
for (int i=0;i<3;i++) {
tft.setCursor(22 + (85*i), 100 + (60*j));
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println(symbol[j][i]);
}
}
}
void checkPassword() {
int value1 = EEPROM.read(addr) ;
int value2 = EEPROM.read(addr1) ;
password = (value1*100)+(value2);
if (pass == 0){
Serial.print(password);
Serial.println();
Serial.print(Number);
if (Number == password){
Serial.println("Access Granted");
Serial.println("Relay ON");
tft.fillScreen(BLACK);
tft.setCursor(25, 115);
tft.setTextSize(5);
tft.setTextColor(BLUE);
tft.println("Welcome");
digitalWrite(relayPin, HIGH);// Turn the relay switch ON to open door
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(buzzer, LOW);
Serial.println("Relay OFF");
delay(8000);
digitalWrite(relayPin, LOW);// set relay pin to LOW
}
else {
Serial.println("Access Denied");
tft.fillScreen(BLACK);
tft.setCursor(30, 100);
tft.setTextSize(5);
tft.setTextColor(RED);
tft.println("Access  Denied");
delay(2000);
digitalWrite(relayPin, LOW);// set relay pin to LOW
}
Number = 0;
setup();
}
if (pass==1){
tft.fillRect(0, 0, 240, 80, CYAN);
tft.setCursor(5, 10);
tft.setTextSize(2);
tft.setTextColor(BLACK);
tft.println("Enter Current PIN:");
if (X<240 && X>160 && Y>0 && Y<85)
{
Serial.println ("OK");
Serial.print(password);
Serial.println(Number);
if (Number == password){
Number = 0;
tft.fillRect(0, 0, 240, 80, CYAN);
tft.setCursor(5, 10);
tft.setTextColor(BLACK);
currentpage = 2;
tft.println("Enter New PIN:");
if (X<80 && X>0 && Y>0 && Y<80){
Serial.print("Set");
}
}
else{
Serial.println("Wrong Pin");
tft.fillScreen(BLACK);
tft.setCursor(60, 100);
tft.setTextSize(5);
tft.setTextColor(RED);
tft.println("Wrong");
tft.setCursor(86, 140);
tft.println("Pin");
delay(2000);
setup();
}
}
}
}
Subramanian
Subramanian

Subramanian MK, currently serving as a workshop instructor at Sakthi Polytechnic College, Erode Tamil Nadu. With a career spanning 25 + years, Subramanian MK has dedicated himself to advancing knowledge in Electronics and Communication Engineering (ECE). His passion for exploring new technologies has led to the development of numerous projects, showcasing expertise in IoT and PCB design.

Articles: 510

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

×

Hi, How can I help you?

×