.. 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.5_keypad_c_pi5:
2.1.5 Keypad
============
Introduction
------------
A keypad is a rectangular array of buttons. In this project, We will use
it input characters.
Components
----------
.. image:: img/list_2.1.5_keypad.png
Principle
---------
**Keypad**
A keypad is a rectangular array of 12 or 16 OFF-(ON) buttons. Their
contacts are accessed via a header suitable for connection with a ribbon
cable or insertion into a printed circuit board. In some keypads, each
button connects with a separate contact in the header, while all the
buttons share a common ground.
.. image:: img/image314.png
More often, the buttons are matrix encoded, meaning that each of them
bridges a unique pair of conductors in a matrix. This configuration is
suitable for polling by a microcontroller, which can be programmed to
send an output pulse to each of the four horizontal wires in turn.
During each pulse, it checks the remaining four vertical wires in
sequence, to determine which one, if any, is carrying a signal. Pullup
or pulldown resistors should be added to the input wires to prevent the
inputs of the microcontroller from behaving unpredictably when no signal
is present.
Schematic Diagram
-----------------
.. image:: img/image315.png
.. image:: img/image316.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: img/image186.png
:width: 800
**Step 2:** Open the code file.
.. raw:: html
.. code-block::
cd ~/davinci-kit-for-raspberry-pi/c/2.1.5/
**Step 3:** Compile the code.
.. raw:: html
.. code-block::
gcc 2.1.5_Keypad.cpp -lwiringPi
**Step 4:** Run.
.. raw:: html
.. code-block::
sudo ./a.out
After the code runs, the values of pressed buttons on keypad (button
Value) will be printed on the screen.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi_pi5`.
**Code**
.. code-block:: c
#include
#include
#define ROWS 4
#define COLS 4
#define BUTTON_NUM (ROWS * COLS)
unsigned char KEYS[BUTTON_NUM] {
'1','2','3','A',
'4','5','6','B',
'7','8','9','C',
'*','0','#','D'};
unsigned char rowPins[ROWS] = {1, 4, 5, 6};
unsigned char colPins[COLS] = {12, 3, 2, 0};
void keyRead(unsigned char* result);
bool keyCompare(unsigned char* a, unsigned char* b);
void keyCopy(unsigned char* a, unsigned char* b);
void keyPrint(unsigned char* a);
void keyClear(unsigned char* a);
int keyIndexOf(const char value);
void init(void) {
for(int i=0 ; i<4 ; i++) {
pinMode(rowPins[i], OUTPUT);
pinMode(colPins[i], INPUT);
}
}
int main(void){
unsigned char pressed_keys[BUTTON_NUM];
unsigned char last_key_pressed[BUTTON_NUM];
if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
printf("setup wiringPi failed !");
return 1;
}
init();
while(1){
keyRead(pressed_keys);
bool comp = keyCompare(pressed_keys, last_key_pressed);
if (!comp){
keyPrint(pressed_keys);
keyCopy(last_key_pressed, pressed_keys);
}
delay(100);
}
return 0;
}
void keyRead(unsigned char* result){
int index;
int count = 0;
keyClear(result);
for(int i=0 ; i