Doorlock using Keypad with RTW

In this tutorial I will show you how to connect your Keypad to ESP32 and make a mock door lock then do an action based on if you get the rigth code.I will be using the Luxorparts 12 key keypad for Arduino, which is a standard keypad which is 3×4 .


1 x ESP32 OLED

1 x Luxorparts Keypad

10 x Wire

1 x 200 Ohm Resistor


The keypad has 7 pins no GND or VCC , It’s 4 pins for ROWS and 3 pins for COLUMNS/COLS

(For a better picture for the pinout check out this link :×4-keypad/)


Pin 12 Row0
Pin 15 Row1
Pin 25 Row2
Pin 27 Row3
Pin 14 Col0
Pin 13 Col1
Pin 26 Col2


Pin 0 Vcc

All pins needs to be connected to digital pins, so on this example I have connected them 12,15,25,27,14,13,26 respectively on the ESP32. I also have a the VCC for the Led on Pin 0 Which is also a digitalpin. This may vary depending on what kind of microcontroller you have.

Source Codex

We start with defining the  rows and cols (Rows and columns) . 1 character is a single byte. Thereafter we define how the Keypad looks like which is a 2 dimensional array of chars. Thereafter we make an array that stores the initial password . Then we create an array that stores which characters you press to compare the 2 arrays. Then 1 variable to save the key that was pressed and a counter that counts how many times you have pressed the buttons. After that you have to declare which pins that you have connected to the board. Then you have to create a keypad using the Keypad library.

In setup i setup the Wifi again since im going to use the wifi protocol to send data and recieve from RTW , then pinMode(Ledpin,output)

In the loop is where our essential code comes. We use kpd.getkey() method that is from the Keypad library to get which character the user pressed and save it to our key_pressed variable that saves which button the user pressed. Then we add it to our password array. Then we repeat until the counter has been incremented 4 times . Since our array only has size of 4 we dont need anymore then that. Then we check if the content of the arrays match if it matches then print to Serial montior here you can also simulate / do an action. Then it also resets the counter to 0. If the password doesnt match you will get a message in the Serial monitor and the counter will be reset.

Some ideas for you if you want to work more on this example : Make it so when you start the program it asks for an initial password. Make combination so that you can change password if you press it . But have to verify by pressing the password first then you can change. Connect Servo etc to simulate action when pressed correct password.

#include <Nornir_ESP32.h>
#include <Keypad.h>
// RTW config 
const String host = "http://[domain][service]?objectID=[id number]&token=[your token]" 
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
// Variable to store initial password
// Should assign initial_password on 
// The start of program or that you can change it
// On a real product
char initial_password[4] = {'1', '2', '3', '4'}; 
// Variable to store users password
char password[4]; 
// Variable to store incoming keys
char key_pressed = 0; 
// Variable used for counter
int i = 0;  
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 12, 15, 25, 27 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 14, 13, 26 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 0
void setup()
//The wificode comes here check the Wifi post to get the code !

void loop() 

key_pressed = kpd.getKey(); // Storing keys
    if (key_pressed)
      password[i++] = key_pressed; // Storing in password variable
    if (i == 4) // If 4 keys are completed
      if (!(strncmp(password, initial_password, 4))) // If password is matched
        Serial.println("Pass Accepted");
        // Door Opened
        digitalWrite(ledpin, HIGH);
        digitalWrite(ledpin, LOW);
        // Door Closed
        i = 0;      }
      else    // If password is not matched
        Serial.println("Wrong Password");
        i = 0;
Related posts
IntegrationPostsThe Things Network

How we integrated an external service to RTW using Webhooks and PhP


Code used to connect to WIFI with Esp32 (Referenced as WifiCode)


Automatic trash can opener based on distance


Automatic adjust brightness according to how bright the environment is

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *