ESP32 S3 - Keypad 3x4

by DIYables in Circuits > Microcontrollers

8 Views, 0 Favorites, 0 Comments

ESP32 S3 - Keypad 3x4

esp32-s3-keypad-3x4-cover.jpg

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/esp32-s3/esp32-s3-keypad-3x4

This build connects a 3x4 membrane keypad to an ESP32 S3 so you can read individual key presses and use them to check a password. The keypad uses seven GPIO pins for row and column scanning, with no separate power pin needed, and the DIYables_Keypad library handles the scanning and key detection in code.

Supplies

Wiring

esp32-s3-keypad-3x4-wiring-diagram.jpg

Connect the keypad's four row pins R1-R4 to GPIO17, GPIO16, GPIO15, and GPIO7 on the ESP32 S3. Connect the three column pins C1-C3 to GPIO6, GPIO5, and GPIO4. The keypad runs on 3.3V logic and needs no separate power pin, so these seven connections are all that's required. Make sure each wire is seated firmly in its slot, since loose wiring is the usual cause of missed or swapped keys.

How It Works

The ESP32 S3 scans the row and column lines in sequence to figure out which of the 12 keys is pressed, using only the seven GPIO connections. In the password version, every key press that isn't '*' or '#' gets appended to an input string. Pressing '#' compares that string against a pre-defined password in the sketch and then clears it, while pressing '*' clears the input without checking anything.

Code

The sketch defines the 4x3 key layout as a character array and passes it, along with the row and column pin arrays, to the Keypad library to build a keypad object. The main loop calls getKey() continuously and prints any detected character to the Serial Monitor. The password version builds on this by accumulating characters into a string and running the compare/clear logic described above when '#' or '*' is pressed.

esp32-s3-keypad-3x4.ino - the sketch from the tutorial, unchanged.

/*
* This ESP32 S3 code was developed by newbiely.com
*
* This ESP32 S3 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp32-s3/esp32-s3-keypad-3x4
*/

#include <Keypad.h>

#define ROW_NUM 4 // four rows
#define COLUMN_NUM 3 // three columns

char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};

byte pin_rows[ROW_NUM] = {17, 16, 15, 7}; // GPIO17, GPIO16, GPIO15, GPIO7 connect to the row pins
byte pin_column[COLUMN_NUM] = {6, 5, 4}; // GPIO6, GPIO5, GPIO4 connect to the column pins

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

void setup() {
Serial.begin(9600);
}

void loop() {
char key = keypad.getKey();

if (key) {
Serial.println(key);
}
}

Things That Go Wrong

If key presses don't register or the wrong character shows up, check that the row and column wires aren't swapped and that each jumper is pushed in all the way. If nothing appears in the Serial Monitor, confirm the baud rate there matches the Serial.begin() value in the sketch and that the correct board and port are selected in the Arduino IDE. Also confirm the DIYables_Keypad library is installed before uploading.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/esp32-s3/esp32-s3-keypad-3x4