# GPIO Zero library

The <mark style="color:blue;">GPIO Zero</mark> library offers a simplified approach to interact with the GPIO pins on a Raspberry Pi, providing an intuitive way to control external devices like LEDs, buttons, and sensors.&#x20;

This library abstracts the complex details of GPIO handling, making it accessible for beginners yet flexible enough for advanced users. Below is a summary of the basic recipes and best practices for using GPIO Zero in your projects:

#### <mark style="color:green;">**Importing GPIO Zero**</mark>

Before using GPIO Zero, you need to import the necessary classes. You can import specific components like `Button` or `LED`, or import the entire library.

```python
from gpiozero import Button
# or
import gpiozero
```

#### <mark style="color:green;">**Pin Numbering**</mark>

GPIO Zero uses Broadcom (BCM) pin numbering. You can also use physical (BOARD) numbering or other schemes by specifying prefixes.

```python
led = LED(17)  # Using BCM numbering
led = LED("BOARD11")  # Using physical numbering
```

#### <mark style="color:green;">**Basic Component Usage**</mark>

Here's how you can interact with basic components using GPIO Zero:

<mark style="color:purple;">**LED**</mark>

Turning an LED on and off:

```python
from gpiozero import LED
from time import sleep

red = LED(17)

while True:
    red.on()
    sleep(1)
    red.off()
    sleep(1)
```

<mark style="color:purple;">**Button**</mark>

<mark style="color:blue;">Detecting button presses</mark>

<pre class="language-python"><code class="lang-python">pfrom gpiozero import Button

<strong>button = Button(2)
</strong>
while True:
    if button.is_pressed:
        print("Button is pressed")
    else:
        print("Button is not pressed")
</code></pre>

#### <mark style="color:purple;">**Advanced Components and Usage**</mark>

GPIO Zero also supports more complex interactions, such as controlling traffic lights, reading from sensors, and controlling motors.

<mark style="color:blue;">**Traffic Lights**</mark>

Controlling a set of traffic lights:

```python
pythonCopy codefrom gpiozero import TrafficLights
from time import sleep

lights = TrafficLights(2, 3, 4)

while True:
    lights.green.on()
    sleep(10)
    lights.amber.on()
    sleep(1)
    lights.red.on()
    sleep(10)
    lights.off()  # Turn off all lights
```

<mark style="color:purple;">**Servo Motors**</mark>

<mark style="color:blue;">Controlling a servo motor:</mark>

```python
from gpiozero import Servo
from time import sleep

servo = Servo(17)

while True:
    servo.min()
    sleep(1)
    servo.mid()
    sleep(1)
    servo.max()
    sleep(1)
```

#### <mark style="color:purple;">**Interactive Projects**</mark>

GPIO Zero allows for creating interactive projects where components respond to input from sensors or buttons.

<mark style="color:purple;">**Button-Controlled LED**</mark>

<mark style="color:blue;">Turn an LED on and off using a button</mark>

```python
from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(2)

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

pause()
```

<mark style="color:purple;">**Robot**</mark>

Control a robot with basic movements:

```python
from gpiozero import Robot
from time import sleep

robot = Robot(left=(4, 14), right=(17, 18))

robot.forward()
sleep(5)
robot.right()
sleep(2)
robot.stop()
```

#### <mark style="color:blue;">**Best Practices**</mark>

* Keep your script running to maintain control over GPIOs, using `signal.pause()` or a loop that waits for an event.
* Clean up GPIO assignments at the end of your script to avoid conflicts on subsequent runs.
* Use `try...except...finally` blocks to handle exceptions and perform GPIO cleanup, ensuring that resources are released properly.

By following these guidelines and utilizing the GPIO Zero library, you can create a wide range of projects with your Raspberry Pi, from simple LED blinkers to complex interactive robots.
