Skip to content

This is a mock keypad simulation project where I simulated circuit in LTspice to use that data for the test bench. I developed a Tkinter UI to generate data, which are sent to Arduino for processing, then sent back to the UI. Possible work extension could be done by substituting the mock keypad with the real one.

Notifications You must be signed in to change notification settings

machinedeck/Matrix-Keypad-Project

Repository files navigation

Python Keypad Simulator into Arduino UNO Microcontroller through Serial Communication to Develop an MDAS Calculator

Author: černýstroj, Last Update: November 14, 2025

Abstract: 4x4 Keypad consists of matrix of key switches with each row/column connected to a horizontal/vertical line, providing eight output pins. For a single-output detection system, proper circuit design is essential to differentiate one key from another. This project performed a single-output keypad simulation in LTspice to characterize the analog output of each pressed key. The resulting data was used to develop a mock keypad user-interface (UI) for an MDAS calculator, involving random signal generation within the specified range of the key's characteristic analog output, and sending it to Arduino UNO microcontroller for detection and processing. The resulting signals were sent back to the UI to build and calculate mathematical expressions. The developed UI platform could potentially be re-used to replace the mock keypad with a real one for lower latency and without serial communication traffic.

Figure 0. Tkinter UI developed in Python which generates a signal when a key is pressed (keypad on the right). This signal's value is shown on the "Sending Info" tab, with a status display to inform that the signal has been serially communicated to the Arduino UNO microcontroller. Upon detection, the signal is processed to determine which character it corresponds to. The resulting information is communicated back to the UI to display the character on the screen.

0. Background

This is a simple project I started when I saw a matrix keypad in my Arduino Kit. The initial idea I have is to build an "instrument" where pressing the keys creates sound, and both the RGB LED module and matrix tube would show visualizations. It was a fancy idea, but far from completion if I could not even set up the keypad. I did not also know where the breadboard was; however, I was really curious as to how it would be done.

The solution I came up with was to involve some circuit simulation. Once I would have the simulation, I could use this information to create signals that I could feed to Arduino, as if I were doing a test bench for VHDL.

Figure 1. Illustration of the matrix keypad with the connections I adopted from [1].

1. Introduction

I have a 4x4 keypad with 8 pins as illustrated in Fig. 1, which I do not know the specifications or type. My Arduino kit only shows "Key Board 1PCS". Since I wanted to test it but I did not have the necessary electronic parts for connections, I decided to perform simulations first. I also though that performing simulations and test bench could save me time for developing the codes later as I would just have to connect everything and see if it works.

My assumption is that the keypad is a matrix of switches, and the connections I have followed is given below:

Figure 2. Circuit schematic of the keypad switches arranged in a 4x4 matrix. The blue lines correspond to rows while the red ones correpond to columns (Adopted from [1]).

I saw this "1-Wire Keypad Interface With Arduino" from 2, and I wanted to challenge myself to design a circuit that determines which key is pressed only from a single output. This is what I ended up with:

Figure 3. Schematic of the single output keypad circuit. The row connections are in series, and so are the columns, with the output measured at node C1.

Since I intended to use a single output, I had to test whether there would be different voltages corresponding to pressing each individual key.

2. Simulation

I performed a simulation in LTspice with the circuit as follows:

Figure 4. LTspice schematic of Fig.3 for circuit simulation.

As can be seen from Fig. 4, the switches are voltage controlled. In order to check the output due to pressing each individual key, I created a set of pulsed switch voltage sources (SVS) as follows:

Figure 5. Pulse voltages for each switch/key.

Each switch turns ON when its corresponding switch voltage is more than the threshold. Before proceeding, it should be noted that I performed transient simulation, which shows the evolution of the output over time. In order to view the characteristic output of each pressed key, I had to turn an SVS within some specific time, then turn it OFF for the rest of the simulation to turn ON the others, allowing to study their corresponding voltage outputs. Hence, voltages sources in Fig. 4 are pulsed.

For each SVS, it pulses at 5 V during a 0.5-s interval, indicating that is ON. At the start of the simulation, S11 from Fig. 1 turns ON for 0.5 s, and turns off until the end. After 0.5 s when S11 switches off, S12 turns on also for another 0.5 s. The other switches follows the same timing, starting from left to right, top to bottom. The result of the simulation is given as follows:

Figure 6. Pulse timing of each switch/SVS (bottom) and the resulting measured output Vout (top). The output characteristics is divided into four regions according to their row locations. Vout of each pressed key corresponds to the height of the bar.

From the data of Fig. 6, the Vout of the following switches are as follows:

Row 1 Row 2 Row 3 Row 4
Switch/Key Vout [mV] Switch/Key Vout [mV] Switch/Key Vout [mV] Switch/Key Vout [mV]
S11 4775.5 S21 4571.1 S31 4385.5 S41 4210.7
S12 2442.6 S22 2388.0 S32 2335.8 S42 2285.8
S13 1640.9 S23 1616.1 S33 1592.0 S43 1568.7
S14 1235.5 S24 1221.4 S34 1207.5 S44 1194.0

Table 1. Corresponding Vout for each switch/key measured from the LTspice simulation.

3. Implementation

Now that I have identified the output of each key, I can now tell Arduino which key is pressed based on the analog signal it measures. Arduino UNO board, on the other hand, uses the 8-bit ATMEGA328P microcontroller. However, during my initial trial, I assumed 10-bit instead of 8-bit. In any case, this means that the digitization process goes in discrete steps calculated as follows

$$\displaystyle \Delta_{\text{step}} = \frac{V_{\text{max}} - V_{\text{min}}}{2^{n} - 1}$$

where $n$ is the number of bits and ($V_{\text{max}}$, $V_{\text{min}}$) is the voltage range of interest. I assumed that in the real case where I measure the actual output node, I might not get the exact $4775.5$ mV for S11. It might fluctuate, so I tried to create a range based on the step calculated using 10 bits. However, upon realizing that it's 8 bit, there are ranges that overlap. I just assume that the fluctuations may be insignificant, and I can set arbitrary ranges around the measured Vout in Table 1.

Based on these ranges, I started the serial communication protocol in Python. I specified that when a key (e.g. S11) is pressed, a random number is generated within its corresponding range. This signal is then sent to the microcontroller. For the microcontroller part, I also specified the corresponding character of each key. For example, if the microcontroller receives a signal within the range of S11, it should output "1". For characters such as "+", etc. I did not really write them as is. I wrote then in terms of the number of keys, and when the information is sent back to Python, they will be decoded to matching characters.

The script Keypad_Read.ino must be uploaded to the Arduino UNO microcontroller to proceed with the next steps.

3.1. UI

To successfully imitate a keypad, a friendly UI was developed using Tkinter. To make this project much challenging and exciting, I decided to build an Multiplication, Division, Addition and Subtraction (MDAS) calculator. This project then involves two parts: mock keypad integration and MDAS algorithm development.

The first part is straightforward and will only need to match the signal to its corresponding character. There are two windows developed as depicted in Fig. 0. The first window is the main MDAS calculator which has pressable keys, and two screens for the MDAS mathematical expression and the calculated answer. The other window shows the generated pseudosignal of the pressed key, and the status if it has been serially sent to the microcontroller. The pseudosignal is detected and processed, and sent back to the UI, displaying the corresponding character on the main window. The MDAS algorithm runs only when "=" is pressed, which is discussed in detail in the following section.

3.2. MDAS calculation

The generation and transmission of the signal between the UI and microcontroller is straightforward. However, the processing of the string of characters had become a rigorous and crucial task for a successful MDAS calculator.

For the calculation of the MDAS expressions, the code does not recognize the characters as numbers or operators. This makes it not so straightforward. Moreover, the process becomes complicated when there are multiple operations within the string, and a robust and reliable system should be implemented to assure that the correct answer is supplied by the algorithm.

Figure 7. Outline of the MDAS algorithm developed in Python, showing the flow from the string input A into conditional outputs. The inset photo(s) on the upper left show(s) the snippets of the code.

Fig. 7 shows the general implementation of the MDAS algorithm, consisting of conditional statements to consider the possible outcomes when e.g. the number is divided by zero or there is a syntax error. This algorithm is only triggered when "=" is pressed. However, the features of the UI exclude decimal and negative numbers, except if the output is one of those types. This means that double minus sign or a minus sign before a number automatically displays a syntax error. The operators are defined merely for operations.

To properly run the UI, the following codes are needed:

  • keypad.py : Main code to launch the UI. Dependencies include the next scripts
  • keypad_functions.py : Contains the MDAS algorithm and the conversion of the signal into characters
  • sim_data.py : This is unnecessary and can be omitted. This just contains the data of the simulation, which is used to specify the values of the pseudosignals.

4. Conclusion

I presented a mock keypad based on the data of LTspice circuit simulation. I proceeded to develop a UI interface for an MDAS calculator. I outlined the data processing flow from the input string to output when performing calculations.

5. Recommendations

As expected there was an observed delay from pressing the key to displaying the corresponding character due to the back-to-back serial communication. It is recommended to also try to measure this delay as it is not addressed in this project. However, this delay can be mitigated by integrating a real keypad. This may avoid serial communication traffic when sending a pseudosignal and transmitting a processed one from the microcontroller.

6. Future Work

I tried to implement the simulated circuit in a PCB to practice with KiCAD software. My continuing work can be found in this link.

7. References

[1] https://www.circuitbasics.com/how-to-set-up-a-keypad-on-an-arduino/.

[2] https://circuitdigest.com/microcontroller-projects/interface-4x4-membrane-keypad-with-arduino.

[3] https://www.electronicwings.com/arduino/4x4-keypad-interfacing-with-arduino-uno.

About

This is a mock keypad simulation project where I simulated circuit in LTspice to use that data for the test bench. I developed a Tkinter UI to generate data, which are sent to Arduino for processing, then sent back to the UI. Possible work extension could be done by substituting the mock keypad with the real one.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages