View All Posts
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

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.

Related Content

[0:00] I’ve been messing around making PCB coils, but instead of drawing them by hand,

[0:04] which would be pretty frustrating I’ve automated the entire process.
[0:09] I’m working on a new project with one of my friends and we’ve taken

[0:12] inspiration from Carl Bugeja’s amazing work on PCB Motors.
[0:17] If you haven’t seen his channel then check it out it’s very very cool.
[0:21] Now it’s not clear if what we’re doing will actually work as well as his projects,

[0:26] but there’s only one way to find out.
[0:28] As always my PCBs will be manufactured by PCBWay more about them later.
[0:34] So, making coils on a PCB. You might think this

[0:38] is pretty trivial and for a basic coil, it actually is very simple.
[0:42] So we’ll start off doing this first and then move

[0:45] on to the more challenging problem of creating arbitrary coil shapes.
[0:49] For our PCB coils we want to create as many turns as possible.
[0:53] Our main constraint is going to be our track width

[0:56] and track spacing along with the number of layers that we can use.
[0:59] Looking at PCBWay we can see that prices start to go up very quickly as soon as

[1:05] we go below 5 mil for the track width and spacing.
[1:08] So this will be the smallest size we’ll try and use. This

[1:11] is equivalent to a track width and spacing of 0.127 millimeters each.
[1:16] Staying on the theme of keeping it cheap, we’ll also be using a

[1:20] two layer board. Going up to a four layer board would increase the cost considerably.
[1:24] It is possible that these constraints will make our coils too weak to do anything,

[1:28] but we’ll see once we have the pcbs manufactured.
[1:32] So how do we make a coil?
[1:34] Well all we need to do is generate a spiral.
[1:37] If we were just trying to make a circular track, then we would simply use these two equations.
[1:42] We would then generate points on the circle by sweeping the angle from 0

[1:46] to 360 degrees and keeping the radius constant.
[1:50] The X and Y coordinates of the points on the circle can be calculated using basic trigonometry.
[1:55] To make a spiral we just need to make the radius increase proportionally to the current Angle.
[2:01] Now we can calculate the coordinates on a spiral by sweeping our angle from 0 to

[2:05] 360 Degrees multiplied by the number of turns that we need.
[2:09] We’re going to also want to have a coil on the bottom layer of the board.
[2:12] So we’re going to need some space in the center for a via to connect the top and bottom layers.
[2:17] We can do this by starting our radius off with a small offset. We can now

[2:22] just join our top and bottom chords together using a via in the center.
[2:26] If we look at the direction of the magnetic field that will be created

[2:30] from the two coils we can see that they will cancel each other out.
[2:33] To fix this we just need to flip the bottom coil.
[2:36] The current will now flow into the top coil and out of the bottom coil in the same direction.
[2:40] The top and bottom coils will now reinforce each other.
[2:43] Generating this and turning it into tracks and vias results in this very nice looking

[2:48] PCB layout in KiCad. It’s pretty cool. We have the top layer coil and the bottom layer coil.
[2:54] If we apply power to the top coil the current will flow in

[2:57] this direction and it will also flow in the same direction on the bottom coil.
[3:01] Now we’re going to be trying to repeat some of Carl’s work and make a three-phase PCB motor.
[3:06] For this kind of motor we’re going to need three coils spaced 120 degrees around our PCB.
[3:12] The motor can be made more powerful by duplicating each

[3:16] coil on the opposite side of the PCB. So we end up with six coils in total.
[3:21] To make the wiring up of these coils easier I’m adding a 180 degree offset

[3:25] to the angle on the bottom coil so that the end of the coil finishes on

[3:29] the opposite side to the start of the coil on the top layer.
[3:31] We can now easily connect the pairs of coils

[3:34] together by connecting the ends of the coils on the bottom layer.
[3:37] To make the current flow in the correct direction we just

[3:40] need to flip the Y coordinates of the opposite coils again.
[3:43] Connecting the ends of the opposite coils together

[3:45] to a common Point gives us the classic star wiring pattern.
[3:49] If we connect A to the positive rail and B to the negative rail leaving C unconnected

[3:55] then the A coils will create north magnets and the B coils will create south magnets.
[3:59] Similarly connecting C to the negative rail will make the C coils create south magnets.
[4:04] If we now connect A to the negative rail and B to

[4:07] the positive rail we’ll get north from the B coils and south from the A coils.
[4:11] And so on as we drive the coils appropriately.
[4:14] So this is great, but if we look at some images of

[4:17] other people’s PCB stators we can see that they don’t always stick to circular coils.
[4:22] How about generating arbitrarily shaped coils?
[4:26] Now this turned out to be a surprisingly interesting problem.
[4:29] I’ve managed to get something that works pretty well, but I’m sure there

[4:33] must be other solutions - let me know in the comments if you know a good way of doing it.
[4:37] My initial attempts produce some very interesting shapes. I tried to take

[4:42] the existing code for creating spirals, but calculated the radius by sweeping an angle

[4:46] around and casting a ray out to find the intersection point with the template shape.
[4:51] I used the distance from the origin to this

[4:53] intersection point as the radius and then tried offsetting this.
[4:57] Now obviously, this doesn’t work very well. And if we think about

[5:00] what is happening with a normal spiral we can intuitively see that we are always

[5:04] offsetting points at approximately a normal to the template circle shape.
[5:08] So, what we need to do is offset the intersection point at right angles to the line it belongs to.
[5:14] If we do this then we get something that looks much better the only issue is though

[5:19] it doesn’t seem to handle corners - they are getting chopped off.
[5:22] My first attempt to solve this was simply to smooth the corners of the template shape.
[5:27] There’s a really nice algorithm we can use to do this automatically

[5:30] called Chaikin’s corner cutting algorithm.
[5:32] This iteratively smooths lines and gives a really nice smooth

[5:36] result after just a couple of iterations.
[5:39] If we do this first and then run our algorithm we get quite reasonable results.
[5:43] However, we’re not really ending up with the shape that we originally wanted.
[5:48] An alternative way to fix the corner cutter problem is to detect when we

[5:52] switch to a new line segment and create an intermediate point where the corner should be.
[5:56] We do this by calculating the intersection of the

[5:59] previously generated points with the next two generated points.
[6:03] This algorithm works really well and we get a nice looking coil.
[6:06] The only issue we face now is that we can miss small details in our template shape.
[6:11] We are sweeping a ray around a circle and unless

[6:14] we step with very small increments we can easily miss a line segment.
[6:18] But, if we step with really small increments then it takes a very long time to process.
[6:23] So, what I’ve ended up doing is taking each point on the template. I calculate the angle

[6:28] from the point to the origin and from that I can work out how much offset would be required.
[6:32] This generates pretty much the same results as our original sweeping ray algorithm,

[6:37] but it’s a lot faster and it doesn’t miss any line segments.
[6:40] This works really well and we can create pretty cool wedge-shaped coils.
[6:44] Here’s the version with six coils, and we can even generate 12 coils instead.

[6:48] The wiring up is a bit more complicated but it can still all be done programmatically.
[6:53] The 12 coil version still creates the same star-shaped circuit as the six coil version.
[6:58] Everything is fully automated using a KiCad plugin.
[7:00] Making a keycard plugin was an adventure in itself. In

[7:04] many ways it was almost as hard as actually getting the coil algorithm.
[7:07] There are a couple of issues with making KiCad plugins - they are pretty hard to develop and

[7:12] test - you need to edit your code, refresh the plugins, run the plugin, see if it works.
[7:17] It’s a slow process.
[7:19] It’s also quite hard to include additional Python libraries that your plugin might rely on.
[7:24] KiCad comes with its own installation of Python. In theory you can add new

[7:28] python libraries using the bundled pip installer.
[7:31] However, on modern Mac computers this causes all sorts of problems.
[7:35] Macs have an additional security layer that prevents applications from being

[7:39] modified. This does help prevent viruses and malware from infecting your computer,

[7:44] but unfortunately running pip install in the KiCad directory modifies the KiCad python installation.
[7:50] So the operating system thinks it’s no longer valid and won’t let you run it.
[7:55] To avoid all these issues I’ve developed all the complicated code in a Python notebook.

[8:00] This lets me develop the coil generation algorithms much more quickly and I can

[8:04] easily preview what it will look like by plotting the output directly in the notebook.
[8:08] The notebook produces very simple JSON representation of the tracks, the vias,

[8:13] silk and pads. And my KiCad plugin simply reads this and creates the required objects.
[8:18] It’s a really nice decoupling and it means we aren’t really tired to KiCad at all.
[8:22] We could use any PCB layout tool that supports scripting that can read JSON data.
[8:28] The KiCad python API is pretty difficult to get started with. The documentation is

[8:33] auto generated from header files, so it doesn’t really tell you how to use it.
[8:37] there are quite a few examples, but the API is pretty unstable and seems to change fairly often.
[8:43] You often have to spelunk into the C++ code to see how things actually work.
[8:47] A great example of this was just trying to work out how to draw a circle.
[8:51] I could not work out how to set the radius - there’s no setRadius function!
[8:56] Digging into the code it transpires that you

[8:59] use the distance between the start and end points to set the radius.
[9:02] This is one of the big issues with auto-generating documentation from header

[9:05] files - there’s no context to help you understand how to use the API.
[9:09] Having said that, it’s an open source and free project. So I’m not going to complain too much.

[9:15] If I really wanted to make it better I should contribute some documentation back to the project.
[9:20] I won’t go into too much detail on the KiCad plugin itself. It’s surprisingly easy to

[9:25] create everything we need including tracks, edge cuts, vias, pads and mounting holes.
[9:30] It’s very very cool and definitely worth investing some time in learning how it works.
[9:34] I’ve put my code up on GitHub, hopefully, it will help other people.
[9:39] My pcbs are being manufactured by PCBWay - we’ve used them for a lot

[9:42] of projects and they keep on delivering the goods.
[9:44] There’s now a really nice plug in for KiCad that will submit your order directly to them - so

[9:49] there’s no more messing around trying to work out how to export the Gerber files properly.
[9:53] If you haven’t tried them out then give them a go.
[9:55] I should get the pcbs pretty soon, so we’ll soon see if what I’ve done has any chance of working.
[10:00] I’ll see you in the next video!

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

> Image


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