Files
python-gpiozero/docs/index.md
Dave Jones b1913e5e39 Add events to all input devices
Fairly major tidy up of the hierarchy as well. There's now a trivial
base class: InputDevice which simply permits reading of state.
WaitableInputDevice descends from this and introduces waitable events
and callbacks, and provides a hook for calling them but needs further
machinery to activate that hook.

DigitalInputDevice (crap name?) descends from WaitableInputDevice and
uses the standard RPi.GPIO callback mechanisms to handle events. This is
intended for use with trivial on/off devices with predictably small
bounce times.

Next is SmoothedInputDevice (crap name?) which also descends from
WaitableInputDevice. This includes a background threaded queue which
constantly monitors the state of the device and provides a running mean
of its state. This is compared to a threshold for determining active /
inactive state. This is intended for use with on/off devices that
"jitter" a lot and for which a running average is therefore appropriate
or for devices which provide an effectively analog readout (like
charging capacitor timings).

MonitorSensor and LightSensor now descend from SmoothedInputDevice, and
Button descends from DigitalInputDevice. All "concrete" classes provide
event aliases appropriate to their function (e.g. when_dark,
when_pressed, etc.)
2015-09-22 12:48:09 +01:00

3.2 KiB

gpio-zero

A simple interface to everyday GPIO components used with Raspberry Pi

Why?

The "hello world" program in Java is at least 5 lines long, and contains 11 jargon words which are to be ignored. The "hello world" program in Python is one simple line. However, the "hello world" of physical computing in Python (flashing an LED) is similar to the Java program:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

red = 2

GPIO.setup(red, GPIO.OUT)

GPIO.output(red, True)

6 lines of code to flash an LED. And skipping over why GPIO is used twice in the first line; what BCM means; why set warnings to False; and so on. Young children and beginners shouldn't need to sit and copy out several lines of text they're told to ignore. They should be able to read their code and understand what it means. This module provides a simple interface to everyday components. The LED example becomes:

from gpiozero import LED

red = LED(2)

red.on()

Any guesses how to turn it off?

Implemented Components

  • LED
  • Buzzer
  • Button
  • Motion Sensor
  • Light Sensor
  • Temperature Sensor
  • Motor

Usage

LED

Turn an LED on and off repeatedly:

from gpiozero import LED
from time import sleep

red = LED(2)

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

Alternatively:

from gpiozero import LED

red = LED(2)
red.blink(1, 1)
sleep(10)

Buzzer

Turn a buzzer on and off repeatedly:

from gpiozero import Buzzer
from time import sleep

buzzer = Buzzer(3)

while True:
    buzzer.on()
    sleep(1)
    buzzer.off()
    sleep(1)

Button

Check if a button is pressed:

from gpiozero import Button

button = Button(4)

if button.is_active:
    print("Button is pressed")
else:
    print("Button is not pressed")

Wait for a button to be pressed before continuing:

from gpiozero import Button

button = Button(4)

button.wait_for_press()
print("Button was pressed")

Run a function every time the button is pressed:

from gpiozero import Button

def warning():
    print("Don't push the button!")

button = Button(4)

button.when_pressed = warning

Motion Sensor

Detect motion and light an LED when it's detected:

from gpiozero import MotionSensor, LED

pir = MotionSensor(5)
led = LED(16)

pir.when_motion = led.on
pir.when_no_motion = led.off

Light Sensor

Wait for light and dark:

from gpiozero import LightSensor

sensor = LightSensor(18)

while True:
    sensor.wait_for_light()
    print("It's light! :)")
    sensor.wait_for_dark()
    print("It's dark :(")

Run a function when the light changes:

from gpiozero import LightSensor, LED

sensor = LightSensor(18)
led = LED(16)

sensor.when_dark = led.on
sensor.when_light = led.off

Temperature Sensor

Retrieve light sensor value:

from gpiozero import TemperatureSensor

temperature = TemperatureSensor(6)

print(temperature.value)

Motor

Drive two motors forwards for 5 seconds:

from gpiozero import Motor
from time import sleep

left_motor = Motor(7)
right_motor = Motor(8)

left_motor.on()
right_motor.on()
sleep(5)
left_motor.off()
right_motor.off()