Skip to content

RP2040 Compatibility Fix for Keypad Library #55

@djairjr

Description

@djairjr

The Issue

When running the standard Keypad library on the RP2040 architecture (e.g., Raspberry Pi Pico, Arduino Nano RP2040 Connect), the high execution speed of the processor leads to "ghosting" or "bouncing" effects on the keypad matrix scan. Specifically, after driving a column pin LOW, the voltage on input row pins does not settle fast enough before the read operation occurs, due to residual capacitance. This often manifests as unintended triggers for specific keys (commonly keys 1 and 8 on 4x4 matrices).

The Proposed Fix

Add a small settling delay (20 microseconds) in the scanKeys() function, immediately after the column pin is driven LOW. This delay is only applied when compiling for the RP2040 architecture to avoid affecting performance on slower platforms.

Implementation Details

In Keypad.cpp, within the scanKeys() function:

void Keypad::scanKeys() {
    ...
    for (byte c=0; c<sizeKpd.columns; c++) {
        pin_mode(columnPins[c],OUTPUT);
        pin_write(columnPins[c], LOW);	// Begin column pulse output.

        // --- NEW FIX FOR RP2040 ---
        #if defined(ARDUINO_ARCH_RP2040)
        delayMicroseconds(20);			// Settling time for high-speed RP2040 IO
        #endif
        // --------------------------

        for (byte r=0; r<sizeKpd.rows; r++) {
            bitWrite(bitMap[r], c, !pin_read(rowPins[r]));
        }
    ...
}

Benefits

  • Resolves ghosting/false triggering issues on RP2040.
  • Maintains zero impact on other architectures (AVR, ESP8266, etc.) due to conditional compilation.
  • Minimal overhead (20µs per column scan) which is imperceptible for human-input interfaces.

This fix has been verified by the community for stabilizing direct GPIO keypad matrices on RP2040-based boards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions