Arduino - Keypad - Relay

In this tutorial, we are going to learn how to use a keypad, relay, and Arduino together. In detail, If a user inputs the password on the keypad correctly, Arduino turns the relay on.

The tutorial also provides the code that turns on a relay in a period of time and then turns off without using delay() function. The Arduino code also supports multiple passwords.

By connecting relay to Electromagnetic Lock, Solenoid Lock, , Linear, Heating Element, Pump, or Fan ... We can control them by keypad.

Arduino keypad relay

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×Keypad 3x4 and 4x4 Kit
1×Relay
1×Jumper Wires
1×(Optional) Solenoid Lock
1×(Optional) 12V Power Adapter
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Keypad and Relay

If you do not know about keypad and Relay (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino keypad relay wiring diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code - turn relay on if the password is correct

The below codes turn a relay on if the password is correct

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad-relay */ #include <Keypad.h> #define RELAY_PIN A0 // the Arduino pin, which connects to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "ABC1234"; // change your password here const String password_2 = "5642B"; // change your password here const String password_3 = "9765"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password } else if (key == '#') { if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct, turning ON relay"); digitalWrite(RELAY_PIN, HIGH); } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “keypad”, then find the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
Arduino keypad library
  • Search “ezOutput”, then find the ezOutput library by ArduinoGetStarted
  • Click Install button to install ezOutput library.
Arduino output library
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
Arduino IDE Upload Code
  • Press 1234 keys and press #
  • Press 9765 keys and press #
  • See the result on Serial Monitor and the state of relay
COM6
Send
The password is incorrect, try again The password is correct, turning ON relay
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Authorized passwords are pre-defined in the Arduino code.

A string is used to store the password inputted by users, called input string. In keypad, two keys (* and #) are used for special purposes: clear password and terminate password. When a key on keypad is pressed:

  • If the pressed key is not two special keys, it is appended to the input string
  • If the pressed key is *, input string is clear. You can use it to start or re-start inputing the password
  • If the pressed key is #:
    • The input string is compared with the pre-defined passwords. If it matched with one of the pre-defined passwords, the relay is turned on.
    • No matter the password is correct or not, the input string is clear for the next input

Arduino Code - turn a relay on in a period of time if the password is correct

The below code turns the relay on for 5 seconds if the password is correct. After 5 seconds, the relay is turned off.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad-relay */ #include <Keypad.h> #include <ezOutput.h> #define RELAY_PIN A0 // the Arduino pin, which connects to the IN pin of relay #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns #define RELAY_ON_TIME 5000 // in milliseconds char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); ezOutput relay(RELAY_PIN); const String password_1 = "ABC1234"; // change your password here const String password_2 = "5642B"; // change your password here const String password_3 = "9765"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed relay.low(); } void loop() { relay.loop(); // MUST call the loop() function first char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // reset the input password } else if (key == '#') { if (input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct, turning ON relay"); relay.low(); // set low before making a high pulse relay.pulse(RELAY_ON_TIME); // turn on relay in RELAY_ON_TIME duration and then turn off } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES