Sl-Alex home lab

3D models for KiCAD. Part 1 - OpenSCAD

Views: 13150Comments: 1
Articles

Translation: RU
3D models for KiCAD. Part 1 - OpenSCAD

If you are hardware engineer and you spent some time for searching an open-source software then I'm pretty sure you know KiCAD project. This project is for creating schematics and printed circuit boards. It is cross-platform, easy to learn and stable project. One of its outstanding features is a support of a 3D models for components, so you have a possibility to view the complete board in 3D without assembling it. Unfortunately, many people think, that it's hard to create 3D model. Moreover, KiCAD supports only models created in Wings3D, which is quite hard to learn, especially for beginners. Fortunately, there are more clear and easy ways. One of them I will describe today. I talk about OpenSCAD.

As an example for this article I decided to take SSOP28 case. Why? Just because I need it in one of my projects. By the way, during modelling we will parametrize all dimensions, so after that you could be able to get any case from µSOP to LQFP with just several changes.

So, our model will be, let's say, a rectangular parallelepiped with arrays of pins on sides and a small notch near the first pin. We can draw it in the following sequence:

  • Create pin
  • Create pin array
  • Create case
  • Create notch in the case.
  • Align pin arrays around the case

Let's start from pin creation. Usually, pins are not completely described in datasheets, and they can differ between same cases from different manufacturers. So, our task is to create roughly the same shape, but with specified width, length and height.

Let's draw a pin as a polygon. Here we can choose one of two approaches: we can manually calculate all coordinates or specify several dimensions and calculate the rest automatically. First approach is easier for prototyping, but much harder if you decide to change something, so we will choose the second one and define the following dimensions:

Pin drawing Figure 1: Pin drawing

Assuming that pin thickness is constant, we can calculate all points and create pin:

// Bended pin
// _L - whole pin length
// _l - length of bottom part
// _H - whole pin height
// _th - pin thickness
// _a - bending angle
// _W - pin width
module pin(_L,_l,_H,_th,_a,_W)
{
    // Internal variables
    _d1 = _th*sin(_a) / (1-cos(_a));
    _d2 = (_th-_H)*cos(_a) / sin(_a);

    rotate([90,0,90])
        // Extrude the following polygon
        linear_extrude(height = _W, center = true, convexity = 10, twist = 0)
            polygon([[0,0],
                     [_l,0],
                     [_l+_d2,_H-_th],
                     [_L,_H-_th],
                     [_L,_H],
                     [_l+_d2-_d1,_H],
                     [_l-_d1,_th],
                     [0,_th]]);
}

// Example of usage
pin(2,1,1,0.2,115,0.5);

In this script you can see polygon definition, linear extrusion of this polygon and rotation of the result to the desired position.

Now we should get the following image in OpenSCAD viewer:

Pin Figure 2: Pin

This module can be used anywhere, including other modules.

Array of pins is much easier:

// Array of pins
// _cnt - number of pins
// _distance - distance between pin centres
module pins_array(_cnt, _distance)
{
    if (_cnt>0)
    {
        for (_i = [0:_cnt-1])
        {
            translate([_distance*_i,0,0])
            pin(pin_L,pin_l,pin_H,pin_th,pin_a,pin_W);
        }
    }
}

Our model is almost finished. We will not create exact model, we will just create a rectangle parallelepiped and substract a small cylinder to create a notch near the first pin.

// Simple case with notch
module case(){
    difference() {
        cube([case_x,case_y,case_z]);
        translate([dot_radius+dot_off_x,dot_radius+dot_off_y,case_z])
            cylinder(h=dot_depth*2, r = dot_radius, center= true, $fn = 10);
    }
}

Now all components are ready and we just align arrays of pins around the case:

// Complete model
union()
{
    // Top pins
    translate([(side1_pins-side3_pins)*pin_distance/2,case_y+pin_L*2,0])
        mirror([0,1,0])
            pins_array(side3_pins, pin_distance);

    // Bottom pins
    pins_array(side1_pins, pin_distance);

    // Left pins
    translate([-(case_x-(side1_pins-1)*pin_distance)/2 - pin_L,pin_L+case_y/2-(side4_pins-1)*pin_distance/2,0])
        mirror([1,0,0])
            rotate([0,0,90])
                pins_array(side4_pins, pin_distance);

    // Right pins
    translate([-(case_x-(side1_pins-1)*pin_distance)/2-pin_L+case_x+2*pin_L,pin_L+case_y/2-(side2_pins-1)*pin_distance/2,0])
        mirror([0,1,0])
            mirror([0,1,0])
                rotate([0,0,90])
                    pins_array(side2_pins, pin_distance);

    // Case
    translate([-(case_x-(side1_pins-1)*pin_distance)/2,pin_L,0])
        case();
}

Here is the result:

Result Figure 3: Result

Notice that our model is completely parametrized, so, we can get almost any SMD case with bended pins (µSOP, SSOP, SO, TQFP,LQFP, etc.) If necessary, after modification of pin model and alignment we can get other cases, like PLCC or DIP. Parametrization, from my point of view, is the best advantage of OpenSCAD in comparison with other 3D modelling software.

So, here is a complete code of our model (you can play with parameters and create your own case):

Show...

A bit better and completely parametrized model, which I use in real life, can be downloaded here. It uses polyhedron instead of cube to make a case and supports faces.

It looks like the following:

Extended model Figure 4: Extended model

In the next article we will look at the different way of 3D modelling: we will use FreeCAD. And in the final article we will add some colours to our models and import our models in KiCAD.