.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
.. _2.1.8_js:
2.1.8 Keypad
============
Introduction
------------
A keypad is a rectangular array of buttons. In this project, We will use
it input characters.
Required Components
------------------------------
In this project, we need the following components.
.. image:: ../img/list_2.1.5_keypad.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_extension_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_keypad`
- \-
Schematic Diagram
-----------------
.. image:: ../img/image315.png
.. image:: ../img/image316.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../img/image186.png
**Step 2:** Open the code file.
.. raw:: html
.. code-block::
cd ~/raphael-kit/nodejs/
**Step 3:** Run.
.. raw:: html
.. code-block::
sudo node keypad.js
After the code runs, the values of pressed buttons on keypad (button
Value) will be printed on the screen.
**Code**
.. code-block:: js
const Gpio = require('pigpio').Gpio;
var rowsPins = [18,23,24,25];
var colsPins = [10,6,27,17];
var keys = ["1","2","3","A",
"4","5","6","B",
"7","8","9","C",
"*","0","#","D"];
for(let i=0;i{
col=i;
pressed_keys=keys[row*colsPins.length+col];
if(last_key_pressed!=pressed_keys){
console.log(`${pressed_keys}`);
}
last_key_pressed = pressed_keys;
});
}
var row=-1;
setInterval(() => {
row=(row+1)%rowsPins.length;
for(let i=0;i {
row=(row+1)%rowsPins.length;
for(let i=0;i{
col=i;
// pressed_keys=keys[row*colsPins.length+col];
// if(last_key_pressed!=pressed_keys){
// console.log(`${pressed_keys}`);
// }
// last_key_pressed = pressed_keys;
});
}
Set up interrupt functions for the four column pins,
and the variable ``col`` is used to locate the column pins that trigger the rising edge interrupt event.
.. code-block:: js
pressed_keys=keys[row*colsPins.length+col];
if(last_key_pressed!=pressed_keys){
console.log(`${pressed_keys}`);
}
last_key_pressed = pressed_keys;
There is also a piece of code in the break function to get the specific key value from the ``keys`` matrix according to ``row`` and ``col``.
And every time you get a new key value, print the value.
Phenomenon Picture
------------------
.. image:: ../img/image188.jpeg