PyRaspBeta

Buying and naming parts

Every part in the shop has a Python class, generated from its catalogue entry. from wiring import * gives you all of them, named after the part: LED, Resistor, Button, Servo and so on. Creating a part buys it — its price goes on the bill and the part is laid out on the board.

circuit.py
from wiring import *

pi = Pi()

pi.GPIO17 >> Resistor(330) >> LED("red") >> pi.GND
pi.GPIO27 >> Part("resistor", 330) >> Part("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)

Settings

A part's main setting can be passed positionally; any setting can be passed by name:

Resistor(330)           # the same as Resistor(ohms=330)
LED("red")              # the same as LED(color="red")
Relay("high")           # the same as Relay(trigger="high")
LED(color="green")
SlideSwitch(position="right")
Potentiometer(10000)

Only one positional argument is allowed, and only for parts that have a main setting. Settings are checked as they are set:

  • an enum setting must be one of its listed options, and its value is compared as text, so Potentiometer(10000) and Potentiometer("10000") both work;
  • a number setting must be a real number (a boolean is rejected) and lie inside its range — Resistor(0) is an error because resistance runs from 1 Ω up;
  • an unknown setting raises a CircuitError naming the settings that part does have.

A part with no main setting, like the button, takes no positional argument at all.

Any part by slug

Part("slug", …) is the generic constructor: it looks the slug up in the catalogue and builds the class that goes with it. It takes the same positional argument and settings as the class itself:

Part("dht22")
Part("resistor", 10000)             # positional, like Resistor(10000)
Part("resistor", ohms=10000)        # by name
Part("led", color="blue")

An unknown slug raises a CircuitError — shop() prints the catalogue below if you need the spelling.

Naming and placing

Both the generated classes and Part accept two extra keywords:

  • label="status" names the part: it is used in the part's id, in error messages and on the board;
  • at=(80, 10) pins the part to a whole-cell grid position instead of letting the layout pick one.
status = LED("green", label="status")
res = Resistor(330, at=(20, 5))

at= takes a pair of whole numbers; anything else is a CircuitError.

The bill

Creating a part buys it. The bill feeds into the contest score, so the wiring you choose has a price; the boards themselves are free. The built-in kits are billed as one kit item instead of their parts — see Kits.

shop() prints every part and kit with its price, for when you are in the workspace and want the list:

shop()                  # every part and kit, one per line, with its price

The prices above and below come from the catalogue, so they always match the shop. A resistor costs $0.25, an LED $1, and a distance sensor $3.

Every part in the shop

Basics

  • Resistor — $0.25 — Limits current. Resistor(330) for LEDs, Resistor(10000) for pull-ups.

Light

  • LED — $1 — Needs a resistor in series (330 Ω) or it burns out.
  • RGB LED — $2 — Three LEDs in one; each colour needs its own resistor.
  • NeoPixel stick (8) — $7 — 8 addressable RGB LEDs on GPIO18. neopixel.NeoPixel.
  • NeoPixel ring (12) — $10 — 12 addressable RGB LEDs on GPIO18.

Input

Sensors

Chips

Displays

Sound

Motion

  • Servo — $5 — Angle from a PWM pulse on SIG. Power it from 5 V. gpiozero.AngularServo.
  • DC motor + L298N — $12 — IN1/IN2 set direction, PWM on ENA sets speed.
  • Stepper + ULN2003 — $8 — Step it by writing the coil sequence to IN1–IN4.

Power

  • Relay module — $4 — Switches a load. Coil on DC+/DC−, signal on IN, contacts COM/NO/NC.
  • Heater — $10 — Warms its room. Switch it with a relay.
  • Fan — $6 — Airs its room. Switch it with a relay.
  • Water pump — $8 — Waters the soil. Switch it with a relay.
  • Lamp — $5 — Lights its room. Switch it with a relay.
  • Breadboard power supply — $5 — Separate 5 V / 3.3 V rails for hungry parts.

Each entry links to the part's own page: what it is, its pins, a wiring example and how to drive it from main.py. The pages live under the Hardware section, and each one shows the generated pin table and settings for that part. For the detail behind the pins, see Pin naming; for the wires between them, Wires and chains.

Going deeper

  • The classes are generated at build time from the catalogue and the component registry, which is why from wiring import * stays in step with the shop without anyone editing the library.
  • Every part is a Component with a TYPE (its slug). Subclassing it with a TYPE that is not in the catalogue raises a CircuitError; for your own reusable parts, subclass Module instead.
  • The bill is part of the score. A build over the task's budget cannot be submitted, so a cheaper wiring (or a kit) can matter as much as a faster program.
  • Part is the escape hatch for slugs that have not been given a friendly class name yet, and for building parts from a list in a loop.