Make a Voltage Divider in typeCAD
In this example, we will make a voltage divider.
This is the equivalent circuit:
Very simple, just two resistors. Voltage on the top, ground on the bottom, with half the voltage in the middle.
Make a typeCAD project
From a terminal, run:
npx @typecad/create-typecad
🤖 typeCAD Project Creator
✔ Create a project: Project
✔ Project name? voltage_divider
✔ Create a PlatformIO project for firmware? No
✔ Initialize a git repo? No
? Optional packages
+ Project folders created
+ KiCAD project created
+ Creating npm package
+ Installing dependencies
+ Finished
+ Open voltage_divider.code-workspace in VS Code to get started
Call this project voltage_divider
. No need to make a PlatformIO project or initialize a git repo unless you want to. No extra packages are needed either, but feel free to add them later if you want.
Like the output said, open voltage_divider.code-workspace
in VSCode to get started. You should see NPM Scripts
in the sidebar with a few scripts. If you don’t, click View > Open View >
and enter NPM Scripts
.
Test build
Click the 🤖 Build
script and you should see some build output in a terminal. If everything is installed correctly, you should see this:
🏁 typeCAD starting...
R1 created
R1 added
./build/voltage_divider.net file written
🏁 typeCAD finished
The R1
resistor is there as test code. It’s contained in ./src/voltage-divider/index.ts
.
Making resistors
You can delete most of the starter code so you’re left with this:
import { Schematic } from "@typecad/typecad"
import { Resistor } from '@typecad/passives/0603';
let typecad = new Schematic('voltage_divider');
let r1 = new Resistor({ value: '1kohm' });
typecad.create(r1);
This code creates a Schematic
object called typecad
and a Resistor
object called r1
. We need two resistors, so let’s make another.
import { Schematic } from "@typecad/typecad"
import { Resistor } from '@typecad/passives/0603';
let typecad = new Schematic('voltage_divider');
let r1 = new Resistor({ value: '1kohm' });
let r2 = new Resistor({ value: '1kohm' });
typecad.create(r1, r2);
Don’t forget to add r2
to the create
method.
Resistor properties
Components have several properties, the one we’re concerned with here is value
(VSCode tooltips will tell you all the other properties). Our example schematic used 10kOhm resistors, so let’s change that.
import { Schematic } from "@typecad/typecad"
import { Resistor } from '@typecad/passives/0603';
let typecad = new Schematic('voltage_divider');
let r1 = new Resistor({ value: '10kohm' });
let r2 = new Resistor({ value: '10kohm' });
typecad.create(r1, r2);
Build
Build the schematic again. Click the ✳️ Open Board
script. When KiCAD opens, click the Net
icon and locate the voltage_divider.net
file. Then click Update PCB
.
You should see two resistors on the board.
Connections
For passive components, it is typical to just use a r1.pin(x)
method to get a Pin
object. Resistors have two pins and no polarity, so we can use either pin.
To create our voltage divider, we need to connect our two resistors together.
import { Schematic } from "@typecad/typecad"
import { Resistor } from '@typecad/passives/0603';
let typecad = new Schematic('voltage_divider');
let r1 = new Resistor({ value: '10kohm' });
let r2 = new Resistor({ value: '10kohm' });
typecad.net(r1.pin(2), r2.pin(1));
typecad.create(r1, r2);
If we want, we can give this connection a name so it is clearer in KiCAD.
typecad.net(r1.pin(2), r2.pin(1));
typecad.named('VDIV').net(r1.pin(2), r2.pin(1));
Build again
When you build and import the netlist again, you’ll see the connection we just made.
Extras
We need power to make this circuit do something, but that will be covered in a later example.