App - run it as it is

GPIO Panel: a button in and an LED out

Source:
apps/gpio_panel.py
Widgets:
Status, Toggle
Hardware:
Any Raspberry Pi with the 40-pin header, a push button, an LED and a 330 ohm resistor
Status:
Tested with pretend pins, and its pins opened and driven on a real Pi 3A+. A real button press seen on the dashboard is not yet done.

GPIO Panel: a button in and an LED out

Everything a Pi does with the real world comes down to two things: reading a pin, and driving a pin. This app does both, and puts them on the dashboard. It is meant to be copied.

Two pins, two jobs

A push button on one pin shows on the dashboard as Pressed or Released, live. A Toggle on the dashboard switches an LED on another pin.

Swap what the pins mean and you have a door switch, a float switch, a relay, a buzzer. The shape does not change.

Nothing new to learn

The pins are plain gpiozero (Button, LED). The app only connects a gpiozero event to a dashboard component, and a dashboard component to a gpiozero call. ThePiHub adds nothing to the GPIO story, except one helper, prepare_pins(), that lets a Python virtual environment find Raspberry Pi OS's pin driver.

The wiring

Button  one leg -> GPIO 17 (pin 11)   the other leg -> GND (pin 9)   (no resistor: the Pi's own pull-up is on)
LED     GPIO 27 (pin 13) -> 330 Ω -> LED anode (long leg); cathode (short leg) -> GND (pin 14)

An LED needs its resistor: a pin gives about 3.3 V and cannot limit the current itself. Anything that draws more than a few milliamps (a relay board, a buzzer, a motor) needs a transistor or a relay board made for 3.3 V logic. Never connect a pin to more than 3.3 V.

The LED starts off, on purpose

Every time the app starts, the LED is off, and it goes off again when the app stops, whatever the Toggle was left on. The Toggle's state is not saved either. An output that quietly comes back on after a power cut is not what most things you would switch want, so the app makes the safe choice for you. It is a deliberate choice, made in two places in the file (the Toggle's persist=False and the led.off() in start()) that you can change.

Running it

python apps/gpio_panel.py --register TOKEN   # first run, or use `pihub setup`
python apps/gpio_panel.py                    # every run after

No hardware?

--simulate uses gpiozero's pretend pins, so it runs on any computer. In this mode a Test press button appears on the dashboard to press the pretend button, and the Toggle drives a pretend LED. If gpiozero is missing, pip install "thepihub[gpio]"; Raspberry Pi OS already has it.

What next

Read the file: it is short. When you outgrow it, Motion Trap shows the same ideas driving a camera.

← Back to Projects