Build it step by step
0 of 7 doneWire the keypad rows
R1 to R4 to GPIO32, GPIO33, GPIO25, GPIO26.
Wire the keypad columns
C1 to C4 to GPIO27, GPIO14, GPIO13, GPIO4.
Wire the OLED
3V3, GND, SDA to GPIO21, SCL to GPIO22.
Install the libraries
Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The display shows 0.
Try a sum
Press 1, 2, A, 3, then # to get 15.
Clear
Press * to start again.
How do you build an ESP32 keypad calculator?
An ESP32 scans a 4 by 4 membrane keypad itself and runs a four-function calculator on an OLED. You need ESP32 DevKit V1, 4x4 matrix keypad (membrane), SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.
- Level
- Intermediate
- Time
- About 150 min
- Parts
- 4
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SwitchesBuy
- DisplaysBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Keypad row pins R1 to R4 | ESP32 GPIO32, GPIO33, GPIO25, GPIO26 | Rows driven low one at a time |
| Keypad column pins C1 to C4 | ESP32 GPIO27, GPIO14, GPIO13, GPIO4 | Columns read with pull-ups |
| OLED VCC | ESP32 3V3 | Power at 3.3 V |
| OLED GND | ESP32 GND | Common ground |
| OLED SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C data and clock |
Code
A keypad of sixteen keys would need sixteen pins if every key had its own wire. A matrix keypad saves pins by arranging the keys in four rows and four columns, so eight wires are enough. To find which key is pressed the program sets one row low at a time and then reads all four columns. If a column reads low while that row is low, the key where that row and column cross is pressed. The columns use the board's pull-up resistors so they read high when nothing is pressed. The pins chosen here are all safe ones on the ESP32, avoiding the pins that affect booting. The calculator is a small state machine: digits build up the current number, an operator key stores the first number and waits for the second, and the equals key works out the answer. Whole numbers only, so division drops any remainder, and dividing by zero shows Error until you clear it.
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int ROWS[4] = {32, 33, 25, 26};
const int COLS[4] = {27, 14, 13, 4};
const char KEYS[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
}; // A plus, B minus, C times, D divide, * clear, # equals
long acc = 0, cur = 0;
char op = 0;
bool fresh = true, err = false;
char scanKey() {
for (int r = 0; r < 4; r++) {
digitalWrite(ROWS[r], LOW);
delayMicroseconds(50);
for (int c = 0; c < 4; c++) {
if (digitalRead(COLS[c]) == LOW) {
digitalWrite(ROWS[r], HIGH);
return KEYS[r][c];
}
}
digitalWrite(ROWS[r], HIGH);
}
return 0;
}
long apply(long a, char o, long b) {
switch (o) {
case 'A': return a + b;
case 'B': return a - b;
case 'C': return a * b;
case 'D': if (b == 0) { err = true; return 0; } return a / b;
}
return b;
}
void handle(char k) {
if (k >= '0' && k <= '9') {
if (err) { acc = cur = 0; op = 0; err = false; fresh = true; }
if (fresh) { cur = 0; fresh = false; }
if (cur < 100000000L) cur = cur * 10 + (k - '0');
} else if (k == '*') {
acc = cur = 0; op = 0; fresh = true; err = false;
} else if (k == '#') {
if (op && !fresh) { cur = apply(acc, op, cur); op = 0; }
fresh = true;
} else if (k >= 'A' && k <= 'D' && !err) {
if (op && !fresh) cur = apply(acc, op, cur);
acc = cur; op = k; fresh = true;
}
}
void draw() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
oled.setCursor(0, 0);
if (op) {
oled.print(acc);
oled.print(op == 'A' ? " +" : op == 'B' ? " -" : op == 'C' ? " x" : " /");
}
oled.setTextSize(2);
oled.setCursor(0, 28);
if (err) oled.print("Error"); else oled.print(cur);
oled.display();
}
void setup() {
for (int r = 0; r < 4; r++) { pinMode(ROWS[r], OUTPUT); digitalWrite(ROWS[r], HIGH); }
for (int c = 0; c < 4; c++) pinMode(COLS[c], INPUT_PULLUP);
Wire.begin(21, 22);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
draw();
}
void loop() {
static char last = 0;
static unsigned long changed = 0;
char k = scanKey();
if (k != last && millis() - changed > 30) { // debounce
changed = millis();
last = k;
if (k) { handle(k); draw(); }
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| 12 + 3 then equals | 15 |
| 9 - 4 then equals | 5 |
| 6 x 7 then equals | 42 |
| 8 / 2 then equals | 4 |
| 5 / 0 then equals | Error |
| Press * | Back to 0 |
If it does not work
Keys give the wrong character
Your keypad's pins may be in a different order, test with a short program that prints the row and column.
Some keys do nothing
Check the ribbon connector is pushed in fully and all eight wires are connected.
Two characters per press
Raise the debounce time from 30 to 50 milliseconds.
Display blank
Check the I2C wires and that the address is 0x3C.
Board will not boot with the keypad connected
Make sure the keypad is on the pins listed, not on GPIO12 or GPIO0.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the ESP32 keypad calculator?
ESP32 DevKit V1, 4x4 matrix keypad (membrane), SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the ESP32 keypad calculator take to build?
About 150 minutes for a intermediate maker, in 7 steps.
How do I know the ESP32 keypad calculator works?
12 + 3 then equals: 15. 9 - 4 then equals: 5. 6 x 7 then equals: 42.
Keys give the wrong character: what should I check?
Your keypad's pins may be in a different order, test with a short program that prints the row and column.
Some keys do nothing: what should I check?
Check the ribbon connector is pushed in fully and all eight wires are connected.
Two characters per press: what should I check?
Raise the debounce time from 30 to 50 milliseconds.
Display blank: what should I check?
Check the I2C wires and that the address is 0x3C.
