#define NUM_ROWS 4 #define NUM_COLS 4 const uint8_t rowPins[4] = {10,11,12,13}; const uint8_t colPins[4] = {18,19,20,21}; char keyMap[NUM_ROWS][NUM_COLS] = { {'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'} }; void initializeKeypad() { // Columns: OUTPUT, start HIGH for (int i=0; i<NUM_COLS; i++) { gpio_init(colPins[i]); gpio_set_dir(colPins[i], GPIO_OUT); gpio_put(colPins[i], 1); } // Rows: INPUT with pull-up for (int i=0; i<NUM_ROWS; i++) { gpio_init(rowPins[i]); gpio_set_dir(rowPins[i], GPIO_IN); gpio_pull_up(rowPins[i]); } } char readKeypad() { for (int col=0; col<NUM_COLS; col++) { gpio_put(colPins[col], 0); // activate col for (int row=0; row<NUM_ROWS; row++) { if (!gpio_get(rowPins[row])) { // Key pressed at [row][col]! while(!gpio_get(rowPins[row])){} // wait release gpio_put(colPins[col], 1); return keyMap[row][col]; } } gpio_put(colPins[col], 1); // deactivate col } return 0; } int main() { stdio_init_all(); initializeKeypad(); while(1) { char key = readKeypad(); if (key != 0) { printf("Key pressed: %c\n", key); sleep_ms(200); // debounce } } }