Interface 4×4 Matrix Keypad with Arduino and Buzzer

Back to Blog
Matrix-Keypad-Buzzer

Interface 4×4 Matrix Keypad with Arduino and Buzzer

We have seen how a Matrix keypad works and How to use it with Arduino in the last tutorial. This tutorial explains how to use this keypad with a Buzzer.

Let’s connect Arduino with a buzzer and program it to perform this action ‘when a particular key press happens the Buzzer will Beep for 1 second. You can also connect a Relay to control it by keypad.

Connect the hardware to the Arduino as shown in the circuit diagram given below.

4x4 Matrix Keypad with Arduino Buzzer_bbNow to turn ON the buzzer when the 5th key is pressed we need to include the following code. In the if loop the key value is compared. If the pressed key is 5 then the buzzer connected at pin 3 turned ON for 1000 milliseconds and then Turned OFF (To avoid continuous beep sound). In setup, we need to define the buzzer pin connected at pin 3.

if (key == 5){

Serial.println(key); //Prints the key in serial monitor

digitalWrite (buzz_pin,HIGH);

Serial.println(Buzzer ON);

delay(1000);

digitalWrite (buzz_pin,LOW);

Serial.println(Buzzer OFF);

}

The following code will print the key pressed to the serial monitor and when you press the key 5 the buzzer Turns ON and Turns OFF after 1 second. For understanding, we print the buzzer status using Serial Monitor.

#include <Keypad.h>

int buzz_pin = 3;

const byte ROWS = 4; //four rows

const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}   

};

byte rowPins[ROWS] = {13, 12, 11, 10}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){

      Serial.begin(9600);

     pinMode(buzz_pin, OUTPUT);

}

void loop(){

  char key = keypad.getKey();  //Variable to store pressed key


  if (key){

    Serial.println(key); //Prints the key in serial monitor

delay(1000);

  }

  if (key == 5){

  Serial.println(key); //Prints the key in serial monitor

digitalWrite (buzz_pin,HIGH);// Turn ON Buzzer

Serial.println(Buzzer ON);

delay(1000);

digitalWrite (buzz_pin,LOW); //Turn OFF Buzzer

Serial.println(Buzzer OFF);

  }

}

 

Share this post

Leave a Reply

Back to Blog