Skip to content
Tech News
← Back to articles

Gpiozero Flow

read original more articles
Why This Matters

The gpiozero library simplifies programming GPIO devices on the Raspberry Pi by providing intuitive, high-level abstractions and multiple programming paradigms, making it accessible for both beginners and advanced users. Its flexible API supports procedural, event-based, and declarative styles, enabling versatile and efficient hardware control, which accelerates development and innovation in the maker and tech communities.

Key Takeaways

Back in 2015, I had the idea for gpiozero — a Python library for Raspberry Pi, making programming GPIO devices simpler by abstraction: instead of thinking about pin channels, voltages, pull-up resistors and rising or falling edges, we think about buttons being pressed and released, sensors detecting light or motion, and motors driving forwards and backwards. gpiozero let you write code in these terms.

We developed a device-focused API, and patterns emerged. We ended up supporting three types of programming:

Procedural:

while True : if button . is_pressed : led . on () else : led . off ()

Event-based:

button . when_pressed = led . on button . when_released = led . off

Declarative:

led . source = button

These three snippets achieve the same thing in different ways. One repeatedly asks the button if it is pressed; one tells the button to control the LED; and one tells the LED to follow the button's state.

It's hard to say which one is easier to read, write or understand. Maybe the last one, since it's a single line. But if you wanted to reverse the logic, and make the button turn the LED off instead of on, it would be trivial to work out how to change the first two, but not the third.

... continue reading