Arduino Nano - Keypad - Relay

This tutorial instructs you how to use a keypad and Arduino Nano to control relay. When the user inputs the correct password on the keypad, the Arduino Nano will activate the relay.

The tutorial also provides the Arduino Nano code that activates a relay for a certain duration and then deactivates it without using the delay() function. Furthermore, the Arduino Nano code is capable of handling multiple passwords.

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

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Keypad
1×Relay
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Keypad and Relay

If you are unfamiliar with keypad and relay (including pinout, operation, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and keypad relay

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - turn relay on if the password is correct

If the password is correct, the following codes will activate a relay.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-relay */ #include <Keypad.h> const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //four columns const int RELAY_PIN = 2; // The Arduino Nano pin connected to the IN pin of relay module const byte pin_rows[ROW_NUM] = {10, 9, 8, 7}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {6, 5, 4}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); const String password_1 = "4321"; // change your password here const String password_2 = "09876"; // 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 correct password! Turning ON relay"); digitalWrite(RELAY_PIN, HIGH); } else { Serial.println("The incorrect password! try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Detailed Instructions

  • Connect a USB cable to the Arduino Nano and PC.
  • Open the Arduino IDE, select the correct board and port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “keypad” and locate the keypad library created by Mark Stanley and Alexander Brevig.
  • Then, press the Install button to install the keypad library.
Arduino Nano keypad library
  • Search for “ezOutput”, then locate the ezOutput library by ArduinoGetStarted.
  • Press the Install button to install the ezOutput library.
Arduino Nano output library
  • Copy the code and open it in the Arduino IDE.
  • Then, press the Upload button in the Arduino IDE to compile and upload the code to the Arduino Nano.
Arduino IDE Upload Code
  • Enter 1234 followed by the # key and then 9765 followed by the # key.
  • Check the result on the Serial Monitor and observe the state of the relay.
COM6
Send
The incorrect password! try again The correct password! Turning ON relay
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Code Explanation

Authorized passwords are pre-defined in the Arduino Nano code. A string is utilized to store the password inputted by users, referred to as the input string. On the keypad, two keys (* and #) are used for special purposes: clearing the password and terminating the password. When a key on the keypad is pressed:

  • If the pressed key is not either of the two special keys, it is appended to the input string.
  • If the pressed key is *, the input string is cleared. This can be used to start or restart inputting the password.
  • If the pressed key is #:
    • The Arduino Nano verifies the input password by comparing the input string with the pre-defined passwords. If it matches one of the pre-defined passwords, the relay is turned on.
    • Regardless of whether the password is correct or not, the input string is cleared for the next input.

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

If the password is correct, the relay will be switched on for 5 seconds. After that period of time, the relay will be deactivated.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-keypad-relay */ #include <Keypad.h> #include <ezOutput.h> #define RELAY_ON_TIME 5000 // in milliseconds const int ROW_NUM = 4; //four rows const int COLUMN_NUM = 3; //four columns const int RELAY_PIN = 2; // The Arduino Nano pin connected to the IN pin of relay module const byte pin_rows[ROW_NUM] = {10, 9, 8, 7}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {6, 5, 4}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM); ezOutput relay(RELAY_PIN); const String password_1 = "4321"; // change your password here const String password_2 = "09876"; // 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 correct password! 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 incorrect password! try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!