Pi with a keypad matrix

 Pi with a keypad matrix

Today we will see how to interfacing the Raspberry Pi with a matrix keypad usingRpi-hw library.

Matrix keypad interfacing – Video

The library provides the class keypad::matrix, defined in “rpi-hw/keypad/matrix.hpp”, with which it is possible to manage keypads of any size.

Its constructor method takes two ordered lists containing the GPIO pins used by the device:

  1. keypad::matrix(
  2. { COL0, COL1, COL2, … }, // Column pins
  3. { ROW0, ROW1, ROW2, … } // Rows pins
  4. )

Let’s take an example. The following image shows how to connect the Raspberry Piwith a standard 12-key keypad:

Matrix keypad interfacing - Circuit

Matrix keypad interfacing – Circuit

We used the following GPIO pins: 2110, and 4 for columns; 221415, and 17 for rows. This is the internal circuit diagram of the keypad (where pin names refer to the keypad connector):

Matrix keypad interfacing - Internal circuit

Matrix keypad interfacing – Internal circuit

The instance of keypad::matrix will check periodically which buttons are pressed, looking for connection between rows and columns. The control algorithm can be summarized as follows:

  1. Set all GPIOs of rows as input.
  2. Set all GPIOs of columns as output (low).
  3. Set a column pin to high.
  4. If one of rows reads a high value, then the button between those column and row is pressed.
  5. Set the previous column to low.
  6. Repeat from line #3 with another column, until there are no more.
Matrix keypad interfacing - Possible connections

Matrix keypad interfacing – Possible connections

And finally, here is a sample code that shows you how to read the state of some buttons in the 12-key keypad:

  1. #include <iostream>
  2. // Include Rpi-hw headers
  3. #include <rpi-hw.hpp>
  4. #include <rpi-hw/time.hpp>
  5. #include <rpi-hw/keypad/matrix.hpp>
  6. // Use Rpi-hw namespace
  7. using namespace rpihw;
  8. int
  9. main( int argc, char *args[] ) {
  10. // Matrix keypad controller
  11. keypad::matrix dev( { 21, 10, 4 }, { 22, 14, 15, 17 } );
  12. // Main loop
  13. for ( ;; ) {
  14. // Check some keys state
  15. if ( dev.pressed(0) )
  16. std::cout << “You have pressed button 0!\n”;
  17. if ( dev.released(2) )
  18. std::cout << “You have released button 2!\n”;
  19. if ( dev.pressed(1) && dev.pressed(4) )
  20. std::cout << “You have pressed buttons 1 and 4!\n”;
  21. // Wait some time
  22. time::msleep( 100 );
  23. }
  24. return 0;
  25. }

To compile it (or any other programs using the library) use the command:

  1. g++ `pkg-config –libs –cflags rpi-hw` <SOURCE> -o <TARGET>

A more realistic example that uses an Event Listener to handle a 16-keys keypad is the following:

  1. #include <iostream>
  2. #include <memory>
  3. // Include Rpi-hw headers
  4. #include <rpi-hw.hpp>
  5. #include <rpi-hw/time.hpp>
  6. #include <rpi-hw/keypad/matrix.hpp>
  7. // Use the Rpi-hw namespace
  8. using namespace rpihw;
  9. /*
  10. (14, 15, 18, 23) colums = 4
  11. ||||
  12. ———————-
  13. | (1) (2) (3) (A) |
  14. | |
  15. | (4) (5) (6) (B) |
  16. | |
  17. | (7) (8) (9) (C) |
  18. | |
  19. | (*) (0) (#) (D) |
  20. ———————-
  21. ||||
  22. (24, 25, 8, 7) rows = 4
  23. */
  24. /** The class of my application **/
  25. class MyApp {
  26. public:
  27. // Define the keymap
  28. std::vector< uint8_t > keymap = {
  29. ’1′, ’2′, ’3′, ‘A’,
  30. ’4′, ’5′, ’6′, ‘B’,
  31. ’7′, ’8′, ’9′, ‘C’,
  32. ‘*’, ’0′, ‘#’, ‘D’
  33. };
  34. /** Constructor method **/
  35. MyApp() : m_keypad( new keypad::matrix( { 14, 15, 18, 23 }, { 24, 25, 8, 7 }, keymap ) ) {
  36. keypad::T_EventListener listener = std::bind( &MyApp::eventListener, this, std::placeholders::_1 );
  37. // Add the keypad event listener
  38. m_keypad->addEventListener( listener );
  39. }
  40. /** Destructor method **/
  41. ~MyApp() {}
  42. /** A simple keypad event listener **/
  43. void eventListener( keypad::base &dev ) {
  44. const std::vector< uint8_t > &keystate = dev.keyState();
  45. for ( uint8_t c : keystate )
  46. std::cout << (char) c << std::flush;
  47. }
  48. /** Main loop **/
  49. void run() {
  50. for ( ;; ) {
  51. /* … */
  52. }
  53. }
  54. private:
  55. //! The keypad instance.
  56. std::unique_ptr< keypad::matrix > m_keypad;
  57. };
  58. int
  59. main( int argc, char *args[] ) {
  60. MyApp app;
  61. app.run();
  62. return 0;
  63. }

In this manner the keypad instance will call the method MyApp::eventListener to handle the input data.

You can find more information on the reference manual and on the wiki.

That’s all! Feel free to comment my work!

[Updated on 2013-10-22]

Did you find this article helpful?

0 comments

Add a new comment

 

 

 

Please insert the result of the arithmetical operation from the following image:

Please insert the result of the arithmetical operation from this image. =