View All Posts
read
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
#ACTION PLUGIN #COIL GENERATION #KICAD #PCB DESIGN #PCBNEW #PYTHON

I’ve been making a KiCad PCBNew action plugin recently for generating coils - you can watch a video of the results below, but I wanted to capture what I had learnt for future reference - mostly so that I have something to refer to, but hopefully this will be useful to others as well.


There’s some pretty reasonable documentation here: https://dev-docs.kicad.org/en/python/pcbnew/ that gives you the basic structure of a plugin, but beyond that you’ll need to look at other peoples examples and the auto-generated documentation - http://docs.kicad.org/doxygen-python/namespacepcbnew.html

Quite often you’ll have to dive into the C++ code to really understand what is going on and how to use the API.

The first thing you will need to do in your plugin is get hold of the current board:

board = pcbnew.GetBoard()

Once you have this you can start adding things to it. You will probably want to add things to a particular layer and you may also want to assign tracks to a particular net. You can get the layer and net objects from the board:

front_copper = pcbnew.F_Cu
back_copper = pcbnew.B_Cu
front_silk = pcbnew.F_SilkS
back_silk = pcbnew.B_SilkS

If you go here: https://docs.kicad.org/doxygen-python/namespacepcbnew.html and search for F_Cu you’ll find the complete list of layer constants.

# find the matching net for the track
net = board.FindNet("NET NAME")
# optionally you can create the net if it doesn't exist - handy if you are creating a PCB from scratch
if net is None:
    net = pcbnew.NETINFO_ITEM(board, "NET NAME")
    board.Add(net)

Once you have the layer and the net you can to use you can create tracks - you just need to specify the start and end points along with the width. You can also specify the layer and net:

track = pcbnew.PCB_TRACK(board)
track.SetStart(pcbnew.wxPointMM(x1, y1))
track.SetEnd(pcbnew.wxPointMM(x2, y2))
track.SetWidth(int(thickness * pcbnew.IU_PER_MM))
track.SetLayer(layer)
track.SetNetCode(net.GetNetCode())
board.Add(track)

Similarly you can create vias - you just need to specify the position, the diameter and the drill size. You can also specify the layer and net:

pcb_via = pcbnew.PCB_VIA(board)
pcb_via.SetPosition(pcbnew.wxPointMM(x, y))
pcb_via.SetWidth(int(via_diameter * pcbnew.IU_PER_MM))
pcb_via.SetDrill(int(via_drill_diameter * pcbnew.IU_PER_MM))
pcb_via.SetNetCode(net.GetNetCode())
board.Add(pcb_via)

One thing that I did find was that I needed to add the vias after I had added the tracks - if I didn’t do this it seemed to cause some errors when running the DRC checks.

Adding silk screen text is also pretty straightforward:

pcb_txt = pcbnew.PCB_TEXT(board)
pcb_txt.SetText("Hellorld")
pcb_txt.SetPosition(pcbnew.wxPointMM(x, y))
pcb_txt.SetHorizJustify(pcbnew.GR_TEXT_HJUSTIFY_CENTER)
pcb_txt.Rotate(pcbnew.wxPointMM(x, y), text["angle"])
pcb_txt.SetTextSize(pcbnew.wxSizeMM(size, size))
pcb_txt.SetLayer(pcbnew.F_SilkS)
board.Add(pcb_txt)

If you want the text to flip to the other side of the board them you specify a bottom silk later, or you can just flip the text:

pcb_txt.Flip(pcbnew.wxPointMM(x, y), True)

The first argument is the point to flip around, and the second argument is whether to mirror the text as well.

You’ll probably also need some way of connecting things to your PCB - pins and pads to connect leads components and leads to your board.

I haven’t yet worked out how to add a footprint from the footprint libaries to the board, but I have worked out how to add pins and pads.

To create a plated through hole pin you can do this:

module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
board.Add(module)
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(pin_diameter, pin_diameter))
pcb_pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_PTH)
pcb_pad.SetLayerSet(pcb_pad.PTHMask())
pcb_pad.SetDrillSize(pcbnew.wxSizeMM(pin_drill, pin_drill))
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad.SetNetCode(net.GetNetCode())
module.Add(pcb_pad)

And to create a pad you can do this - I’m still not sure on the use of LSET here, but it seems to work - if you change F_Cu to B_Cu the pad will be on the back of the board:

lset = pcbnew.LSET()
lset.AddLayer(pcbnew.F_Cu)
module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
board.Add(module)
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(width, height))
pcb_pad.SetShape(pcbnew.PAD_SHAPE_RECT)
pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_SMD)
pcb_pad.SetLayerSet(pcb_pad.SMDMask())
pcb_pad.SetLayerSet(lset)
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
pcb_pad.SetNetCode(net.GetNetCode())
pcb_pad.Flip(pcbnew.wxPointMM(x, y), False)
module.Add(pcb_pad)

To make mounting holes you can create pins, but change them so that they are not plated through holes:

module = pcbnew.FOOTPRINT(board)
module.SetPosition(pcbnew.wxPointMM(x, y))
board.Add(module)
pcb_pad = pcbnew.PAD(module)
pcb_pad.SetSize(pcbnew.wxSizeMM(diameter, diameter))
pcb_pad.SetShape(pcbnew.PAD_SHAPE_CIRCLE)
pcb_pad.SetAttribute(pcbnew.PAD_ATTRIB_NPTH)
pcb_pad.SetDrillSize(pcbnew.wxSizeMM(diameter, diameter))
pcb_pad.SetPosition(pcbnew.wxPointMM(x, y))
module.Add(pcb_pad)

Finally you need to add edge cuts to the board - this is the outline of the board - to make a circular board you can do this:

circle = pcbnew.PCB_SHAPE(board)
circle.SetShape(pcbnew.SHAPE_T_CIRCLE)
circle.SetFilled(False)
circle.SetStart(pcbnew.wxPointMM(CENTER_X, CENTER_Y))
circle.SetEnd(pcbnew.wxPointMM(CENTER_X + radius, CENTER_Y))
circle.SetCenter(pcbnew.wxPointMM(CENTER_X, CENTER_Y))
circle.SetLayer(pcbnew.Edge_Cuts)
circle.SetWidth(int(0.1 * pcbnew.IU_PER_MM))
board.Add(circle)

Note the use to SetStart and SetEnd to set the radius of the circle - there’s no SetRadius method.

Hopefully the above will be useful for other people. I’ll add and update this post as I learn more and correct any errors in my code…

#ACTION PLUGIN #COIL GENERATION #KICAD #PCB DESIGN #PCBNEW #PYTHON

Related Posts

Scripting KiCad to make coils - I've been diving deep into the world of PCB (Printed Circuit Board) coils, inspired by Carl Bugeja's incredible PCB motors! After deciding to set up an automated process for creating the coils rather than manually drawing them (can you imagine?), I found our main constraints became track width and spacing, and the number of layers we could use. During the process of creating spirals for our coils, I encountered some interesting problems - spirals are easy, but what about arbitrarily shaped coils? My attempts ultimately lead me to develop more efficient algorithms and KiCad plugins to generate these special coils. A rewarding challenge of math, programming, and electronics! My code has been published on GitHub, and I’m eager to see how others might make use of it!
Easy kicad symbol footprint and 3d - After many months of using EasyEDA for PCB design due to its tight integration with LCSC's parts catalog, I switched to KiCad for one critical feature: 3D model exports for designing enclosures. Despite KiCad's vast library, sourcing symbols, footprints, and 3D models for less common parts was no picnic. Luckily, the 'easyeda2kicad' Python package became a game-changer, converting EasyEDA models from LCSC into KiCad-compatible formats. It's a neat workaround, though one must stay vigilant to verify footprints against component datasheets to avoid manufacturing mishaps. Blend freeware magic with a dash of tinkering in CAD software, and voilà - your custom PCB design is practically popping out of the screen!
Axial Flux PCB Motors - Wedge or Spiral Coils - Which is best? - I've delved deep into the reason why people prefer to use wedge-shaped coils when building PCB motors. After a lot of Googling, a bit of Python scripting, and a ton of visuals, what emerged was a very compelling argument for the humble wedge coil. Turns out, radial lines are the heroes in the story – the more the merrier – because these are what generate the fields in the correct direction to crank up the torque. Don’t get too attached though; there may be alternative, more efficient coil designs yet to discover, but those are topics for another day!
The PCBs are in production - what have I messed up? - After some stress and trepidation, I finally took the plunge and sent my PCB design off for manufacturing. My design centers around building a large seven-segment clock with LED filaments. Jumping hurdles such as voltages, pin usage, and limiting the load on my power supply, I've settled on the ESP32 as the system's heart and come up with a final circuit design. While doing this, I've quickly realized I could improve my layout and fixed a small mistake. Also, I've prepared for either types of LED filaments - the high-voltage ones or the larger, 3v ones. However, I did bungle up a couple of things on the enable line of the shift registers and board layout. But hey, this is a learning curve, right? Can't wait to get the boards and see what other exciting errors surface!

Related Videos

Create Powerful PCB Coils with Automation - KiCad Plugin Secrets! - Delve into the fascinating process of creating custom PCB coils, drawing inspiration from Carl Bugeja's PCB Motors. Learn how to generate spirals, tackle challenges in creating arbitrary coil shapes, and develop a KiCad plugin to automate the entire process.
Easy - KiCad Symbol, Footprint and 3D Model! - In this video, I walk you through the transition from EasyEDA to KiCad for your PCB designs. While we lose the tight integration with LCSC parts, we gain the ability to export 3D models in KiCad. I discuss the challenges of finding certain components and how a handy script can help pull LCSC components into KiCad. We also explore how to position and adjust components correctly. Needing enclosures for your design? We discuss how to export these 3D models for review and even import them into Fusion360. To top it off, I mention the value in using PCBWay for your PCB needs, including 3D printing and CNC work. It's a whirlwind of information, aimed to help you navigate and enhance your PCB design workflow.
KiCad Tutorial - Custom Symbol, Footprint and 3D Model - Learn how to create custom footprints, symbols, and 3D models in KiCad 6 with this step-by-step guide. Design your own LED filament for a PCB project and watch it come to life in a 3D preview!
Mastering KiCad: Design & Build Your Custom Stereo Amplifier Board! - Learn how to create a version 2 of an I2S stereo amplifier board using KiCad in this engaging tutorial. Find out how to set up design rules, import symbols and footprints, generate the PCB layout, and add wiring and custom board outlines.
Sneak Preview of work in progress. - Dive into the fascinating world of PCB coils with this informative video, showcasing innovative ways to create and experiment with them. Get ready to be inspired by @CarlBugeja's groundbreaking work!
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


Published

> Image

atomic14

A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts