I wanted to use a 4x4 Matrix Keypad in a Raspberry Pi project running on Windows 10 IoT Core, but I couldn't find any libraries that supported it. So, I had to write one myself. I found several Python libraries so I waded through those and figured out what they did. The C# GPIO libraries are built slightly differently so I had to work from that framework so the code didn't port exactly.
The electronics part is really simple with the Pi -- you just hook up the 8 leads from the keypad to 8 GPIO ports on your Pi. In the source code, when you instantiate MatrixKeypadMonitor, you give it the list of pins you used so it knows where to look. If you follow the diagram included, the program should work out of the box.
This is my first attempt using this keypad and seems to work pretty well, but there are a few minor issues with it. The main issue is that you can't press they keys really fast - there has to be a slight delay between presses. I'm not sure if that's a problem with this code, or with the keypad. If you figure out anything, let me know!
You can clone the GitHub repo listed below and it has the full project that you should be able to deploy right to your Pi and try it out.
Steps to run the Test Project- Wire up your matrix keypad to 8 GPIO pins on your Raspberry Pi and make a list of the 8 pin numbers you used. For my project, I hooked them up to Pins 16, 20, 21, 5, 6, 13, 19, 26
- Download the
CS_Universal_App
folder from GitHub and open theMatrixKeypad.csproj
.
- Update the
MatrixKeypadPage.xaml.cs
(Line 28) if you used different pins than what I listed above.
- Deploy the project to your Pi and test it out!
If you want to use it in other projects, all you should need is the MatrixKeypadMonitor.cs
file. Include that file and then put this code in your code somewhere.
// The List on the next line is the GPIO pins that you hooked up
var matrixPad = new MatrixKeypadMonitor(new List<int> { 16, 20, 21, 5, 6, 13, 19, 26 });
// Subscribe to an event that is triggered when a keypress happens
if (matrixPad.SetupSuccessful)
{
matrixPad.FoundADigitEvent += FoundDigit;
}
else
{
Debug.WriteLine(matrixPad.SetupMessage);
}
// This event gets triggered when a key is pressed
public void FoundDigit(object sender, string digit)
{
// Do something here with your keypress
Debug.WriteLine(string.Format("{0} was pressed!", digit));
}
Comments