Blink

Let's start by blinking the LED on the Quick2Wire interface board.

The Raspberry Pi's header has 8 pins reserved for GPIO, numbered 0 to 7. The Quick2Wire interface board breaks these pins out to their own header and clearly labels them on the board. Pin 1 can be jumpered to the on-board LED, so you can flash an LED without having to do any additional wiring.

In the Quick2Wire API, software controls a physical GPIO pin via a Pin object. A Pin has a direction (In or Out) and a value (1 or 0) that can be read (if the Pin has direction In) or written (if the Pin has direction Out).

A program gets hold of a Pin object from a PinBank, which represents a collection of related pins indexed by pin number. The Quick2Wire API defines a PinBank for the Pi's 8 GPIO pins, called simply "pins". It also defines PinBanks for the Pi's header, indexed by header pin number 0 to 26 and by the pin numbers defined by the Broadcom SoC. The latter two are not used in this demo, so we'll talk of them no more.

Here's how to use a Pin to blink an LED.

#!/usr/bin/env python3
 
from quick2wire.gpio import pins, Out
from itertools import cycle
from time import sleep
 
led = pins.pin(1, direction=Out)
 
with led:
    for v in cycle([1,0]):
        led.value = v
        sleep(0.5)
 

The next example, button-blink, shows how to also read the state of a GPIO input pin connected to a push-button.

Generated with Code Guide.