Here a password based servo control system using Arduino and keypad is illustrated. This system can be used to make door closing and opening system where user has to enter correct password to enter the room. This system can also be used in other similar application where password based control system are required.
In this example, user is asked to enter password on a 4x4 keypad. If the password is correct, then the green LED is turned on for 5 seconds and a servo arm is rotated by 180 degree. The servo arm can be connected to a door lock to close and open the door. Also a message that door is opening is displayed on the serial monitor. If the password is incorrect then the red LED is turned on for 5 seconds and user are informed about the incorrect password. Also a message is displayed that the password is incorrect.
Circuit Diagram for Keypad Interfacing with Arduino for password based system is shown below.
The program code for password protected and servo motor controlled door closing and opening system is below.
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
Password password = Password("1111"); // Set password
//Create Servo object
Servo myservo;
//LEDs
const int greenLED = 10;
const int redLED = 11;
//keypad
const byte ROWS = 4; // Set four rows
const byte COLS = 4; // Set four columns
// Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 9,8,7,6 }; // Pins connected to keypad ROW0, ROW1, ROW2 and ROW3
byte colPins[COLS] = { 5,4,3,2, }; // Pins connected to keypad COL0, COL1 and COL2
// Create the keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
delay(200);
pinMode(11, OUTPUT); // Set green LED as output
pinMode(12, OUTPUT); // Set red LED as output
myservo.attach(12); // Pin connected to servo
keypad.addEventListener(keypadEvent); // Add an event listener to detect key presses
}
void loop(){
keypad.getKey();
myservo.write(0);
}
void keypadEvent(KeypadEvent mykey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(mykey);
switch (mykey) {
case '*':
checkPassword();
break;
case '#':
password.reset();
break;
default:
password.append(mykey);
}
}
}
void checkPassword(){
if (password.evaluate()){
Serial.println("Opening.."); // If the password is correct opening...
myservo.write(180); // Move servo arm 180 degrees
digitalWrite(greenLED, HIGH); // Turn on green LED
delay(5000); // Wait 5 seconds
digitalWrite(greenLED, LOW); // Turn off green LED
}
else{
Serial.println("Password Incorrect"); // If the password is incorrect...
myservo.write(0);
digitalWrite(redLED, HIGH); // Turn on red LED
delay(500); // Wait 5 seconds
digitalWrite(redLED, LOW); // Turn off red LED
}
}
In the above code, we have used the Keypad, Servo, and Password libraries. The Servo library is included in the IDE, but the Keypad and Password libraries have to be downloaded and installed from the Arduino IDE library manager.
The pin 10 and pin 11 of Arduino is connected to the red and green LEDs and the pins are called redLED and greenLED. The servo input pin is connected to pin 12 of Arduino.
The default password set is 1111 which you can change to another 4 digits number. The 4x4 keypad pins are connected to eight pins of Arduino from 2 to 9.
When the program is uploaded and started, then Arduino waits for password input. After the user has to password digits and presses the asterisk key(*) the password is checked. If the hash key(#) is pressed the program is reset and user can enter new password.
In this tutorial we showed password based servo controlled door opening/closing system where we have used libraries. For password based servo controlled system without libraries see Password based Door Locking System using Arduino.