Skip to content

Build reusable circuit blocks ​

A repeated circuit needs both an electrical boundary and a physical arrangement. In this lesson you will create an LED channel with two ports, then place two independent instances of it.

Start from a project made with pcbjs create. Replace its board entry with this small exercise:

ChannelsCompiled from the source below
Highlighted lines are this step’s focus
import PCB, { circuit, nets, route, type Ports } from '@pcbjs/core'
import { LED0603, R0603, rect } from '@pcbjs/lib'

const channel = circuit('channel', ({ VCC, GND }: Ports<'VCC' | 'GND'>, { nets: { led } }) => {
  const R = R0603('330').connect({ 1: VCC, 2: led })
  const D = LED0603('green').connect({ A: led, K: GND })
  return { R, D }
})

const ChannelRow = channel.layout((g, { at, side }) => (
  <g at={at} side={side}>
    <g.R at="-3.5 0" />
    <g.D at="3.5 0 180deg" />
    {route(g.R[2], g.D.A).width(0.3).Z()}
  </g>
), 'ChannelRow')

const { VCC, GND } = nets()
const CH1 = channel({ VCC, GND }, 'CH1')
const CH2 = channel({ VCC, GND }, 'CH2')

export default (
  <PCB name="Channels">
    <path layer="Edge.Cuts" d={rect(24, 18)} />
    <ChannelRow g={CH1} at="8.5 5" />
    <ChannelRow g={CH2} at="8.5 12" />
  </PCB>
)
4 placed parts · 2 tracks

Follow the boundary ​

VCC and GND are the connections the caller supplies. led belongs to the circuit; CH1 and CH2 each get a separate internal net. Both channels share the same component definitions and arrangement, while their handles remain distinct: CH1.R and CH2.R name different resistors.

The layout receives the instance as g. Passing at and side into its outer <g> makes the whole channel movable, rotatable and reversible as one placement. Returning every part from the circuit makes its identity available to the layout and to inspection.

Inspect the repeated design ​

Run npx pcbjs dev, select each channel and inspect its parts. The exercise routes only each resistor-to-LED connection: the supply and ground connections between channels still need board-level routing. Use the findings and ratsnest to identify that remaining work.

For a complete example, Blinky uses a related channel pattern three times. Explore its complete placement state below.

Reusable channel placementsCompiled from the source below
Highlighted lines are this step’s focus
// Reusable channel placements. Coordinates are millimetres, x right and y down.
import PCB, { route, pour, via } from '@pcbjs/core'
import { Brand, rect, roundedRect } from '@pcbjs/lib'
import * as sch from './blinky.sch'
import './blinky.sourcing'
import { layout as LedChannelRow } from './lib/led-channel'

const W = 30,
  H = 20

export default (
  <PCB name="Blinky" rev="0.1.0" schematic={sch} rules={{ clearance: 0.2, minTrackWidth: 0.2, requireSourcing: true }} autoroute={{ bend: '45', viaCost: 4 }}>
    <path layer="Edge.Cuts" d={roundedRect(W, H, 2)} />

    <J1 at="3 8" />
    <Q1 at="26 10" />
    <LedChannelRow g={CH1} at="16 5" label="LED1" />
    <LedChannelRow g={CH2} at="16 10" label="LED2" />
    <LedChannelRow g={CH3} at="16 15" label="LED3" />
    <C1 at="15 10" side="B" />

  </PCB>
)
9 placed parts · 0 tracks

Change only CH2's at to "18 12 90". Its resistor, LED and internal route should turn together, while CH1 stays in place. You now have a block whose electrical and physical reuse are explicit.

Continue with instance layout overrides. The Circuits reference explains ports, naming, nested instances and multiple layouts.

MIT licensed. A proof of concept, pre 0.1.