KiCad keyboard PCB generator

jeroen94704

23 Apr 2020, 09:28

Hi,

I've been wanting to create my own custom keyboard PCB's for a while, but was always put off by the prospect of having to manually position dozens of switch footprints correctly. Therefore, I did what every self-respecting software engineer would do and created a script that takes a layout from the Keyboard Layout Editor and generates a KiCad project (schematic + layout) out of that. It's pretty bare-bones at this point, and still misses some features I think are required (ISO-enter, rotated keys), but I thought it might be of interest to some here anyway. You can find the generator on github:

https://github.com/jeroen94704/klepcbgen

If anyone tries it feel free to provide feedback (both good and bad), so I can make improvements going forward.

Here are a couple of renders of the first PCB I created using this script (it's currently on order):

Image
Image

Regards,

Jeroen
Last edited by jeroen94704 on 23 Apr 2020, 11:18, edited 2 times in total.

Findecanor

23 Apr 2020, 11:16

Cool stuff. I'll definitely take a look and steal your ideas. ;)
I've done something similar: creating KLE and DXF output from a Python program (computed layout).

BTW, You haven't posted a license/copyright information.

jeroen94704

23 Apr 2020, 11:33

Thanks, I indeed forgot to add the license. Fixed now.

What was the input of your program that you generate a KLE output from?

Findecanor

23 Apr 2020, 12:34

jeroen94704 wrote:
23 Apr 2020, 11:33
What was the input of your program that you generate a KLE output from?
Oh, they were hardcoded into the Python program. I had to express the relations of keys to other keys in various ways: to rotate them while avoiding that they bump into other keys.
It's your KiCad output code that I want to steal. :evilgeek:

jeroen94704

23 Apr 2020, 13:07

It's all based on Jinja templates, which I've come to like a lot. I'm still tinkering with it though, and right now I feel it's pretty fragile, in the sense that the code and the templates cannot change independent of each other. This is because of the way nets work in the kicad layout format. I'm sure there is a better way to handle that though, preferably by letting KiCad do the work for me.

User avatar
swampangel

23 Apr 2020, 22:19

Wow, thanks so much for sharing this. I had dreamed about building a tool like that, but never took it past learning the very basics of kicad. Excited to try this code. :D

User avatar
ddrfraser1

24 Apr 2020, 17:58

This is awesome! Unfortunately I can't get it to work (

For those of us who are not software engineers, how do you execute the script from the command line?
I tried from cmd prompt, that didn't work. Then I ran as administrator from the file directory where I saved the klepcbgen file but when I try to execute the command I get an error: No module named 'jinja2'

Findecanor

24 Apr 2020, 20:52

Ah. Is it for Python 2 or Python 3?

User avatar
ddrfraser1

24 Apr 2020, 22:38

Findecanor wrote:
24 Apr 2020, 20:52
Ah. Is it for Python 2 or Python 3?
Wasn't sure if you were replying to me. I tried it with python 3.8, did nothing. Error: invalid syntax

kelvinhall05

26 Apr 2020, 03:07

Very interesting, good work :D

jeroen94704

26 Apr 2020, 19:49

ddrfraser1 wrote:
24 Apr 2020, 22:38
Wasn't sure if you were replying to me. I tried it with python 3.8, did nothing. Error: invalid syntax
Oh, sorry about the incomplete info. I guess I should add a "prerequisites" section to the readme.

You need to install the jinja2 python module. This is most easily done using pip from the command-line:

Code: Select all

pip install jinja2
Hope this helps. If you need more info don't hesitate to ask.

Jeroen

pcaro

26 Apr 2020, 22:35

ddrfraser1 wrote:
24 Apr 2020, 22:38
Findecanor wrote:
24 Apr 2020, 20:52
Ah. Is it for Python 2 or Python 3?
Wasn't sure if you were replying to me. I tried it with python 3.8, did nothing. Error: invalid syntax
It is running with python3.

Just do:

Code: Select all


git clone https://github.com/jeroen94704/klepcbgen
cd klepcbgen/
python3 -m virtualenv venv
source venv/bin/activate
pip install jinja2
python klepcbgen.py example_layout.json mykeyboard
Great job @jeroen94704 !

jeroen94704

28 Apr 2020, 12:17

jimbaker wrote:
27 Apr 2020, 12:25
Findecanor wrote:
23 Apr 2020, 11:16
Cool stuff. I'll definitely take a look and steal your ideas. ;)
I've done something similar: creating KLE and DXF output from a Python program (computed layout).

BTW, You haven't posted a license/copyright information.
Yeah, I've noticed that too. I think you should add a license/copyright information here.
There's a LICENSE file included in the repository

adamws

10 Jan 2021, 13:14

I did something similar but with slightly different approach, you can take a look: https://github.com/adamws/kicad-kbplacer

To be honest I didn't play with your project yet but I like some of the ideas. For example I do not handle schematics at all. On the other hand I think that for layout generation, using kicad API is better than templates. Perhaps we should join forces, you can hit me up on github if you are interested.

HuBandiT

10 Jan 2021, 15:54


adamws

11 Jan 2021, 23:36

HuBandiT wrote:
10 Jan 2021, 15:54
Don't forget SKiDL: https://xess.com/skidl/docs/_site/index.html
problem with SKiDL is that you don't get graphical representation of your schematic. I think it could be used only if you are going to use it for all components (key matrix + IC + others) which increases complexity significantly. My current focus is on automating tedious manual stuff i.e key matrix.

HuBandiT

12 Jan 2021, 15:45

oh sorry, I thought the microcontroller part of the circuit has a well working fixed schematic already, so that you could write that in skidl once, and then just generate the key matrix programmatically and connect the matrix row and column signals to free pins on the microcontroller. so that basically the whole schematic/netlist will be auto-generated by your script, and you won't have to open eeschema at all.

alternatively, you could look into how to do hierarchical schematics in KiCad, draw up the microcontroller part of the circuit in eeschema normally (visually), label the pins of the microcontroller intended for row/column connections from the key matrix in a predefined way. then your script generates another part of the schematics/netlist using the predefined labels, and then KiCad merges the two into a complete circuit schematic (even if you can't see some parts of that visually), and then for each keyboard you only have to do the PCB visually.

adamws

16 Feb 2021, 01:40

HuBandiT wrote:
12 Jan 2021, 15:45
alternatively, you could look into how to do hierarchical schematics in KiCad, draw up the microcontroller part of the circuit in eeschema normally (visually), label the pins of the microcontroller intended for row/column connections from the key matrix in a predefined way. then your script generates another part of the schematics/netlist using the predefined labels, and then KiCad merges the two into a complete circuit schematic (even if you can't see some parts of that visually), and then for each keyboard you only have to do the PCB visually.
this is what I decided to do ;) eventually I'd like to add options to generate micro-controller part as well.
Project is bit more mature now, you can take a look here: https://adamws.github.io/keyboard-tools ... guide.html and
http://keyboard-tools.xyz/

Feedback will be welcomed

Post Reply

Return to “Gallery”