One Pin 4×4 Keypad

752

14

8

About: Electronics ,Arduino, sensors,automation, PCB ....

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 6: After Some Soldring Work

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.

Step 9:

Step 10:

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    8 Discussions

    0
    None
    cordovox

    26 days ago on Step 4

    Very good explanation. I have used something like this in a progect. I changed the resistors and used forward biased diodes to get 0.6V drop between switches I was only using 6 bush buttons but it was necessary to calibrate the voltage ladder to get reliable detection.

    1 reply
    0
    None
    rbusch

    4 weeks ago

    even though you are reading analog values might be worth experimenting with hardware
    de-bouncing. probably can get by with just one extra capacitor and resistor near the input, might help eliminate false readings if that is an issue. can also use software debouncing but if your already going to have that many components might as well use a few more.

    1 reply
    0
    None
    robertbu

    4 weeks ago

    "readvalue" for a specific push button may change a bit based on temperature, or how you are powering the board, or other factors. Consider giving your detection a bit of leeway. Something like:

    if (abs(readvalue - 751) <= 3)

    Or better yet, put your values sorted in a table, find the nearest value to readvalue, and output if the value is within a threshold.

    1 reply
    0
    None
    btmarobertbu

    Reply 4 weeks ago

    Thanks robertbu I will take that in consideration, about your 2nd solution that's what I did.
    0
    None
    btmatomatoskins

    Reply 4 weeks ago

    thanks, your 74 instructables are great too :)