{"id":7272,"library":"gpiozero","title":"GPIO Zero","description":"GPIO Zero is a Python library that provides a simple and approachable interface to GPIO (General Purpose Input/Output) devices on Raspberry Pi. It abstracts away low-level pin details, offering high-level classes for common components like LEDs, buttons, motors, and sensors, making physical computing accessible. The current version is 2.0.1. It generally follows a release cadence tied to bug fixes and new features, with major versions introducing breaking changes.","status":"active","version":"2.0.1","language":"en","source_language":"en","source_url":"https://github.com/gpiozero/gpiozero","tags":["raspberry pi","gpio","robotics","physical computing","hardware","education"],"install":[{"cmd":"pip install gpiozero","lang":"bash","label":"Install via pip"},{"cmd":"sudo apt update && sudo apt install python3-gpiozero","lang":"bash","label":"Install on Raspberry Pi OS (Python 3)"}],"dependencies":[],"imports":[{"symbol":"LED","correct":"from gpiozero import LED"},{"symbol":"Button","correct":"from gpiozero import Button"},{"symbol":"gpiozero (full import)","correct":"import gpiozero"},{"note":"gpiozero is a higher-level abstraction; RPi.GPIO is an underlying library with a different API. Do not mix their usage without understanding pin factories.","wrong":"import RPi.GPIO","symbol":"RPi.GPIO (common mistake)"}],"quickstart":{"code":"from gpiozero import LED\nfrom signal import pause\n\n# Replace 17 with your actual GPIO pin number (BCM numbering)\nled = LED(17)\n\nled.blink()\npause()","lang":"python","description":"This quickstart code demonstrates how to make an LED connected to GPIO pin 17 blink. The `pause()` function keeps the script running indefinitely, allowing the LED to continue blinking."},"warnings":[{"fix":"Migrate your code to Python 3.9 or newer. Ensure your environment uses Python 3.","message":"Python 2.x support was removed in GPIO Zero 2.0. The minimum supported Python version is now 3.9.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Pass initialized `Motor` or `PhaseEnableMotor` objects to the `Robot` constructor. The old tuple-based method is deprecated and will be removed.","message":"The `Robot` class constructor API changed in 2.0. It now expects `Motor` or `PhaseEnableMotor` instances, not tuples of pin numbers.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"If your project relies on the previous default, explicitly set `GPIOZERO_PIN_FACTORY=rpigpio` as an environment variable or set it in your code using `gpiozero.Device.pin_factory = 'rpigpio'`.","message":"The default pin factory changed from `RPiGPIOFactory` to `LGPIOFactory` in version 2.0. This might affect advanced users relying on specific underlying pin library behaviors.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Rename your script to anything other than `gpiozero.py` (e.g., `my_gpio_project.py`).","message":"Naming your Python script `gpiozero.py` will cause an `ImportError` or unexpected behavior because Python will try to import your script instead of the installed library.","severity":"gotcha","affected_versions":"All"},{"fix":"Always assign the function object, not its return value, to event handlers: `button.when_pressed = my_function`.","message":"Assigning the *result* of a function call to an event handler (e.g., `button.when_pressed = my_function()`) instead of the function itself (`button.when_pressed = my_function`) will prevent the handler from being called.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Rename your script file to something different, like `my_project.py` or `led_blink.py`.","cause":"You have named your Python script 'gpiozero.py', causing Python to try and import your own script instead of the actual gpiozero library.","error":"Traceback (most recent call last):\n  File \"gpiozero.py\", line 1, in <module>\n    from gpiozero import LED\nImportError: cannot import name 'LED' from 'gpiozero' (/path/to/your/gpiozero.py)"},{"fix":"Ensure all previous scripts using GPIO have terminated cleanly. You can also explicitly close devices with `device.close()`. If the issue persists, reboot your Raspberry Pi.","cause":"The GPIO pin you are trying to use is currently in use by another program or a previous script did not properly clean up its GPIO usage.","error":"RuntimeError: Failed to add edge detection. (or similar 'GPIO Busy' error)"},{"fix":"Use the correct event handler property, for example, `button.when_pressed = my_callback_function` to assign a function to be called when the button is pressed. If you intended to check the state, use `button.is_pressed`.","cause":"You are trying to access a non-existent attribute. Event handlers are typically set via `when_pressed` (a property for assigning a callback), not directly accessed as `pressed`.","error":"AttributeError: 'Button' object has no attribute 'pressed'"}]}