PyRaspBeta

← All practice problems

Raspberry Pi practice problem · Buttons & input

Dial safe

HardBudget $6Python · gpiozero

A safe with a dial. A rotary encoder: + to 3.3 V, CLK on GPIO17, DT on GPIO18, push switch SW on GPIO27 (internal pull-up). A green LED on GPIO22 is the bolt.

  • The dial shows a digit 0–9: it starts at 0, each clockwise click adds 1, each anticlockwise click subtracts 1, wrapping (9 → 0, 0 → 9). Print Dial: 7 whenever the digit changes.
  • Clicking the knob enters the current digit.
  • After three digits: if they are 3, 7, 1, print Open and light the LED for 3 s; otherwise print Wrong. Either way, start a new code.

The test dials 1, 2, 0 (each entered with a click) from 1.0 s, then 3, 7, 1 from 4.0 s. The angles are 18°, 36°, 0°, then 54°, 126° and 198° (that last one is 11 clicks from zero, so the dial wraps round to 1), each followed 0.4 s later by a click.

More in Buttons & input