Introduction

In this article, I will explain the method of interfacing the keyboard with Arduino, using Python code. The output will be displayed on the Python side, according to the keyboard condition.
Parts of the list
Hardware Side
  • Arduino Uno
  • Keypad
  • Bread Board
  • Hook-up wires
Software List
  • Arduino IDE
  • Python 3.5.2
Keypad
Keypad
Figure 1: Keypad
  • It is a miniature keyboard.
  • This is the keypad of the 3*4 matrix.
  • It consists of buttons for operating a portable electronic device, telephone, or other equipment.
  • The keypad consists of numeric numbers for the door password.
Simple Programming
Arduino side
  1. /*Arduino Program Keypad Display pushed key in serial monitor*/
  2. const int numRows = 4; // Rows in keypad
  3. const int numCols = 3; // Columns in Tastatur
  4. const int debounceTime = 50; // Time in milliseconds to stabilize key signal
  5. // Keymap defines chars for each key
  6. const char keymap[numRows][numCols] = {
  7. {
  8. '1',
  9. '2',
  10. '3'
  11. },
  12. {
  13. '4',
  14. '5',
  15. '6'
  16. },
  17. {
  18. '7',
  19. '8',
  20. '9'
  21. },
  22. {
  23. '*',
  24. '0',
  25. '#'
  26. }
  27. };
  28. // Arrays definition for the Arduino Pins
  29. const int rowPins[numRows] = {
  30. 7,
  31. 2,
  32. 3,
  33. 5
  34. };
  35. const int colPins[numCols] = {
  36. 6,
  37. 8,
  38. 4
  39. };
  40. void setup() {
  41. Serial.begin(9600); // Open Serial Port
  42. for (int row = 0; row < numRows; row++) {
  43. pinMode(rowPins[row], INPUT); // Switch Pins for Rows as input
  44. digitalWrite(rowPins[row], HIGH); // Activate Pullups
  45. }
  46. for (int column = 0; column < numCols; column++) {
  47. pinMode(colPins[column], OUTPUT); // Switch Pins for Columns as input
  48. digitalWrite(colPins[column], HIGH); // Columns are inactive
  49. }
  50. }
  51. void loop() {
  52. char key = getKey();
  53. if (key != 0) {
  54. // if key was pushed
  55. Serial.println(key); // display key
  56. delay(10);
  57. // wait 10ms for next input
  58. }
  59. }
  60. // getKey() returns pushed key or 0 if no key was pushed
  61. char
  62. getKey() {
  63. char key = 0; // 0 means no key was pushed
  64. for (int column = 0; column < numCols; column++) {
  65. digitalWrite(colPins[column], LOW); // Activate column.
  66. for (int row = 0; row < numRows; row++) {
  67. // Test for all raws if key was pushed.
  68. if (digitalRead(rowPins[row]) == LOW) {
  69. // key pushed?
  70. delay(debounceTime); // Stabilize signal
  71. while (digitalRead(rowPins[row]) == LOW);
  72. // Wait for key to be released
  73. key = keymap[row][column]; // Put char of pushed key in variable key
  74. }
  75. }
  76. digitalWrite(colPins[column], HIGH);
  77. // Activate column
  78. }
  79. return key;
  80. // return key or 0
  81. }
Explanation
  1. const int rowPins[numRows] =
  2. {
  3. 7,
  4. 2,
  5. 3,
  6. 5
  7. };
  8. const int colPins[numCols] = {
  9. 6,
  10. 8,
  11. 4
  12. };
It is the connection code of the Arduino board and keypad when the keypad is connected in the form according to the row and the column. When the program is checked and uploaded to the board, the output will be displayed.
Python Side
  1. # import the serial library
  2. import serial, time
  3. # Boolean variable that will represent whether or not the arduino is connected
  4. connected = False
  5. # open the serial port that your arduino is connected to.
  6. ser = serial.Serial("/dev/tty.usbmodem1411", 9600)
  7. print(ser.name)
  8. # loop until the arduino tells us the serial connection is ready
  9. while not connected:
  10. serin = ser.read()
  11. connected = True
  12. # if serial available save value in var, print value and clear it afterwards for new input
  13. while True:
  14. var=ser.read()
  15. print(var)
  16. del var
  17. # close the port and end the program
  18. ser.close()
Explanation
The Python program will display the output based on the Arduino side. In the python side, true and false conditions are given. When the condition is false, it will not be able to read the value but when the condition is true, it will read the value and end the program.
Output
Output
Figure 1: Output