PiDP-8/I SoftwarePiDP Keyswitch
Not logged in

State Diagram & Schematic

Following is an idea for interfacing a 3-position keyswitch with a Raspberry Pi:

schematic and state diagram

Description

States

  1. Off: The microcontroller cuts the power to the Pi by dropping its power output pin.
  2. Shutdown: Passes the signal to the Pi via a spare GPIO input. Software on the Pi can monitor this and do a soft shutdown.
  3. Run: The shutdown signal is dropped and the power line is raised, allowing power to the Pi.

The shutdown state is special: it leaves the power line in the prior position, so that power to the Pi depends on whether you got to shutdown state from off or run. If the system comes up in shutdown state, it behaves as if the prior state was off.

Microcontroller Program

The proposed microcontroller is a PIC10F220, a 12-bit 256-word 8-pin controller. This program is simple enough to write in a few tens of lines of assembly.

This program should do software debouncing of the switch contacts to avoid the need for external capacitors on the GPIO inputs.

The program should sleep as much as possible. It may be possible to make the program interrupt driven on these three GPIO inputs, so that it sleeps when the contacts are unchanging. Otherwise, it should just wake up every 10-50 ms to "taste" the GPIO inputs and change states as necessary.

Power GPIO

The power GPIO line has a pull-down resistor to ensure that the system power stays off until the microcontroller comes up and decides whether to raise this line and thus open the MOSFET Vds path, allowing power to the Pi.

Shutdown GPIO

This line can be monitored by a trivial program on the Pi side which sets up the shutdown GPIO input with a soft pull-up. The microcontroller's corresponding output can either be passive (open-drain) or an active output defaulting to a high logic state. When the line goes down, the Pi-side program simply calls systemctl shutdown.

Because the monitoring program sets up the pull-up before it begins checking its state, it doesn't need an external pull-up/down resistor.

Power MOSFET

The choice of an On Semi RFD14N05L was done purely by messing with the Digi-Key parametric search engine. I don't have any of these on hand and have no experience with the part. It just looks like it should work, and it's acceptably priced. There may be better options.

Cost/Benefit

BOM cost is approximately US $1.60, not counting the switch. At $0.11/kWh of electricity and ~100 mA of idle current in the poweroff state, a Pi costs only about 50 cents per year to run. Because the microcontroller will sleep much of the time, the circuitry changes this cost very little from an idle current draw standpoint. Thus, payback time from a pure power savings standpoint is roughly 3 years, and then only if you'd otherwise leave the Pi in poweroff state that whole time.

Therefore, the only sensible reason to do this is because it would be nifty. :)