Arduino MicroPython Keypad 3x4
This guide shows you how to use an Arduino with a 3x4 keypad and MicroPython. You will learn:
- How to connect a 3x4 keypad to an Arduino
- How to write MicroPython code for Arduino to read a key press on a 3x4 keypad
- How to check if the password entered on the keypad is correct
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 3x4 Keypad
The 3x4 keypad has 12 soft keys arranged in a grid of rows and columns. Each key represents a button.
Pinout
A 3x4 keypad has 7 pins. These pins are split into two types: rows and columns.
- 4 pins connect to the rows (R1, R2, R3, R4).
- 3 pins connect to the columns (C1, C2, C3).
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino MicroPython Code
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect your Arduino to the 3x4 keypad as shown in the diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- Navigate to the Tools Manage packages on the Thonny IDE.
- Search “DIYables-MicroPython-Keypad”, then find the Keypad library created by DIYables.
- Click on DIYables-MicroPython-Keypad, then click Install button to install Keypad library.
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Press a few keys on your keypad.
- Check out the message in the Shell at the bottom of Thonny.
If it does not work, please check your wiring carefully. It is very easy to confise when connecting Arduino to the keypad 3x4.
Keypad and Password
A keypad is often used to type in a password. Here, we focus on two important keys:
- A key to start or restart typing the password. For example, use the key "*."
- A key to finish typing the customer. For instance, use the key "#."
The password will consist of the other keys, except for two special keys that are not included.
When you press a key.
- If the key is neither "*" nor "#", include it in the password being typed.
- If the key is "#", verify if the typed password is correct, then clear it.
- If the key is "*", clear the password.
Keypad - Password Code
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Arduino
- Type in "123", follow with pressing “#.”
- Type in "1234", then press "#."
- Check out the message in the Shell at the bottom of Thonny.