
Overview
This tutorial aims to provide a step-by-step instruction to use a keypad with an Arduino Uno to input and interpret gender and sex. It utilizes a 4×4 matrix keypad and builds on Krishna Pattabiraman’s introductory code on CircuitBasics “HOW TO SET UP A KEYPAD ON AN ARDUINO” tutorial, as well as the CustomKeypad example code from the Mark Stanley and Alexander Brevig Keypad Library.
This tutorial’s code groups individuals based on their sex and age to group them in particular age/sex brackets. This code is intended to be later combined with an oximeter for certain health levels to be determined based on heart rate, age, and sex.
Parts
- 4×4 Matrix Keypad
- Arduino Uno
- 8 x Male-to-male wires
- USB Cable
Diagram


Before running the code, make sure the keypad is wired to the Arduino Uno like above. The connections are fairly simple, with corresponding pins inputted into the keypad being aligned right to left into 2, 3, 4, 5, 6, 7, 8, and 9 digital pin inputs on the Arduino Uno.
Steps
- Wire the keypad to the Arduino Uno as shown in the diagram and as described. Plug the Arduino Uno into the computer with Arduino IDE open.
- Install the keypad library necessary. Specifically, the library used for this tutorial and for this matrix keypad is Keypad library by Mark Stanley and Alexander Brevig. This library simplifies the setting up of the pins and polling the different columns and rows. To install the Keypad library, go to Sketch > Include Library > Manage Libraries and search for “keypad” and “Mark Stanley”. There is only one version available to install (3.1.0).
- Validate and upload the following code on Arduino IDE.
/*
Stephanie Cheng
CPLN 571 Sensing the City
Tutorial: Keypad
This tutorial aims to provide a step-by-step instruction to use a keypad
with an Arduino Uno to input and interpret age and sex.
It utilizes a 4x4 matrix keypad and builds on Krishna Pattabiraman's
introductory code on CircuitBasics "HOW TO SET UP A KEYPAD ON AN ARDUINO" tutorial,
as well as the CustomKeypad example code from the Mark Stanley and Alexander Brevig Keypad Library.
This being said, Mark Stanley and Alexander Brevig's Keypad Library should be installed first and foremost.
8 male-to-male wires should be aligned to the digital pins 2-9 and inputted right to left for the keypad.
*/
#include <Keypad.h> // include the library
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
// define the keypad pins
char hexaKeys[ROWS][COLS] = {
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
// Turn the Serial Protocol ON
Serial.begin(9600);
Serial.print("Press any key to start! (: \n");
}
void loop()
{
static unsigned long lastKeypressTime = 0; // initialize the lastKeypressTime variable
char customKey = customKeypad.getKey(); // variable for the input key
if (customKey) //if there is any input
{
lastKeypressTime = millis(); // update the lastKeypressTime variable with the current time
Serial.print("Enter your gender: (A = Male, B = Female)\n");
Serial.print("You pressed: ");
Serial.println(customKey);
if (customKey == 'A')
{
Serial.print("You are a male\n");
// Ask for the user's age
delay(2000);
Serial.print("Enter your age: ");
class String ageString = ""; // Initialize an empty string to hold the user's age
// Wait for the user to enter their age (up to 10 seconds)
while (millis() - lastKeypressTime <= 10000) {
char ageKey = customKeypad.getKey();
// Initialize if a key is pressed
if (ageKey) {
lastKeypressTime = millis();
Serial.print(ageKey);
ageString += ageKey; // Append the entered digit to the age string
// If the age string has two digits, categorize the age
if (ageString.length() == 2) {
// Print the user's age and age group
Serial.print("\nYour age is: ");
Serial.println(ageString.toInt());
// Convert the age string to an integer
int age = ageString.toInt();
if (age >= 18 && age <= 25) {
Serial.println("You are male in the age group 18-25.");
}
else if (age >= 26 && age <= 35) {
Serial.println("You are male in the age group 26-35.");
}
else if (age >= 36 && age <= 45) {
Serial.println("You are male in the age group 36-45.");
}
else if (age >= 46 && age <= 55) {
Serial.println("You are male in the age group 46-55.");
}
else if (age >= 56 && age <= 65) {
Serial.println("You are male in the age group 56-65.");
}
else if (age >= 66) {
Serial.println("You are male, 66 or older.");
}
else {
Serial.println("Invalid age.");
}
break;
// Check if 10 seconds have passed since the last keypress
if (millis() - lastKeypressTime > 10000) {
Serial.print("No input received. Please try again. \n");
}
}
}
}
}
else if (customKey == 'B')
{
Serial.print("You are a female\n");
// Ask for the user's age
delay(2000);
Serial.print("Enter your age: ");
class String ageString = ""; // Initialize an empty string to hold the user's age
// Wait for the user to enter their age (up to 10 seconds)
while (millis() - lastKeypressTime <= 10000) {
char ageKey = customKeypad.getKey();
// Initialize if a key is pressed
if (ageKey) {
lastKeypressTime = millis();
Serial.print(ageKey);
ageString += ageKey; // Append the entered digit to the age string
// If the age string has two digits, categorize the age
if (ageString.length() == 2) {
// Print the user's age and age group
Serial.print("\nYour age is: ");
Serial.println(ageString.toInt());
// Convert the age string to an integer
int age = ageString.toInt();
if (age >= 18 && age <= 25) {
Serial.println("You are female in the age group 18-25.");
}
else if (age >= 26 && age <= 35) {
Serial.println("You are female in the age group 26-35.");
}
else if (age >= 36 && age <= 45) {
Serial.println("You are female in the age group 36-45.");
}
else if (age >= 46 && age <= 55) {
Serial.println("You are female in the age group 46-55.");
}
else if (age >= 56 && age <= 65) {
Serial.println("You are female in the age group 56-65.");
}
else if (age >= 66) {
Serial.println("You are female, 66 or older.");
}
else {
Serial.println("Invalid age.");
}
break;
// Check if 10 seconds have passed since the last keypress
if (millis() - lastKeypressTime > 10000) {
Serial.print("No input received. Please try again. \n");
}
}
}
}
}
else if (millis() - lastKeypressTime > 10000) { // check if 10 seconds have passed since the last keypress
Serial.print("Error. Please enter either A or B. \n");
}
else {
Serial.print("Error. Please enter either A or B. \n");
}
lastKeypressTime = millis(); // update the lastKeypressTime variable with the current time
}
}
4. Test out the code by entering sex and age into the keypad.