One Pin Keypad Raspberry Pi Usage Guide
by Progetto Company in Circuits > Raspberry Pi
1112 Views, 1 Favorites, 0 Comments
One Pin Keypad Raspberry Pi Usage Guide
DISCLAIMER: THIS IS A USAGE GUIDE FOR AN OPEN SOURCE PRODUCT: THE ONE PIN KEYPAD. THIS IS NOT A DIY PROJECT. IF YOU WANT TO MAKE THE BOARDS YOURSELF, THE EAGLE FILES CAN BE FOUND AT THE END OF THE TUTORIAL.
What is the One Pin Keypad?
One Pin Keypad makes using a matrix keypad so much easier and lets you reserve your precious I/O pins for other purposes. A 16 button matrix keypad typically requires 8 digital inputs on a microcontroller or single board computer. The most popular Arduinos limit you to only 14 digital pins, so a 16-button keypad leaves you with only 6 pins for the rest of your project. This resource-shortage may lead to a costly design trade-off or a more costly microcontroller upgrade. The One Pin Keypad module eliminates the need for all of these digital inputs. As it's name implies, One Pin Keypad uses only one pin (an analog input) plus GND and VCC connections, so wiring your keypad is a snap! For Raspberry Pi users (the target audience for this tutorial) it also requires an ADS1115 analog to digital converter, which is really easy to wire. The One Pin Keypad Python Package (included) also simplifies the programming required to process the keypad inputs, saving you more time and effort.
For the Arduino Usage Guide, click here.
Gather All the Parts!
To get started with your One Pin Keypad board, you'll need:
- A One Pin Keypad Board
- 3 M/F jumper cables,
- 4 M/M OR 4 more M/F jumper cables, depending what type of headers are on your Raspberry Pi
- A 16 button membrane keypad
- Breadboard (A really small one is used here)
- An ADS1115 Module (from Adafruit or a clone)
- A Raspberry Pi with an internet connection and Raspbian OS (I used Stretch)
- Raspberry Pi Accessories (an HDMI Cable, a power supply, dongles, monitor, etc. depends on the flavour of your Raspberry Pi)
Wiring!
Wire the One Pin Keypad according to the Fritzing diagram, the photo or both, whichever you prefer.
Please note: the note on the Fritzing diagram represents the One Pin Keypad (and the abbreviations GND VCC and AOUT designate the pinouts of the One Pin Keypad board)
Python Package Installation!
Open a terminal window and type the following commands:
sudo apt-get update
then
pip3 install Progetto-One-Pin-Keypad
to install the package and its necessary dependencies
Installing the Examples
I created a dev folder to house the repository under home/pi if you wish to do the same (it is completely optional), run this command:
mkdir dev
then, to navigate into the folder, type:
cd dev
Lastly, to clone the repository, enter the following command:
git clone https://github.com/ProgettoCompany/Progetto_One_Pin_Keypad_Python_Package
Enabling I2C
The ADS1115 ADC (Analog to Digital Converter) relies on I2C (Inter-Integrated Circuit) protocol to communicate with the Raspberry Pi, by default the I2C bus is disabled, to enable it, type:
sudo raspi-config
which will take you to a blue configuration window, from there, using the arrow keys and the enter key, choose
- Interfacing Options
- I2C
- <Yes>
- <Ok>
- Then go down to <Finish>
Tip: Referring to the screenshots above may help a little bit! If you are still stuck, fear not! Refer to this amazing how-to from Sparkfun!
Run Calibratethresholds!
Navigate to the repository if you haven't already and go to the examples folder using these commands:
Tip: type 'cd Prog' then hit Tab, it should autocomplete the repository name!
cd Progetto_One_Pin_Keypad_Python_Package
then type:
cd examples
next, run the calibratethresholds example using this command:
python3 calibratethresholds
and follow the instructions displayed on the console to calibrate your board. After calibration walks through all the buttons, test out your new analog values to see if they work by pressing some buttons when prompted to "Begin.", if the new values prove to be inaccurate, use Ctrl C to kill the program and run it again.
The calibration example should provide you with 2 lines of code you'll need in the next example, make sure to keep the console open and/or copy them both!
Modify and Then Run Readkeypadwithtimeout
Now that your One Pin Keypad is calirbated, it is time to put it to use. While you could do this the from the command line using vi, I personally prefer using an IDE like IDLE.
Open IDLE up (you'll want the python3 version) and modify the following lines:
Remember that analog value array you copied? Replace line 13 (see below) with those newly calibrated analog values.
# ex: my_thresholds = [calibrated values would be here]
is replaced with this:
my_thresholds = [225, 2116, 3904, 5200, 6300, 7350, 8450, 9325, 10000, 10750, 11500, 12100, 12550, 13100, 13800, 14250]
Next, add the next line of code to after line 23, (see below):
keypad.useCalibratedThresholds(my_thresholds)
is added after line 23:
# ex: keypad.use_calibrated_thresholds(your array name)
making lines 22 to 24 look like this in the end:
# If calibrated values are being used, use_calibrated_thresholds below:
# ex: keypad.use_calibrated_thresholds(your array name)
keypad.use_calibrated_thresholds(my_thresholds)
After that's done, run the code and test it out by pressing some buttons! Feel free to adapt the example sketch to your projects! Remember that read_keypad_with_timeout returns the button being pressed as a char and the timeout is set in milliseconds! If you don't want a timeout use the keypad.NO_TIMEOUT constant.
How the One Pin Keypad Works
Much of the One Pin Keypad's design is based off of Hari Wiguna's implementation of a voltage divider to obtain a unique analog value for each key in a 16 pin keypad. One Pin Keypad's design remains largely the same, with a few tweaks. If you a want a great tutorial on how the One Pin Keypad works, feel free to check out Hari Wiguna's awesome video here!
Conclusion
In this instructable, we covered how to get started with your One Pin Keypad board. Now, the curious among you may have wondered why the readKeypadInstantaneous example sketch was never used, the reason is that it is not as useful, because the button has to be pressed when it is called, the readKeypadInstantaneous() function is used primarily to simplify readKeypadWithTimeout() in the library. If it catches your eye feel free to take it for a spin. Happy Hacking!
If you have any questions, leave a comment below or email us at progettocompany@gmail.com. All the best, Progetto Company P.S. As promised, a link to the board's Eagle Files.