This final lesson takes a different approach. Instead of drawing schematics and placing components by hand, you will define the ATmega328P breakout board from Lesson 1 entirely in Python code. The circuit becomes a script you can version control, parameterize, and regenerate on demand. #KiCad #Python #SKiDL
Why Code-Based PCB Design?
Code Your Circuits
Every board you designed in Lessons 1 through 8 lives in binary KiCad project files. They work, but they are hard to diff, hard to parameterize, and impossible to generate programmatically. Code-based PCB design solves these problems:
Version control: a Python script diffs cleanly in Git, line by line. A binary .kicad_sch file does not.
Parametric designs: change one variable (crystal frequency, number of I/O pins, LED count) and regenerate the entire board.
Automation: generate 50 board variants from a single script with a loop.
Testing: write unit tests for your circuit definitions, catching wiring errors before you ever open KiCad.
Reproducibility: anyone can run your script and get the exact same netlist, every time.
Review: code review a circuit the same way you review firmware. Pull requests for hardware.
If you have taken the Code-Based Mechanical Design course, this is the electronics equivalent: replacing a GUI workflow with a script that produces the same output, but with all the benefits of code.
Tools Overview
Two Python tools make code-based PCB design practical with KiCad:
SKiDL
A Python library for defining electronic circuits in code. You declare parts, connect pins to nets, and SKiDL generates a KiCad-compatible netlist. It replaces the schematic editor for circuit definition. Install with pip install skidl.
KiCad pcbnew API
KiCad’s built-in Python module for manipulating PCB files. It can load netlists, place components at specific coordinates, set board outlines, run DRC, and export manufacturing files. It replaces manual interaction with the PCB editor.
How they work together: SKiDL handles the “what” (which components, how they connect), while pcbnew handles the “where” (component placement, board outline, routing). SKiDL outputs a .net file that pcbnew imports, just as if you had drawn a schematic and exported a netlist through the GUI.
Setup
Install KiCad 9
You need KiCad installed for its symbol and footprint libraries. If you have been following this course, KiCad is already on your machine.
Run this script. If it prints the part details without errors, your environment is ready.
Building the ATmega328P Breakout in Code
This is the main event. You will recreate the entire Lesson 1 circuit (ATmega328P, crystal, voltage regulator, LEDs, ISP header, pin headers) as a Python script using SKiDL.
Power Supply Section
Start with the power input and 5V regulation, exactly matching Lesson 1’s barrel jack and AMS1117-5.0 circuit:
Each Part() call mirrors a component you placed in the Lesson 1 schematic. The += operator connects a pin to a net, replacing a wire in the schematic editor.
To use the circuit, combine all the sections above into a single file called atmega328p_breakout.py. The structure is straightforward: imports at the top, net declarations, then each subsection (power, MCU, crystal, reset, ISP, LEDs, headers), ending with ERC() and generate_netlist(). Run it with python atmega328p_breakout.py and SKiDL produces a .net file in the current directory that KiCad can import directly.
From Netlist to PCB
Once you have the netlist, you need to get it into a PCB layout. There are two paths: manual import through the KiCad GUI, or scripted placement using the pcbnew Python API.
Manual Import
Open KiCad and create a new project.
Open the PCB Editor (pcbnew).
File, Import Netlist and select the .net file generated by SKiDL.
Place components manually, just as you did in Lesson 1. All footprints, net connections, and reference designators come from the netlist.
Route traces, add copper pours, and run DRC as normal.
This is the simplest approach. You get the benefit of code-defined circuits with the familiar KiCad layout workflow.
Scripted Placement with pcbnew
For full automation, use KiCad’s pcbnew Python module to place components at specific coordinates:
"""
Basic pcbnew script to set up board outline and place components.
Run inside KiCad's scripting console or with the standalone pcbnew Python module.
print("Components placed. Open the board in KiCad to route traces.")
Parametric Design Example
The real power of code-based design is parameterization. Wrap the breakout circuit in a function that accepts parameters, and you can generate multiple board variants from one script. Here are the key parametric sections:
Three different board variants, three function calls. The breakout_minimal variant uses an 8 MHz crystal with different load capacitors, a single power LED, and only 8 I/O pins. The breakout_extended variant runs at 20 MHz with four LEDs. Each produces its own netlist file.
Automated DRC with Python
KiCad’s pcbnew module lets you run Design Rule Checks programmatically. This is useful in CI/CD pipelines or for batch-checking multiple board variants:
"""
Run DRC on a KiCad PCB file and report results.
Requires KiCad's pcbnew Python module.
"""
import pcbnew
defrun_drc(pcb_path):
"""Run DRC on a board file and return pass/fail with details."""
You can integrate this into a build script or GitHub Actions workflow. Every push to your repository triggers DRC, catching clearance violations and unconnected nets before you order boards.
KiCad Action Plugins
KiCad supports Python plugins that run inside the GUI, adding custom menu items and functionality. Here is a minimal plugin that stamps board information onto the silkscreen layer:
"""
KiCad Action Plugin: Add Board Info Text
Place this file in KiCad's scripting/plugins directory.
txt.SetPosition(pcbnew.FromMM(25,38)) # Near bottom center
txt.SetLayer(pcbnew.F_SilkS)
txt.SetTextSize(pcbnew.FromMM(1,1))
txt.SetTextThickness(pcbnew.FromMM(0.15))
board.Add(txt)
pcbnew.Refresh()
print(f"Added board info: {info_text}")
BoardInfoPlugin().register()
After placing the file in the plugins directory, restart KiCad. The plugin appears under Tools, External Plugins. One click adds a formatted label with the board name, revision, and date.
Limitations and the Road Ahead
Code-based PCB design is powerful but not a complete replacement for the GUI workflow. Here is an honest assessment:
What works well today:
Circuit definition and netlist generation (SKiDL is mature and reliable)
Parametric board variants from a single codebase
Automated DRC and manufacturing file export
Version control and code review for hardware designs
What is still maturing:
SKiDL generates netlists, not visual schematics. You lose the ability to glance at a schematic diagram for quick understanding. Some teams generate a schematic from the netlist after the fact for documentation.
Automated component placement works for simple boards but requires manual tuning for complex layouts.
Automated routing is not built into KiCad’s Python API. External tools like FreeRouting can help, but interactive routing in the GUI is still faster for most boards.
The pcbnew Python API changes between KiCad versions, so scripts may need updates when you upgrade.
The practical workflow:
The most effective approach combines both methods. Use Python and SKiDL for circuit definition (the “what”), then switch to KiCad’s GUI for layout refinement (the “where”). You get version control and parameterization for the circuit while keeping the visual, interactive layout tools where they matter most.
What You Have Learned
Course Summary: PCB Design with KiCad
Over nine lessons, you progressed from a beginner placing through-hole components to designing multi-layer mixed-signal boards and generating circuits from Python code:
Fundamentals (Lessons 1 and 2):
Schematic capture: symbols, wires, power flags, net labels, ERC
Battery management: charging ICs, solar input, deep sleep circuitry
High-speed design: QFN packages, USB signal integrity, PIO interfaces
Mixed-signal design: motor drivers, IMU integration, current sensing, thermal management
Code-Based Design (Lesson 9):
SKiDL for programmatic circuit definition and netlist generation
Parametric design: one script, many board variants
Automated DRC with KiCad’s pcbnew Python API
KiCad action plugins for custom GUI extensions
When to use code vs. GUI in a practical workflow
Comments
Loading comments...
Predict first
You write a complete circuit as a Python script: every part and every connection. You run the script and it reports no errors. What do you have now?
Check yourself
Three questions. Each wrong choice is a mistake people really make, so read the note after you answer.
1. A colleague calls the function of this lesson as make_breakout('12MHz', 20, 6, 'test'). What does the netlist contain?
2. In a new script, a colleague connects a power LED with d1[1] += led_net and d1[2] += gnd, where led_net comes from the resistor. ERC reports nothing. On the assembled board the LED never lights, and a meter shows 5 V across it. What is wrong?
3. The crystal on a prototype is wrong. To save time you change its value directly in the PCB Editor, and you order boards. A month later a colleague runs the script again, imports the new netlist, and orders a second batch. What is on the second batch?
Watch it build: from the netlist to placed parts
The netlist lists 19 parts and which pins are joined. It holds no positions.
After File > Import > Netlist, every footprint sits in one pile, joined by thin lines.
The script draws the 66 × 53 mm outline on Edge.Cuts.
Seven parts move to their coordinates. The other twelve are not in the script, so they stay.
U1's position is pin 1: from 16.5 mm it runs 33 mm, so its centre is at 33 mm, mid board.
The thin lines remain. The script does not route: you do, or FreeRouting does.
The board you will make (66 × 53 mm): the first lesson's breakout, this time from a Python program. Its netlist matches the drawn schematic's part for part and pin for pin, and DRC passes with nothing unconnected.
Coming here first?
This lesson writes the board of the first KiCad lesson as code. It assumes four skills. Each link opens the part that teaches it:
SKiDL 3.0 knows KiCad 10: the lines above use set_default_tool(KICAD10) and the two folders KICAD10_SYMBOL_DIR and KICAD10_FOOTPRINT_DIR. With KiCad 9, write KICAD9 in their place.
Where the libraries are: /usr/share/kicad/symbols for a Linux package; for the Flatpak, run flatpak info --show-location org.kicad.KiCad.Library.Symbols and add /files/symbols (the same for Footprints and /files/footprints).
Check: the test script prints Part created: R, value: 10k. Warnings that other KICAD5 to KICAD9 folders are missing are harmless.
Run it: what we found
The listing runs with SKiDL 3.0 and KiCad 10. The netlist holds 19 parts, and RESET joins the five pins the text names. ✓
It is a shorter circuit than the first lesson's board: no D3, J5, J6, C7 or C8; the jack's switch pin goes to ground; most signal nets get numbers (N$1) in place of names; and the footprints of the capacitors, resistors, crystal and chip differ from that board.
The whole board as code:atmega328p_breakout.py writes all 24 parts with the first lesson's values, footprints and net names. compare_netlists.py compares its netlist with the netlist of the drawn schematic, pin by pin:
python atmega328p_breakout.py
python compare_netlists.py atmega328p_breakout.net drawn.net
24 parts, 31 nets with 2 or more pins; differences: 0
Get drawn.net from the first lesson's project: kicad-cli sch export netlist --format kicadsexpr -o drawn.net atmega328p-breakout.kicad_sch.
With the listing as written, ERC gives three warnings: Insufficient drive current on VIN and GND. Nothing in the script feeds those nets; the supply comes from outside. Tell SKiDL so, as a PWR_FLAG does in the schematic, and ERC reports nothing:
for n in (vcc, gnd, vin):
n.drive = Pin.drives.POWER # needs: from skidl import Pin
KiCad 10: File > Import > Netlist... is where the text says. Check: all 19 footprints arrive in one pile, joined by thin lines.
Checked in KiCad 10
The script runs in KiCad 10.0.8 with KiCad's own Python. SHAPE_T_RECTANGLE, VECTOR2I_MM and Save are all there. It places the seven parts in its table; the other twelve stay where the import put them.
Run it outside KiCad with KiCad's Python: python3 place_parts.py board.kicad_pcb on Linux packages, or flatpak run --command=python3 org.kicad.KiCad place_parts.py board.kicad_pcb for the Flatpak. place_parts.py is the listing with one addition: it stops with a message if a part is not on the board yet.
KiCad 10 still has no router of its own. File > Export > Specctra DSN... and File > Import > Specctra Session... are the two doors to an outside router.
The whole board, from the script
The listing places seven of the parts. Give it the whole table and the program becomes the first lesson's board. place_board.py is the same script with all 24 parts, each where that board has it. The listing's seven positions were a sketch: its crystal sits about 19 mm from pins 9 and 10, and the full table puts it beside them.
1. Script atmega328p_breakout.py2. Netlist 24 parts, 31 nets3. Placed place_board.py4. Routed by hand, then DRC
Who does what. Script marks a step a program does; You marks a step in the PCB Editor.
Script.python atmega328p_breakout.py writes atmega328p_breakout.net. Check:compare_netlists.py against the first lesson's netlist prints differences: 0.
You. New project, PCB Editor, File > Import > Netlist..., save as atmega328p_breakout.kicad_pcb. Check: 24 footprints in one pile.
You.File > Board Setup: the first lesson's rules, clearance 0.3 mm, tracks 0.5 mm, and a Power class with 0.8 mm tracks for +5V, VIN, PWR+ and GND.
Script.python3 place_board.py atmega328p_breakout.kicad_pcb. Check: it prints 24 parts placed and nothing is left in the pile.
You. Open atmega328p_breakout_placed.kicad_pcb and save it as atmega328p_breakout.kicad_pcb (the plugin writes the file's name). Route every track, pour GND on both faces, and move any reference that sits on a pad.
Script.Tools > External Plugins > Add Board Info writes the name, revision and date; drag the text to a free spot.
Script. The kicad-cli lines further down write the Gerbers, the two drill files and the position file.
With no schematic beside the board, DRC has nothing to compare the board with, so its parity check is silent. Step 1 does that job: the netlist itself is checked against the drawn one. If DRC reports every footprint library as not enabled, the shell still holds the KICAD10_..._DIR settings from the setup, pointing where KiCad cannot read: run kicad-cli in a shell without them.
The back of the finished board: ground poured on both faces.
The listing above shows the idea, but it cannot run: the line with three dots stands for code, and u1 is never made. variants.py is the whole function. It asks for the crystal's load capacitance from its data sheet and works out the capacitors, so it cannot guess:
C1 = C2 = 2 x (CL - 5 pF), then the nearest standard value
make_breakout('breakout_full', '16MHz', cl_pf=16, io_pins=14, leds=2)
make_breakout('breakout_minimal', '8MHz', cl_pf=18, io_pins=8, leds=1)
make_breakout('breakout_extended', '20MHz', cl_pf=20, io_pins=14, leds=4)
breakout_full: 16MHz with 22pF load capacitors, 14 I/O pins, LEDs: 2
breakout_minimal: 8MHz with 27pF load capacitors, 8 I/O pins, LEDs: 1
breakout_extended: 20MHz with 27pF load capacitors, 14 I/O pins, LEDs: 4
All three pass SKiDL's ERC with no warning. A call with no load capacitance stops with ValueError: give the crystal load capacitance CL from its data sheet.
KiCad 10: use kicad-cli for the check
In KiCad 10.0.8, pcbnew.WriteDRCReport() stops Python with a crash when it runs outside the KiCad window (assert "process" failed in Pgm(), then exit code 139). Loading and changing a board still works; only the check fails.
So the check goes to kicad-cli, and Python reads its report. run_drc.py needs no KiCad module at all:
import json, subprocess
def run_drc(pcb_path, report="drc_report.json"):
subprocess.run(["kicad-cli", "pcb", "drc", "--schematic-parity", "--severity-all",
"--format", "json", "-o", report, pcb_path], check=True)
rep = json.load(open(report))
found = rep["violations"] + rep["unconnected_items"] + rep["schematic_parity"]
print("DRC PASSED" if not found else f"DRC FAILED: {len(found)} item(s)")
return not found
Check: on the first lesson's finished board it prints DRC PASSED. On a board with a missing track it lists each item and returns exit code 1. For the Flatpak, set KICAD_CLI="flatpak run --command=kicad-cli org.kicad.KiCad" first.
All five commands run in KiCad 10.0.8. With --exit-code-violations, DRC returns 5 when it finds anything, so a pipeline stops. Two changes we make: add --excellon-separate-th to the drill export, and --no-protel-ext to the Gerber export if you want the .gbr names the Plot dialog writes (kicad-cli otherwise names them .gtl, .gbl and so on). Without the first, kicad-cli puts plated and unplated holes in one file, which KiCad's own dialog marks not recommended.
KiCad 10 still loads these plugins. The folder is ~/.local/share/kicad/10.0/scripting/plugins/ on Linux (9.0 becomes 10.0); Tools > External Plugins > Open Plugin Directory opens it on any system, and Refresh Plugins loads a new file without a restart. We ran the plugin's Run() on a board: it writes the text on F.Silkscreen. Its text goes to (45, 44) mm on the sheet, which is on the board only when the outline starts at (0, 0). board_info_plugin.py places it inside any outline and reads the revision from the title block.
Checked in KiCad 10.0.8: the SWIG pcbnew module is still there, and it prints the same warning as in 9, that it will be removed in a future version. Everything in this lesson that needs no window (the netlist, the placement, the checks, the factory files) also runs with SKiDL and kicad-cli alone, which is the safer base for new tools.
Found this useful?
Discussion
Earlier comments
LleZfromFr9 Sept 2026
Nice writeup — the SKiDL + pcbnew combo is a solid workflow, and I like that you called out the maturing pain points honestly (no auto-routing, API changes between KiCad versions, losing the visual schematic).
On the pcbnew side specifically, I ran into a lot of the same rough edges (scripted placement, DRC via DRC_ENGINE, plugin scaffolding). Docs there are thin, so I ended up writing up what I learned — zones, vias, tracks, footprints, with worked examples — at scriptedpcb.com, in case it's useful alongside this. Still growing it, happy to add write-ups for specific gaps people hit.
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
Text size
←→
Previous and next card. On a turned card: Not yet, I knew it
SpaceEnter
Turn the card, show the answer
123
Overview, Recap, Test me
HomeEnd
First and last card
Esc
Close
← → or scroll · N notes · F full screen · B blank0:00
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.
Comments