1 Pin & 6 Buttons - Analog Keyboard
by adachsoft in Circuits > Arduino
2214 Views, 8 Favorites, 0 Comments
1 Pin & 6 Buttons - Analog Keyboard
How to connect six keys with one pin to the microcontroller. It often happens that our circuit is missing free pins. The method to solve this problem is many, for example:
- We can use a microprocessor with more pins.
- We can use i2c, one-wire, UART ( How to use ESP8266 with PCF8574 - 4 input and 4 output).
- We can use the encoder ( Arduino PWM LED dimmer - 6x10W LED)
- Or we can use an analog keyboard.
This article can also see here:
Components
Components I used in this example.
Schematic
Connection of analog keyboard to Arduino.
Software
The method to read the key is very simple. Each key gives different output voltage so using the analog-digital converter recognize which key was pressed. Because reading from the analog input may be slightly different, it is safe to check whether the value is in the range.
#define CHECK_KEY( __v0__, __v1__) __v0__ >= (__v1__ - 10) && __v0__ < (__v1__ + 10)
byte GetKeyNumber(int key){ byte num = 255; //0, 90, 170, 236, 293, 340 if( key < 10 ){ num = 0; }else if( CHECK_KEY(key, 90) ){ num = 1; }else if( CHECK_KEY(key, 170) ){ num = 2; }else if( CHECK_KEY(key, 236) ){ num = 3; }else if( CHECK_KEY(key, 293) ){ num = 4; }else if( CHECK_KEY(key, 340) ){ num = 5; } return num; }Download source code: AnalogKeyboard.ino