How to use a 2.08 inch 256x64 OLED display with a keypad?
How to Use a 2.08 Inch 256x64 OLED Display with a Keypad
To use a 2.08 inch 256x64 oled display with a keypad, you need to wire it to a microcontroller like an Arduino or ESP32, then write code to read key presses and update the display. The display uses SPI communication, which is fast and reliable for showing text or graphics, while the keypad typically uses a matrix scanning method to detect which button is pressed. I’ll walk you through the hardware connections, power requirements, and software setup with real numbers and wiring diagrams, so you can get this working in a project like a menu system or a data logger.
First, let’s talk about the display itself. The 2.08 inch 256x64 oled display is a monochrome OLED panel with a resolution of 256 pixels horizontally and 64 pixels vertically. It uses a single-color pixel arrangement, typically white, yellow, or blue, and draws about 20mA to 30mA at 3.3V when all pixels are lit. The SPI interface requires four pins: SCK (clock), MOSI (data), CS (chip select), and DC (data/command). Some modules also have a RESET pin, which you can connect to a GPIO or tie to the microcontroller’s reset line. The display controller is usually an SSD1306 or SH1106, both of which have a 128x64 internal buffer, but the 256x64 resolution is achieved by using two 128x64 controllers side-by-side or by a custom driver. In practice, you treat it as a 256x64 framebuffer, and the library handles the mapping.
For the keypad, a common choice is a 4x4 matrix membrane keypad, which has 16 keys arranged in 4 rows and 4 columns. Each key connects a row to a column when pressed. To read it, you set the column pins as outputs and drive them low one at a time, then read the row pins as inputs with pull-up resistors. If a row goes low, the key at that row and column is pressed. The 4x4 keypad draws negligible current, under 1mA, but you need to debounce it in software to avoid false triggers. A typical debounce delay is 50 milliseconds. Alternatively, you can use a 3x4 keypad for 12 keys, or a custom keypad with fewer buttons, but the matrix scanning logic is the same.
Now, let’s get into the wiring. Here’s a table for connecting the display to an Arduino Uno, which runs at 5V logic but the display is 3.3V tolerant. You’ll need a level shifter or voltage divider for the MOSI and SCK lines if your microcontroller is 5V, but many OLED modules have built-in regulators. Always check the datasheet for your specific module.
Display to Arduino Uno Wiring
| Display Pin | Arduino Pin | Notes |
| VCC | 3.3V | Some modules accept 5V, but 3.3V is safer |
| GND | GND | Common ground |
| SCK | 13 (SCK) | SPI clock |
| MOSI | 11 (MOSI) | SPI data |
| CS | 10 (SS) | Chip select, can be any digital pin |
| DC | 9 | Data/command, can be any digital pin |
| RESET | 8 | Optional, tie to 3.3V if not used |
For the keypad, a 4x4 matrix typically has 8 pins: 4 for rows and 4 for columns. Connect them to digital pins on the Arduino, say pins 2 through 5 for rows and 6 through 9 for columns. But note that pin 9 is already used for the display’s DC pin, so you’ll need to adjust. I’ll use pins 4,5,6,7 for rows and pins 2,3,8,9 for columns, but move the display’s DC to pin 10 and CS to pin 11. Here’s the keypad wiring:
4x4 Keypad to Arduino Uno Wiring
| Keypad Pin | Arduino Pin | Function |
| Row 1 | 4 | Input with internal pull-up |
| Row 2 | 5 | Input with internal pull-up |
| Row 3 | 6 | Input with internal pull-up |
| Row 4 | 7 | Input with internal pull-up |
| Column 1 | 2 | Output, driven low |
| Column 2 | 3 | Output, driven low |
| Column 3 | 8 | Output, driven low |
| Column 4 | 9 | Output, driven low |
With these connections, the display uses SPI on pins 10 (CS), 11 (MOSI), 13 (SCK), and pin 10 for DC, while the keypad uses pins 2-9. The Arduino’s SPI pins are fixed, but you can reassign CS and DC to any digital pins by changing the library initialization. For the keypad, you can use any digital pins, but avoid using pin 13 if you have a LED on the board, as it might interfere.
Now, let’s talk about power. The display draws 20mA to 30mA at 3.3V, which is fine from the Arduino’s 3.3V output, which can provide up to 150mA. The keypad draws negligible current, so the total load is under 50mA. However, if you use an ESP32, it runs at 3.3V logic, so you can connect the display directly without level shifting. The ESP32’s power consumption is higher, around 80mA to 200mA depending on Wi-Fi usage, but the display and keypad won’t overload it. Use a regulated 3.3V supply for both the microcontroller and the display if you’re building a standalone project.
For software, you’ll need two libraries: one for the OLED display and one for the keypad. For the display, the Adafruit SSD1306 library works with most 128x64 OLEDs, but for a 256x64 display, you might need a modified version or the U8g2 library, which supports a wide range of controllers. U8g2 is more flexible and works with SH1106 and SSD1306 controllers. Install it via the Arduino Library Manager. For the keypad, the Keypad library by Mark Stanley and Alexander Brevig is standard. It handles matrix scanning and debouncing.
Here’s a code example to get you started. This code initializes the display at 256x64 resolution, scans the keypad, and prints the pressed key on the screen. I’ll use U8g2 for the display and the Keypad library.
Arduino Code Example
```cpp
#include
#include
// U8g2 setup: use U8G2_SSD1306_256X64_NONAME_F_4W_HW_SPI for hardware SPI
// Adjust pins: CS=10, DC=11, RESET=8 (or -1 if not used)
U8G2_SSD1306_256X64_NONAME_F_4W_HW_SPI u8g2(/* rotation=*/ U8G2_R0, /* cs=*/ 10, /* dc=*/ 11, /* reset=*/ 8);
// Keypad matrix: 4 rows, 4 columns
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {4,5,6,7};
byte colPins[COLS] = {2,3,8,9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
u8g2.begin();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_ncenB08_tr); // 8-pixel font
u8g2.drawStr(0, 10, "Press a key");
u8g2.sendBuffer();
}
void loop() {
char key = keypad.getKey();
if (key) {
u8g2.clearBuffer();
u8g2.drawStr(0, 10, "Key pressed:");
u8g2.drawStr(0, 30, &key);
u8g2.sendBuffer();
Serial.println(key);
}
}
```
This code uses hardware SPI, which is faster than software SPI. The U8g2 library automatically handles the 256x64 resolution, but you need to ensure the constructor matches your display’s controller. If your display uses an SH1106, change the constructor to U8G2_SH1106_256X64_NONAME_F_4W_HW_SPI. The keypad library uses non-blocking scanning, so it won’t hang your program. The debounce is handled internally with a default delay of 50ms, which you can adjust with keypad.setDebounceTime(30).
One common issue with the 256x64 OLED is that some libraries treat it as two 128x64 displays, so you might see a split in the middle. To fix this, you need to set the correct memory addressing mode. In U8g2, this is handled automatically, but if you use Adafruit’s library, you’ll need to modify the initialization sequence. For example, the SSD1306 datasheet shows that for a 256x64 display, you need to set the column address range to 0 to 255 and the page address range to 0 to 7. In the Adafruit library, you can call display.setColumnAddress(0, 255) and display.setPageAddress(0, 7) after display.begin(). With U8g2, it’s built-in, so it’s simpler.
Another practical detail is the font size. The 256x64 display has 64 pixels vertically, so you can fit about 8 lines of 8-pixel tall text, or 4 lines of 16-pixel tall text. The horizontal resolution of 256 pixels means you can fit about 32 characters of an 8-pixel wide font. For a keypad-driven menu, you can display multiple options on one screen. For example, a 4-line menu with 16-pixel fonts can show 4 items, and you can use the keypad’s arrow keys (if you have them) to navigate. If your keypad doesn’t have arrows, you can map the number keys to functions: 1 for up, 2 for down, 3 for select, etc.
Let’s talk about real-world data. The 2.08 inch display has a viewing angle of over 160 degrees, which is typical for OLEDs, and a contrast ratio of 2000:1. The response time is under 10 microseconds, so it’s fast enough for animations. The keypad, on the other hand, has a mechanical lifetime of about 1 million presses per key, and the contact resistance is around 100 ohms. In a project, you might want to add a buzzer for key feedback, which draws an additional 20mA to 30mA. The total power budget for a battery-powered project should be under 100mA, so a 1000mAh battery would last about 10 hours of continuous use.
If you’re using an ESP32, you can also add Wi-Fi to send keypad data to a server or display web content. The ESP32 has 3.3V logic, so you can connect the display directly. The SPI pins on the ESP32 are typically GPIO 18 (SCK), 23 (MOSI), 5 (CS), and 2 (DC). The keypad can use any GPIOs, but avoid pins that are used for flash memory, like GPIO 6-11. Here’s a quick wiring table for ESP32:
Display and Keypad to ESP32 Wiring
| Component | ESP32 Pin | Notes |
| Display VCC | 3.3V | Direct connection |
| Display GND | GND | Common ground |
| Display SCK | 18 | SPI clock |
| Display MOSI | 23 | SPI data |
| Display CS | 5 | Chip select |
| Display DC | 2 | Data/command |
| Display RESET | 4 | Optional |
| Keypad Rows | 12,13,14,15 | Input with pull-up |
| Keypad Cols | 16,17,21,22 | Output, driven low |
On the ESP32, the Keypad library works the same, but you need to set the pin modes in the code. The U8g2 library also supports ESP32 hardware SPI, but you might need to specify the SPI instance. For example, U8G2_SSD1306_256X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, 5, 2, 4) uses CS=5, DC=2, RESET=4. The code is identical to the Arduino version, but you’ll need to include the ESP32 core in your board manager.
One more thing: the display’s brightness. The OLED pixels are current-driven, so you can adjust brightness by changing the contrast register. In U8g2, you can call u8g2.setContrast(0x7F) where 0x00 is off and 0xFF is max. The default is 0x7F (127), which gives a good balance between brightness and power consumption. At max contrast, the display draws about 30mA, while at half contrast, it’s around 20mA. For a keypad-driven project, you might want to dim the display after a few seconds of inactivity to save power. You can do this by setting a timer in the loop and calling u8g2.setPowerSave(1) to turn off the display, then u8g2.setPowerSave(0) when a key is pressed.
Finally, let’s address some common pitfalls. First, the SPI speed. The display can handle up to 10MHz SPI clock, but Arduino’s hardware SPI runs at 4MHz by default, which is fine. If you use software SPI, it’s slower and might cause flickering. Second, the keypad’s ghosting issue. If you press multiple keys at once, the matrix might register false presses. To avoid this, use a diode in series with each key, but that’s only needed for multi-key applications. For single-key presses, the library handles it. Third, the display’s initialization sequence. Some modules require a specific reset sequence: pull RESET low for 10ms, then high. The U8g2 library does this automatically if you specify the reset pin.
In practice, you can build a menu system with 4 levels, each showing 4 options, and use the keypad to navigate. For example, press 1 to go up, 2 to go down, 3 to select, and 4
Ready to go borderless?
Activate Teleki Blanka's global eSIM in 90 seconds. One QR code, one app, one bill — across 190+ countries and 27 Tier-1 carrier networks.