Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/guides/using-expressions.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: Using Expressions for Component Values
---

import CircuitPreview from "@site/src/components/CircuitPreview"

Tscircuit lets you use normal JavaScript expressions to calculate component values. This is useful when a part's value depends on other parameters, like setting a voltage divider's output or picking the capacitor for an LC resonant circuit.

## Voltage divider

Here we pick `R2` so that a divider powered from `vin` produces `vout`. Changing any of the parameters automatically recomputes `R2`.

<CircuitPreview
schematicOnly
code={`
export default () => {
const vin = 5
const vout = 3.3
const r1 = 1e3
const r2 = r1 * (vout / (vin - vout))
return (
<board>
<voltagesource
name="V1"
voltage={vin}
connections={{ pin1: "net.VCC", pin2: "net.GND" }}
/>
<resistor
name="R1"
resistance={r1}
connections={{ pin1: "net.VCC", pin2: "net.OUT" }}
/>
<resistor
name="R2"
resistance={r2}
connections={{ pin1: "net.OUT", pin2: "net.GND" }}
/>
</board>
)
}
`} />

## LC resonant circuit

The resonant frequency of an LC tank is `f = 1/(2π√(LC))`. Knowing the desired frequency and an inductance, we can compute the required capacitance.

<CircuitPreview
schematicOnly
code={`
export default () => {
const f = 1e6 // 1 MHz
const L = 10e-6 // 10 µH
const C = 1 / ((2 * Math.PI * f) ** 2 * L)
return (
<board>
<inductor
name="L1"
inductance={L}
connections={{ pin1: "net.IN", pin2: "net.OUT" }}
/>
<capacitor
name="C1"
capacitance={C}
connections={{ pin1: "net.OUT", pin2: "net.GND" }}
/>
</board>
)
}
`} />

These examples show how expressions make it easy to drive component values from formulas so designs can adapt when requirements change.