HUM: MEMBRANE KEYBOARD 4 X 1

HUM: MEMBRANE KEYBOARD 4 X 1-Uncategorized

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Dasduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

You have a project in which you need to enter certain values into your program, but you do not know how to do it? Continue reading and you will learn everything about a simple input module.
It is about a membrane keypad that consists of 4 keypads and it is very easy-to-use, and in smaller projects, it is quite sufficient. The keyboard has 4 keypads in one row and works on the same principle as the 4×4 membrane keyboard, but it is smaller and more convenient to use where it is not necessary to have 16 keypads.

Sensor characteristics: 

  • Upper part size: 7 x 2cm
  • Cable length: 9cm, flexible
  • Connector: 5-pin

 

HOW DOES IT WORK?

This keyboard uses 4 keypads with one end connected to the row and the other end to the column. Since we have one keypad in each column, we simply have each keypad connected to one output pin. Keypads simply connect one pin to another, i.e. in our case, they connect the row and column in which they are located, as you can see in the image below.

HOW TO CONNECT IT?

The keyboard can be easily connected to Dasduino because on the keyboard, we have female headers, just like on Dasduino, so we can use male to male cables. The keyboard is connected serially to Dasduino’s digital pins, and the image above shows which pin is which on the keyboard so that we could adjust the given connection scheme to our own projects.

ARDUINO CODE

In order to be able to use the keyboard with our Dasduino, we need to download the Keypad library, and if you do not know how to install a library, read our tutorial.
The library contains examples of how to use the keyboard, so we can also use them to get acquainted with the keyboard’s work principle and the library’s functions which can help us. For starters, we will only print the pressed keypad on the Serial monitor.

#include "Keypad.h"  //including the Keypad library
const byte RED = 1; //defining the number of rows
const byte STUPAC = 4; //defining the number of columns
//defining the symbols that are to be displayed when we press the key
char NasiZnakovi[RED][STUPAC] = {
  {'1','2','3','4'}
};
byte redPin[RED] = {6}; //defining to which Croduino pins we have connected the rows
byte stupacPin[STUPAC] = {4,5,2,3}; //defining to which Croduino pins we have connected the columns (1 > pin4, 2 > pin5, 3 > pin2, 4 > pin3)
//initialization of the NewKeypad class to which we submit NasiZnakovi, pins to which the rows and columns are connected and the number of rows and columns
Keypad Tipkovnica1 = Keypad( makeKeymap(NasiZnakovi), redPin, stupacPin, RED, STUPAC);
void setup(){
  Serial.begin(9600); //starting serial communication
}
void loop(){
  char pritisnutiZnak = Tipkovnica1.getKey();//to the pritisnutiZnak variable, we save the key which is pressed
  // we get the key we have pressed using the getKey() function
  
  if (pritisnutiZnak){
    Serial.println(pritisnutiZnak); //checking whether the keypad is pressed, and if it is, we display it on the Serial monitor  
  }
}

The second example shows us how to use the keyboard to unlock the door. Connect the green LED to pin 12 and connect the red LED to pin 13, both through the resistor of the appropriate value. Connect the keyboard as in the previous example, enter the desired password and length of the password in the program, and we can use the program. When we enter the incorrect password the red diode flashes, and when the password is correct, the green diode turns on for two seconds. In the part of the code where the green diode turns on, we can add the relay or transistor that is connected to the door and can unlock them to also turn on. If necessary we can also increase the time for how long the door is unlocked if we change the value in the delay function that is now set to 2 seconds.

#include "Keypad.h"  //including the Keypad library
#define duljina 4   //defining the password length
int ledz=12;  // the green LED for unlocked, connected to pin 12
int ledc=13;  // the red LED for locked, connected to pin 13
const byte RED = 1; //defining the number of rows
const byte STUPAC = 4; //defining the number of columns
//defining the symbols that are to be displayed when we press the key
char NasiZnakovi[RED][STUPAC] = {
  {'1','2','3','4'}
};
int i=0;
char lozinka[duljina]; //defining the password (lozinka) string which is later used for entering the password using the keyboard
char zadana_lozinka[duljina]="4321" //setting the password we want to use in zadana_lozinka string
char pritisnuto=0; //key char variable to which we will later save the pressed key
byte redPin[RED] = {6}; //defining to which Croduino pins we have connected the rows
byte stupacPin[STUPAC] = {4,5,2,3}; //defining to which Croduino pins we have connected the columns (1 > pin4, 2 > pin5, 3 > pin2, 4 > pin3)
//initialization of the NewKeypad class to which we submit NasiZnakovi, pins to which the rows and columns are connected and the number of rows and columns
Keypad Tipkovnica1 = Keypad( makeKeymap(NasiZnakovi), redPin, stupacPin, RED, STUPAC);
void setup(){
  Serial.begin(9600); //starting serial communication
  pinMode(ledc,OUTPUT); //setting the pin ledc (pin 13) as an output
  pinMode(ledz,OUTPUT); //setting the pin ledz (pin 12) as an output
  digitalWrite(ledc,HIGH); //turn on the red LED because it is locked
  digitalWrite(ledz,LOW); // turn off the green LED
}
void loop(){
  pritisnuto = Tipkovnica1.getKey();//to the pressed (pritisnuto) variable, we save the key that is pressed
  if(pritisnuto){   //checking whether the keypad is pressed, and if it is, we display it on the Serial monitor
  Serial.println(pritisnuto);
  lozinka[i++]=pritisnuto;  //to the password (lozinka) string we enter the key that is pressed
}
if(i==duljina){//checking whether i= equals the variable lenghts (whether we have entered 4 keys, which equals the lenght of our password)
  delay(200); //0.2 second pause
  
  if(!(strncmp(lozinka,zadana_lozinka,duljina))){ // comparing the string lozinka (password) to the string zadana lozinka (default password)
    Serial.println("unlocked password correct"); // if the strings are equal, we display the message that the password is correct on the Serial monitor
    digitalWrite(ledc,LOW); //turn off the red LED
    digitalWrite(ledz,HIGH); //turn on the green LED
   
    delay(2000); //2 second pause
    i=0; // setting i= to 0 so that we could enter the password again, and by doing this, we disable checking of the previous entry, we request a new password entry
    //locking
    digitalWrite(ledz,LOW);   //turn the greed LED off
    digitalWrite(ledc,HIGH); //turn the red LED on
    Serial.println("locked"); //displaying the message locked on the Serial monitor
    }
    else{       //this part is executed if we enter the incorrect password
     digitalWrite(ledz,LOW);
     digitalWrite(ledc,HIGH);
      Serial.println("incorrect password");
      digitalWrite(ledc,LOW);//turn of the red LED for half a second and turn it on again (the LED flashes) to know whether the password is incorrect
      delay(500);
      Serial.println("Try again");
      digitalWrite(ledc,HIGH);// turning the red LED on
      i=0; //set the i= to zero to enter the password again
      }
}
}