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
-
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
-
- const int numRows = 4;
- const int numCols = 3;
- const int debounceTime = 50;
-
- const char keymap[numRows][numCols] = {
- {
- '1',
- '2',
- '3'
- },
- {
- '4',
- '5',
- '6'
- },
- {
- '7',
- '8',
- '9'
- },
- {
- '*',
- '0',
- '#'
- }
- };
-
- const int rowPins[numRows] = {
- 7,
- 2,
- 3,
- 5
- };
- const int colPins[numCols] = {
- 6,
- 8,
- 4
- };
- void setup() {
- Serial.begin(9600);
- for (int row = 0; row < numRows; row++) {
- pinMode(rowPins[row], INPUT);
- digitalWrite(rowPins[row], HIGH);
- }
- for (int column = 0; column < numCols; column++) {
- pinMode(colPins[column], OUTPUT);
- digitalWrite(colPins[column], HIGH);
- }
- }
- void loop() {
- char key = getKey();
- if (key != 0) {
-
- Serial.println(key);
- delay(10);
-
- }
- }
-
- char
- getKey() {
- char key = 0;
- for (int column = 0; column < numCols; column++) {
- digitalWrite(colPins[column], LOW);
- for (int row = 0; row < numRows; row++) {
-
- if (digitalRead(rowPins[row]) == LOW) {
-
- delay(debounceTime);
- while (digitalRead(rowPins[row]) == LOW);
-
- key = keymap[row][column];
- }
- }
- digitalWrite(colPins[column], HIGH);
-
- }
- return key;
-
- }
Explanation
- const int rowPins[numRows] =
- {
- 7,
- 2,
- 3,
- 5
- };
- const int colPins[numCols] = {
- 6,
- 8,
- 4
- };
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
-
- import serial, time
-
-
-
- connected = False
-
- ser = serial.Serial("/dev/tty.usbmodem1411", 9600)
- print(ser.name)
-
- while not connected:
- serin = ser.read()
- connected = True
-
- while True:
- var=ser.read()
- print(var)
- del var
-
- 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.