Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Interfacing The Keyboard With Arduino In Python Coding
WhatsApp
Sr Karthiga
4y
17.3k
0
1
100
Article
ArduinoKeypad.zip
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
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
/*Arduino Program Keypad Display pushed key in serial monitor*/
const
int
numRows = 4;
// Rows in keypad
const
int
numCols = 3;
// Columns in Tastatur
const
int
debounceTime = 50;
// Time in milliseconds to stabilize key signal
// Keymap defines chars for each key
const
char
keymap[numRows][numCols] = {
{
'1'
,
'2'
,
'3'
},
{
'4'
,
'5'
,
'6'
},
{
'7'
,
'8'
,
'9'
},
{
'*'
,
'0'
,
'#'
}
};
// Arrays definition for the Arduino Pins
const
int
rowPins[numRows] = {
7,
2,
3,
5
};
const
int
colPins[numCols] = {
6,
8,
4
};
void
setup() {
Serial.begin(9600);
// Open Serial Port
for
(
int
row = 0; row < numRows; row++) {
pinMode(rowPins[row], INPUT);
// Switch Pins for Rows as input
digitalWrite(rowPins[row], HIGH);
// Activate Pullups
}
for
(
int
column = 0; column < numCols; column++) {
pinMode(colPins[column], OUTPUT);
// Switch Pins for Columns as input
digitalWrite(colPins[column], HIGH);
// Columns are inactive
}
}
void
loop() {
char
key = getKey();
if
(key != 0) {
// if key was pushed
Serial.println(key);
// display key
delay(10);
// wait 10ms for next input
}
}
// getKey() returns pushed key or 0 if no key was pushed
char
getKey() {
char
key = 0;
// 0 means no key was pushed
for
(
int
column = 0; column < numCols; column++) {
digitalWrite(colPins[column], LOW);
// Activate column.
for
(
int
row = 0; row < numRows; row++) {
// Test for all raws if key was pushed.
if
(digitalRead(rowPins[row]) == LOW) {
// key pushed?
delay(debounceTime);
// Stabilize signal
while
(digitalRead(rowPins[row]) == LOW);
// Wait for key to be released
key = keymap[row][column];
// Put char of pushed key in variable key
}
}
digitalWrite(colPins[column], HIGH);
// Activate column
}
return
key;
// return key or 0
}
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 the serial library
import
serial, time
# Boolean variable that will represent whether or not the arduino is connected
connected =
False
# open the serial port that your arduino is connected to.
ser = serial.Serial(
"/dev/tty.usbmodem1411"
,
9600
)
print
(ser.name)
# loop until the arduino tells us the serial connection is ready
while
not
connected:
serin = ser.read()
connected =
True
# if serial available save value in var, print value and clear it afterwards for new input
while
True
:
var=ser.read()
print
(var)
del
var
# close the port and end the program
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
Figure 1: Output
Arduino
IoT
Keyboard Arduino
Keyboard Python
Python
Up Next
Ebook Download
View all
Raspberry Pi -Sensorial Symphony of Connectivity
Read by 693 people
Download Now!
Learn
View all
Membership not found