JS frontend to ngspice electronics simulation software
- use existing ngspice simulation engine
- better glue language (JS via libv8)
- better error messages
- ability to catch stupid errors
- built-in code linter and beautifier
- web-search friendly documentation (instead of one 723-page long PDF)
- overall less frustrating user experience
#!/usr/bin/env ngspicejs
// linter: ngspicejs-lint
"use strict";
// Simple common emitter amplifier 22x
battery('U1', 1, 0, 9);
sinewave('U2', 2, 0).v(0.01).f(196);
var r1 = resistor('R1', 1, 3, '68000');
var r2 = resistor('R2', 3, 0, '4700');
resistor('R3', 1, 4, '20k');
resistor('R4', 6, 0, '330');
resistor('R5', 5, 0, '50k');
capacitor('C1', 2, 3, '10u');
capacitor('C2', 4, 5, '10u');
npn('T1', 4, 3, 6, 'BC547');
// transient analysis, chart, measure gain
var t = tran().run().chart(['V(2)', 'V(5)']);
t.last_chart.gif('hello_transient.gif');
var g = t.gain(2, 5);
echo('gain', g.toFixed(1));
// change R1 and R2 and find maximal gain
var x = [], y = [], gain = [], max = g;
series_e12(10, '1M').forEach(a => {
series_e12(10, '1M').forEach(b => {
r1.r(a);
r2.r(b);
x.push(a);
y.push(b);
g = tran().run().gain(2, 5);
gain.push(g);
if (g > max) {
max = g;
echo('r1', a, 'r2', b, 'gain', g.toFixed(3));
}
});
});
// Show chart and save it as gif
chart_scatter(x, y, gain)
.title('Common emitter gain depending on R1 and R2')
.log_x(true)
.log_y(true)
.label_x('R1')
.label_y('R2')
.size(12)
.show()
.gif('hello_gain.gif');
This script will print:
gain 22.5
r1 1000 r2 82 gain 23.259
r1 1200 r2 100 gain 26.096
r1 1500 r2 120 gain 26.986
r1 1800 r2 150 gain 30.305
r1 2200 r2 180 gain 31.201
r1 2700 r2 220 gain 32.232
r1 3300 r2 270 gain 33.296
r1 3900 r2 330 gain 34.940
r1 5600 r2 470 gain 35.260
r1 390000 r2 39000 gain 35.740
sudo apt-get install ngspice ngspice-dev libngspice0-dev libnode-dev libasound2-dev libreadline-dev
(tested on Ubuntu 22.04.LTS (<=v0.3) and Ubuntu 24.04.LTS (current, >=0.4))
In ngspicejs directory run:
make clean all
Once compiled create ~/bin folder, add it to PATH environment variable (e.g. in ~/.profile, this may require to restart the terminal) and then as a normal user run:
make install
It will create symlinks in ~/bin that will points to ngspicejs, ngspicejs-linter and ngspicejs-beautify. Test it:
./hello.ngjs
If you don't see charts enable SIXEL support in your terminal.
To allow displaying sixel graphics in xterm, you need to enable sixel support first:
echo "XTerm*decTerminalID: vt340" >> $HOME/.Xresources
echo "XTerm*numColorRegisters: 256" >> $HOME/.Xresources
echo "XTerm*disallowedWindowOps: 1,2,3,4,5,6,7,8,9,11,13,19,20,21,GetSelection,SetSelection,SetWinLines,SetXprop" >> $HOME/.Xresources
xrdb $HOME/.Xresources
ngspicejs API is described in separate file. In code you can use api(anything) to print API of anything, for example:
api(resistor);
api(resistor());
api(globalThis);
api(tran);
api(tran());
You can use built-in function help(query) to perform full text searches, for example:
help('how to measure gain');
help('how to do fft of an ammeter');
- Show that saving WAV file requires transient step 40u or lower, otherwise distortion is too high
- Common emitter + Common base with feedback
- Push-pull amplifier driving 66R load
- Sziklay pair driving same 66R load
- Two npn followers as blend circuit with 50k blending pot and 50k load, measure volume sag
- Three npn followers as blend circuit with 50k blending pot and 50k load, measure volume sag
- Quad opamp blend circuit with 50k blending pot and 50k load, measure volume sag
- In emitter follower measure distortion for different input voltage and different loads
- Bootstrap amplifier 1000x gain, 2.5mA consumption, muddy AC
- Common base 50x amplifier driving 5k load
- Three stage common emitter using sub_model with parameters
- Measure battery sensitivity of common emitter amplifier
- Common emitter using darlington npn via existing subcircuit model of TIP120
- Common emitter using darlington subcircuit that itself uses other model
- Common emitter using darlington npn via custom subcircuit
- Common emitter with 24x amplification using BC547
- Optimizing volume pot linearity and range in a condenser mic preamp
- Mosfet_n current mirror
- Using current mirror as a resistor-controlled constant current source
- Compare VA characteristic, timing and AC of 2 diodes
- 4-stage diode based voltage multiplier
- Converting 22kHz 9V square wave (0-9V) to 32V using four voltage doubler subcircuits
- Another dynamic mic preamp with npn and pnp
- Using dynamic_mic device
- Making a dumb spice model of dynamic microphone
- Show that electret mic is passive and needs some power
- This file will be included by other examples
- Emitter follower chart of gain by input offset for different loads
- Changing R1 and R2 and R3 measure gain, current and thd and find the best score
- Changing R1 and R2 and measure gain, current and thd
- Simple emitter follower test
- Emitter follower with constant current source, chart THD by input amplitude for different loads
- Emitter follower chart THD by input amplitude for different loads
- Active envelope follower (AM signal)
- Active envelope follower using opamp
- Active envelope follower (beeping signal)
- Passive envelope follower (AM signal)
- Show that string throw has no stack trace
- Show that exception can be caught and program continues
- By using new Exception() the stack will be deep
- By using new Error() the stack will be deep
- Show how object Exceptions affect the speed compared to faster string exceptions
- FFT of various non-trivial vectors (real inductors, ammeters, floating voltmeters)
- FFT of an ammeter current
- 4-transistor amplifier for 8ohm load
- Weird french 2 BJT amplifier
- Calculate even and odd harmonics ratios using fft
- High-gain (5000x) 2-stage common emitter
- How pickup inductance affect AC characteristic
- Shows how to include other files
- Calculate BETA from Idss and Vto
- Testing different jfets in jfet matcher jig
- Joule thief powering white LED from single 1.5V AAA battery
- Maximize gain of common emitter with 3.3V battery, manually tuned to 22.1x, then optimized to 22.8x
- Maximize gain of common emitter, manually tuned to 217, then optimized to 224
- Maximize gain of common emitter, manually tuned to 209x, then optimized to 224x
- Maximize gain of common emitter, manually tuned to 22.0, then optimized to 22.8x
- NPN Amplifier to amplify signal to measure LC ringing
- Pulse-induction metal detector, after decay the output V(19) is in the middle of 3.3V range
- JFET colpitts oscillator
- 20kHz colpitts oscillator using n-channel mosfet BS170 and extra resistor from gate to drain
- Colpitts oscillator with NPN
- Peltz oscillator
- This is oscillator used in metal detectors by Charles D. Rakes
- Oscillator from treasure witcher metal detector
- Two NPN colpitts oscillator
- N-channel mosfet 10x amplifier with high inductance source, 50k load and reasonably flat AC
- Compare different mosfets for switching 5R load powered from 5V supply using only 3V3 control voltage
- Using motor driver L272M
- Trying to find if hFE can be measured accurately
- Can hFE be reliably measured using 2 resistors?
- Measuring hFE using opamp
- Measure hFE using two ammeters
- Measure forward early voltage VAF
- Show Ic/Vce in chart_xy with multiple series in one chart
- Measure Vce:Ic characteristic of NPN transistor using current source
- NPN phase splitter
- Testing DC coupled gain using opamp and single supply
- Testing DC coupled gain using opamp, symmetrical supply
- Differential opamp is less susceptible to common mode noise
- Show usage of voltmeter for measuring between 2 nodes
- Dual opamp voltage controlled current source, voltage = 0-2.5V, current=0-25mA
- Opamp howland current pump
- Single opamp as vccs, p-channel mosfet and grounded load
- Voltage controlled current source using sensing resistor
- 3-opamp instrumentation amplifier, gain controlled by single resistor R2
- Testing DC coupled gain using opamp
- Summing 100Hz and 700Hz sinewave using opamp
- Opamp with virtual ground made of resistor divider and caps
- Use 3.3V to switch 150R load using BJT, optimize values of R1,R2,R3 to have maximal on current and minimal off current
- Pedal alembic stratoblaster, output, ac, pot sweep (thd and gain)
- Pedal AMZ mosfet booster
- Show how resistor value affects sustain with NPN and darlington
- Pedal dallas rangemaster treble boost
- Guitar pedal "Electra distortion" with original 1N4148+1N34A vs 1N4148+1N4148
- FFT with beta-correct AC128s and comparison with two identical 2N3906
- Observing THD of fuzz face pedal with changes in pot P1 position
- Save output of fuzz face to wav file
- Pedal harmonic percolator, measuring correct CE voltages
- Pedal metal simplex, sweep pot 1 and pot 2 and show THD in 2D colored chart
- Decent octave up pedal with input jfet buffer
- Dan Armstrong Green Ringer octaver
- Octave pedal made of 4 ideal diodes
- Pedal opamp fuzz and THD related to R2 value
- Opamp based narrow band pass filter without inductor
- Jfet preamp with strange LC network across source resistor and corresponding weird AC
- Pedal peppermill and sweep of P1 and P2 pot and corresponding THD
- Pedal Anderton Wah-Antiwah - filter has two peaks, one is going down, second is going up
- Pedal bespeco weeper wah, AC and pot sweep vs peak frequency
- Pedal colorsound wah
- Pedal crybaby wah
- Pedal maestro boomerang wah wah
- Pedal VOX Wah 847
- Pedal wah filter
- Pedal wox wah swell and peak frequency by pot position
- Guitar pickup clamped by two antiparallel diodes to get some distortion
- Piezo pickup 40x jfet amplifier
- Piezo pickup 40x amplifier
- Piezo pickup with mosfet source follower as input buffer
- Piezo pickup and sziklai follower
- Measure PNP's hFE
- Convert NPN common emitter to PNP common emitter with these 2 simple tricks
- Switching between different PNP and observing gain
- How linear pot curve changes when parallel resistor is placed across pot
- Using single pulse source
- Piece-wise linear source
- AC analysis of ideal and real LC circuit
- Ripple rejection circuit using NPN as "capacitance multiplier"
- Sallen-key filter monte carlo parameter optimization
- Amplitude modulated voltage source
- Beeping voltage supply
- Single pulse supply
- Sawtooth supply
- Sinewave supply
- Square wave supply
- BJT emitter follower with low input inpedance is muddy
- BJT emitter follower with high input inpedance and 14uA consumption
- JFET source follower has much flatter AC than BJTs
- Find C0 and L0 of a singlecoil pickup from frequency responses measured using different bypass capacitors
- Response of LC filter to square wave signal (the "ringing")
- Square wave signal
- Show using switch timeline feature where switch states are defined for different times
- Switching between 2 different resistors using 1P2T switch changes the current
- Three phase astable multivibrator
- Transformer from 230V to 12V
- Using TL431 to turn on low-battery indicator
- Using TL431_2 as 2.5V reference
- Using TL431 as 2.5V reference
- 3.7V undervoltage protection using P-channel mosfet transistor and TL431 voltage reference
- 3.7V undervoltage protection using PNP transistor and TL431 voltage reference
- Using existing subckt model
- Using voltmeter to measure voltage of capacitor and inductor in RLC circuit
- Using voltmeter in transient analysis to measure average voltage
- Using voltmeter in transient analysis
- White noise generator using unconnected collector on NPN transistor
- Find model attributes for the zener diode C9V1ST from the measured reverse voltage and current values
- Find model attributes for the zener diode C9V1ST from the measured reverse voltage and current values
- Draw V-A curve using ZENER_C2V4ST model from the model/DIODE/ZENER_C2V4ST.sub
- Draw V-A curve using ZENER_C9V1ST model from the model/DIODE/ZENER_C9V1ST.sub
- How to use small singal AC analysis
- How to use AM source (amplitude modulation)
- How to use ammeter
- How to find peak frequency in AC data
- How to remove previously set device attribute
- How to use wav audio source
- How to write .m sample file
- How to use battery sensitivity analysis
- How to set battery internal resistance
- How to use beeping source
- How to use capacitor
- How to use current controlled current source
- How to use current controlled voltage source
- How to make scatter chart
- How to make XY chart from custom data
- How to use XY chart
- How to use current source
- How to change diode model
- How to change diode parameters
- How to use diode
- How to use dynamic mic
- How to use electret mic
- How to use engineering prefixes
- How to do FFT of a current running through ammeter
- How to do FFT
- How to use pnp
- How to use inductor
- How to use inductor coupling with ugly inductor names
- How to use jfet
- How to print all available models
- How to use bjt model
- How to use diode model
- How to use jfet model
- How to use mos model
- How to use resistor model
- How to use spice model
- How to use vdmos model
- How to use mosfet
- How to print ngspice netlist
- How to print ngspice commands
- How to use npn
- How to make dual opamp DIP8 subcircuit
- How to use pickup
- How to use potentiometer
- How to use pulse source
- How to use piece-wise linear source (pwl)
- How to use resistor
- How to use sawtooth voltage source
- How to make a sinusoidal signal generator?
- How to use spice directly
- How to use square voltage source
- How to make a subcircuit with equations
- How to make a subcircuit
- How to use subcircuit
- How to control 1P2T switch using timeline, how to set on and off resistance
- How to do transient analysis
- How to estimate frequency in tran data
- How to use voltage controlled current source vccs
- How to use voltage controlled voltage source vcvs with poly
- How to use voltage controlled voltage source vcvs
- How to measure average voltage using voltmeter in ngspicejs
- How to measure maximal voltage using voltmeter in ngspicejs
- How to measure minimal voltage using voltmeter
- How to measure voltage swing using voltmeter
- How to set voltmeter internal resistance
- How to use floating voltmeter that is not connected to ground
- How to use voltage reference vref
You can support development on Patreon or you can hire me via Upwork