PyRaspBeta

Wires and chains

A wire joins two pins. Writing a >> b wires pin a to whatever b is and gives back the right-hand side, so a chain reads left to right and each link passes its far end to the next:

circuit.py
from wiring import *

pi = Pi()

pi.GPIO17 >> Resistor(330) >> LED("red") >> pi.GND

far = connect(pi.GPIO27, Resistor(330), color="wire-orange")
far >> LED("green") >> pi.GND
main.py
from gpiozero import LED
from time import sleep

red = LED(17)
green = LED(27)

while True:
    red.on()
    green.off()
    sleep(0.5)
    red.off()
    green.on()
    sleep(0.5)

Both wires do the same thing: connect() is the function form, and it is the one that takes a colour. >> on a pin is shorthand for connect without a colour.

Chains through two-terminal parts

A part with exactly two terminals is a chain link. Its catalogue entry names the two ends in order, and the chain uses them as a near end and a far end:

  • a wire into the part connects to its near end;
  • >> returns its far end for the next link.

For the resistor the ends are 1 → 2 and for the LED they are A → K, so pi.GPIO17 >> Resistor(330) >> LED("red") >> pi.GND wires GPIO17 to resistor lead 1, lead 2 to the LED anode, and the cathode to ground.

A part on the left of >> uses its far end, so the two directions join the same terminals:

pi.GPIO17 >> Resistor(330)      # GPIO17 to resistor lead 1, returns lead 2
Resistor(330) >> LED("red")     # resistor lead 2 to the LED anode, returns the cathode

A part that does not have two ends — the relay has six pins — cannot be a chain link. pi.GPIO17 >> Relay() raises a CircuitError that points at one of its pin attributes (dc_plus for the relay); wire the pin you mean instead: pi.GPIO17 >> relay.in_. Even a one-pin part such as the camera is not a chain link; name its pin. See the errors.

connect()

connect(a, b, color=None) wires the same way and returns the far side:

connect(pi.GPIO17, Resistor(330))       # returns the resistor's lead 2
connect(pi.GPIO17, LED("red"))          # returns the LED's cathode

When the left argument is itself a two-terminal part, the wire starts at that part's far end:

r = Resistor(330)
connect(r, LED("red"))                  # the resistor's lead 2 to the LED anode

Wire colours

Without a color, the library picks one from the two pins' labels:

  • a ground pin (GND, a name ending in GND, or - / DC-) gets "wire-black";
  • a power pin (3V3, 5V, +, VCC, VDD, VIN, DC+, V+, +5V, +12V) gets "wire-red";
  • anything else cycles through yellow, green, blue, orange, white, one colour per new wire.

Pass a colour name to connect to choose it yourself; the board draws "wire-red", "wire-black", "wire-yellow", "wire-green", "wire-blue", "wire-orange" and "wire-white":

connect(pi.GPIO27, Resistor(330), color="wire-orange")

>> always uses the automatic colour — if you care about the colour, use connect.

Repeats, and mistakes

  • Wiring a pair of pins that is already wired does nothing; the second wire is ignored.
  • The same wire counts whichever way round it is written, so connect(a, b) and connect(b, a) are one wire.
  • Wiring a pin to itself is a CircuitError — pick two different pins.
  • Only pins can be wired. connect() and >> accept pins and two-terminal parts, and anything else is a CircuitError.

Going deeper

  • The ends are catalogue data. ends comes from the part's shop entry, not from its drawing: the button is A1 → B1, the active buzzer is + → -. A part page shows its ends in the generated part summary; the pin naming page covers how the pins are spelled in Python.
  • Wires are just connections, not current direction. >> picks ends so chains read the way a schematic is drawn, but the board solves the circuit it is given; swapping the two ends of a resistor changes nothing electrically.
  • A chain through a part is two wires. pi.GPIO17 >> Resistor(330) >> LED("red") adds the wire GPIO17–lead 1 when the first >> runs, and the wire lead 2–anode when the second does. The returned far end is a real Pin, which is why the next >> works.
  • Every message above is spelled out on Circuit errors.