How to use Matrix Keypad with Single Arduino Pin?
The 4×4 matrix keypad has 8 pins to interface with Arduino. Even it reduces the number of GPIO pins to interface with Arduino still we may require more pins for other hardware. In this topic, we will see how to use the keypad with using only one pin of Arduino.
Before moving further we need to know about the Voltage Divider Circuit. Voltage divider circuit divides the voltage in between two different resistors. For example, Connect two 4.7K Ohm resistors R1 and R2 in series, the first end connected to +5V and the last end connected to Ground and Vout taken out in between R1 and R2. Now if we read the voltage from Vout we will get half of the Voltage that is 2.5V.
If we use 4 resistors in series R1+R2+R3+R4 with the same 5v input then we will get 4 different voltages when keeping the ground pin common. The output voltages will be is 1.25v at R4, 3v at R3, 4.5v at R2 and 5v at R1. By calculating the amount of voltage we are getting in A0 pin the corresponding key is identified.
Note: This is not a recommended method over the multi-wire interface; because when the voltage value is low the precision will not be fine as it is too difficult to differentiate too small values. This leads to wrong key identification. Try at your own risk.
Make the connections as per the circuit diagram is given below:
Schematic View:
Program:
Before uploading the program you need to install the onewirekeypad.h library by Andrew Mascolo Jr. After installing navigate to examples and open OnewireKeypadFinal example. Modify the keys if needed according to the symbols pasted on your keypad. Upload the code and open serial monitor to check the output. This library example also displays output in LCD if you connected it with the setup.
#include <OnewireKeypad.h> #include <LiquidCrystal.h> LiquidCrystal lcd(2,3,4,5,6,7); char KEYS[] = { '1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D' }; OnewireKeypad <Print, 16 > KP2(Serial, KEYS, 4, 4, A0, 4700, 1000, ExtremePrec ); void setup () { lcd.begin(16, 2); Serial.begin(9600); KP2.SetKeypadVoltage(5.0); } void loop() { char Key; byte KState = KP2.Key_State(); if (KState == PRESSED) { if ( Key = KP2.Getkey() ) { Serial << "Key: " << Key << "\n"; lcd.setCursor(0, 0); lcd.clear(); lcd.print(Key); } } }
Output:
Press the keys one by one and you can see the result in Serial monitor. Make sure you set the serial monitor baud rate to 9600. As I mentioned above when you press the last row, you might not get the correct key because of precise variation in voltages upon keys.
Comments (4)
This is a very interesting sketch.
For me as a non-electrician, such a circuit (sketch) is a little difficult to understand.
Could you please explain where and how the individual fields are identified in the sketch.
I would like to try this principle with 5 normal buttons and 5 LEDs.
Thank you very much
This sketch uses OneWireKeypad Library. So the main operation is hidden in its library file.
What is happening is, In a series of resistors the voltage will vary at each resistor, the voltage varies from 0-5V (as we use 5V).
Ex: If button 1 is pressed, the row resistor & Column resistor combinedly give some voltage. Similarly, if button 2 is pressed, it adds up another resistor in a column and gives another voltage. By measuring the voltage, the library maps the button location.
Thank you Sharath,
for your quick answer.
Has become more understandable now.
Thank you Sharath,
for your quick answer.
Has become more understandable now.