PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Buttons & input

Keypad calculator

HardBudget $6Python · gpiozero

The keypad's rows R1–R4 go to GPIO5, 6, 13, 19 and its columns C1–C4 to GPIO12, 16, 20, 21 (layout 123A / 456B / 789C / *0#D). Scan it: drive one row low at a time, with the columns as pulled-up inputs; a low column is a pressed key.

Turn it into a calculator that works left to right:

  • digits build a number; A is +, B is −
  • # is equals: print = <result> (for example = 42 or = -3) and start afresh
  • \* clears everything typed so far and prints Cleared
  • C and D are ignored; each key counts once per press

The test types 12A30# from 1.0 s, 7B10# from 3.0 s, 9A* from 4.6 s and 5A5A5# from 5.6 s (0.25 s between keys).

More in Buttons & input