One Wire Key Pad

This page describes how to wire the output pins of a:

- seven pins, 3 columns * 4 rows key pad


and a
keypad
- four pins three button 3 x 1 keypad,

to a one wire analogue output.
keypad 3b


The addition with a few resistors converts the key pads to one wire and saves many digital pins at the cost one analogue pin.
Libraries are not needed anymore and will save many memory bytes but lacking
interrupt driven detection of operation of the key pad.
But processors are very fast and in many programs it is possible to check the buttons more than 1000 times a second.
The three button key pad is a nice, flat, replacement of a rotary encoder saving three digital pins, a library and 1500 bytes.

3 X 4 key pad
Three different resistor resistances are used.
This page: http://www.circuitbasics.com/how-to-set-up-a-keypad-on-an-arduino/ explains nicely how the key pad works.

There are many tutorials how to make a one-key wire keypad but or they use many resistors, make use of libraries or are hard to follow.

In the picture on the right one can see that by pressing a button a connection is made between a row and a column.
By choosing the right resistor combination the output will be different for every button.
5V is connected to C1 and ground after a 1.1 kilo-ohm resistor.
When 3 is pressed the current flows from R1 tot C3 and flows to a 1.1 kilo-ohm resistor resulting in a reading of 989 bits.
Pressing * R4 and C1 and connected resulting in a flow through all the resistors and a reading of 125 bits.
To discriminate between the buttons the difference between the difference button must be as large as possible.
Because resistors of identical resistance value will not have exact the same resistance measured, readings between modules will vary a little. In the Arduino source code the range per button is large enough to overcome these differences.
Analogue readings Keys 
 

3x4 Keypad Module

Source code
OneWireKeyPad.ino
OneWireKeyPad.fzz Fritzing file

Below the source code to read the key values


3 X 1 key pad

I use this key pad instead of a rotary encoder.

PCB3-button
Read bits PCB
   
Source code: OneWireKeyPad3x1.ino  

                           
//--------------------------------------------
// KEYPAD check for Onewire Keypad input with 5V and 1.1, 4.7, 4.7, 4.7 kOhm resistors
//--------------------------------------------
int sensorPin = A0;   // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
char keyvalue;
char Key;
 
void setup() 
{
 pinMode(ledPin, OUTPUT);   // declare the ledPin as an OUTPUT:
 Serial.begin(9600); 
 Serial.println("Started. Press a key");
}
void loop() 
{
 sensorValue = analogRead(sensorPin); // read the value from the sensor:
 switch(sensorValue)
  {
    case   0 ... 385:  keyvalue = 99;            break;            // noise
    case 386 ... 635:  keyvalue = -1; Key = 'G'; break;            // G 
    case 636 ... 910:  keyvalue =  0; Key = 'Y'; break;            // Y 
    case 911 ... 1023: keyvalue =  1; Key = 'R'; break;            // R 
  }
 if(keyvalue<2) 
   { 
    Serial.print(Key); Serial.print("  Read bits:"); Serial.println(sensorValue);
    digitalWrite(ledPin, HIGH);          // turn the ledPin on
    delay(300); 
    digitalWrite(ledPin, LOW);           // turn the ledPin off:
   }
}
                          


FibonacciClock software V039 + libraries for one wire 4x3 keypad

Ed Nieuwenhuys, July 2022, March 2021
November 2019

< -- Back to start