Extracting footprint information from KiCAD

The source design I was working from

Sometimes being lazy is really hard work. I’ve got the PCB design for the Bluetooth Chord keyboard and I want to extract the key positions for the case. I’ve discovered the really useful KiCadSetupUp plugin for FreeCad CAD that lets me import my PCB as an object in drawing, but what I really want to do is get the positions of keys on the PCB so that I can cut holes in the case in exactly the right positions.

I could spend five minutes copying the values from the KiCad PCB design. But I’m lazy. I’d much rather spend most of a day discovering how I can run Python code inside KiCad and write a program that does it for me. There is a bit of method in my madness, in that if the design of the PCB changes (I’m going to have to move the keys a little bit to stop the keycaps from clashing) I just have to run my program again. There’s probably some kind of integrated manufacturing trick that lets you automagically import this kind of information into FreeCAD but I’m really too lazy to learn how that works. Instead I wrote this little program:

file = "C:\\Users\\rsmil\\pcbs\\keyboard\\flippedkeyboard.kicad_pcb"

board = pcbnew.LoadBoard(file)

o = board.GetOrigin()
x = o.x / 1000000.0
y = o.y / 1000000.0

l = []

for f in board.GetFootprints():
    b = f.GetBoundingBox()
    ref = f.GetReference()
    p = f.GetPosition()
    px = (p[0] / 1000000.0)-x
    py = (p[1] / 1000000.0)-y
    height = b.GetHeight() / 1000000.0
    width = b.GetWidth() / 1000000.0
    print(ref)
    t = '    makeKey("{0}",{1:.2f},{2:.2f},{3:.2f},{3:.2f})'.format(ref,px,py,height,width)
    l.append(t)

l.sort()
for t in l:
    print(t)

It prints out a function call for each footprint. This is what I get from my design:

makeKey("Control_1",100.07,21.29,17.38,17.38)
makeKey("Control_2",100.07,38.43,17.38,17.38)
makeKey("Control_3",100.07,55.58,17.38,17.38)
makeKey("Control_4",100.07,72.72,17.38,17.38)
makeKey("Control_5",100.07,89.87,17.38,17.38)
makeKey("Index_1",64.51,17.16,17.38,17.38)
makeKey("Index_2",64.51,33.99,17.38,17.38)
makeKey("Little_1",11.17,21.29,17.38,17.38)
makeKey("Little_2",11.17,38.43,17.38,17.38)
makeKey("Little_3",11.17,55.58,17.38,17.38)
makeKey("Little_4",11.17,72.72,17.38,17.38)
makeKey("Little_5",11.17,89.87,17.38,17.38)
makeKey("Middle_1",46.73,17.16,17.38,17.38)
makeKey("Middle_2",46.73,33.99,17.38,17.38)
makeKey("Ring_1",28.95,21.29,17.38,17.38)
makeKey("Ring_2",28.95,38.43,17.38,17.38)
makeKey("Ring_3",28.95,55.58,17.38,17.38)
makeKey("Ring_4",28.95,72.72,17.38,17.38)
makeKey("Ring_5",28.95,89.87,17.38,17.38)
makeKey("Thumb_1",82.29,21.29,17.38,17.38)
makeKey("Thumb_2",82.29,38.43,17.38,17.38)
makeKey("Thumb_3",82.29,55.58,17.38,17.38)
makeKey("Thumb_4",82.29,72.72,17.38,17.38)
makeKey("Thumb_5",82.29,89.87,17.38,17.38)

This runs inside my little Python layout program that builds the box (which I really need to write sometime). It turns out that it is very easy to run code inside KiCad, although some of the help online seems to be a bit out of date. You also need to be very careful with some parts of your code. If you get the path to the file wrong this won’t just crash your program, it will take out the KiCad program completely. I tried a number of different ways to catch the exceptions and errors that might be thrown by the LoadBoard function but I was never able to stop the crashing. Just make sure that your path is correct and that any backslash path separators are properly escaped as you can see above.

Grappling with KiCad

This is the view from the underneath of the board

One of my new year resolutions this year (apart for the one to by a 4K monitor - ho ho) was to make my own printed circuit board and have it made. The Bluetooth chord keyboard seems to be an ideal device for this. The number of connections is limited. I’m using the wonderful PICO LiPo shim (of which more later) for the power management so I just have to wire up the switches and the display.

I’ve played with KiCad before but I don’t make enough circuit boards to have built up much experience with it. Every time I use it I get tangled up in library management and fretting about schematic designs and footprint files. Today was no exception. I found a PICO-W design schematic on GitHub and tried to add it to my project. This did not go well. The model seemed to be missing important files (or at least I couldn’t find them). All the instructions seemed to imply that what you want to do is add your components to a library on your machine so you can easily add them to projects. I much prefer to make all my projects free-standing so I’ve put all my files in with the designs.

It got a lot easier once I worked out that libraries in KiCad are just folders that contain stuff, and when you browser a library you are just traversing that folder. If you put the files in the right place they just get picked up and all is well. You assign a footprint file to the schematic component and this is picked up by the PCB. Above I’ve used the “push button switch” component in the schematic. I then assign this component to the key socket that I’m going to be using on the board.

Above you can see the PCB view of part of the circuit. If I was using a different component I’d change the footprint file. The schematic and the footprint editor are used together. You can update the design in one from the other. You start by drawing the schematic and then you import that circuit into the footprint editor and start laying things out. After a while it becomes quite fun.

Fun with KiCad

After spending a chunk of today with KiCad I have managed to make a schematic. I managed to go from this error…

..to this error…

… to no errors. Now I just have to figure out how to make this design into a PCB.

Note that if you are into hardware development you should probably think about getting into KiCad. It really is a wonderful program. It’s free and it lets you design your own circuit boards.