## Introduction: One Pin 4×4 Keypad

Every time I see a keypad, it comes with a lot of pins , it's a big waste of your Arduino pins ,so can we run a keypad with one and only one pin ? .The answer is here.

## Step 1: Bill of Materials

Hardware:

01 Arduino UNO

02 Breadboards

01 LCD with I2C

16 Push buttons

04 resistors 1.5 kΩ

04 resistors 620 Ω

04 resistors 220 Ω

08 resistors 100 Ω

01 resistor 1 kΩ

07 Jumper wires

Software:

Arduino IDE installed on your PC

## Step 2: Schematic and Cicruit

The all idea is that we have 4*4 matrix of push buttons connected vertically to the Ground by the right lead and horizontally by the other lead (the button lead) and resistors of 1.5 kΩ, 620Ω, 220Ω, and 100Ω, the ends of the 4 rows are connected by four 100Ω resistors as shown the schematic.

Every time you push a button you close the circuit and the current goes through a different path and different chain of resistors that's why pin A0 recieve a different analog read for every push button. All what you need now is codding.

## Step 3: The Code

#include

#include

LiquidCrystal_I2C lcd(0x3f, 20, 4);

int Button = A0;

int readvalue;

void setup()

{

Serial.begin(9600);

lcd.begin();

pinMode(Button, INPUT);

lcd.backlight();

lcd.print("Hello World");

delay(2000);

lcd.clear();

lcd.print("One pin 4*4 keypad");

delay(2000); }

void loop()

{

readvalue = analogRead(Button);

Serial.println(readvalue);

if (readvalue==852){lcd.clear();lcd.print("A");}

else{ if (readvalue==763){lcd.clear();lcd.print("B");}

else{ if (readvalue==685){lcd.clear();lcd.print("C");}

else{ if (readvalue==965){lcd.clear();lcd.print("D");}

else{ if (readvalue==565){lcd.clear();lcd.print("9");}

else{ if (readvalue==614){lcd.clear();lcd.print("6");}

else{ if (readvalue==360){lcd.clear();lcd.print("3");}

else{ if (readvalue==335){lcd.clear();lcd.print("#");}

else{ if (readvalue==396){lcd.clear();lcd.print("8");}

else{ if (readvalue==349){lcd.clear();lcd.print("5");}

else{ if (readvalue==235){lcd.clear();lcd.print("2");}

else{ if (readvalue==279){lcd.clear();lcd.print("0");}

else{ if (readvalue==452){lcd.clear();lcd.print("7");}

else{ if (readvalue==271){lcd.clear();lcd.print("4");}

else{ if (readvalue==170){lcd.clear();lcd.print("1");}

else{ if (readvalue==92){lcd.clear();lcd.print("*");}else{}}}}}}}}}}}}}}}} }

## Step 4: Correction of Values

When you open the serial moniter it will show a value of 1023 , if you push a button it will give you another reading you have to take those values and make some changes in the code

## Step 5: Project After Criticism and Review

There is no doubt that we are all here to learn and share our knowledge,thanks to some comments left by some people from the community that were very helpful,I decided to make some adjustments and improvements to my project:

The hardware:

I decided to solder all components in a PCB to avoid the problem of bad connection in the breadboards.

The code:

A friend advised me to use a software debouncing and it's just a loop ("for" loop for example) to make the programme take some time to pick a read it means that it make a lot of reads (500 in my examle) but takes only the last one.

for (i=1; i<= 500; i++) { // take only the 500th analogread

value = analogRead(Button);} // that's help to take some time an avoid bad readings

An other friend thanks to him advised me to compare "readvalue" with a range of values not one because "readvalue" takes a lot of values for the same push button.The "A" for example gives a read of : 849, 850 ,851 852, 853, 854 ,855 so it's a range of 7 values :a threshold (852) and 3 values left and right. what we have to do here is to compare the absolute value of the difference between "readvalue" and "852" to "3".

if(abs(readvalue-852)<=8){lcd.clear();lcd.print("A");}

## Step 7: The Moment of Truth

As you can see the program sometimes confuses buttons but it still work , in theory ther is no thing wrong with the circuit but the code needes more calibration.

## Step 8: The End

I hope you like this project and you try to do it, may be you will do better than I did.

Ask me if you have questions ,leave comments and, don't forget to vote for me if you like that.