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 libnode-dev libasound2-dev libreadline-dev
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 in /example/audio
- Common emitter + Common base with feedback in /example/bjt_high_current_output_amps
- Push-pull amplifier driving 66R load in /example/bjt_high_current_output_amps
- Sziklay pair driving same 66R load in /example/bjt_high_current_output_amps
- Two npn followers as blend circuit with 50k blending pot and 50k load, measure volume sag in /example/blend_circuit
- Three npn followers as blend circuit with 50k blending pot and 50k load, measure volume sag in /example/blend_circuit
- Quad opamp blend circuit with 50k blending pot and 50k load, measure volume sag in /example/blend_circuit
- In emitter follower measure distortion for different input voltage and different loads in /example/blend_circuit
- Bootstrap amplifier 1000x gain, 2.5mA consumption, muddy AC in /example/bootstrap_amplifier
- Common base 50x amplifier driving 5k load in /example/common_base
- Three stage common emitter using sub_model with parameters in /example/common_emitter
- Measure battery sensitivity of common emitter amplifier in /example/common_emitter
- Common emitter using darlington npn via existing subcircuit model of TIP120 in /example/common_emitter
- Common emitter using darlington subcircuit that itself uses other model in /example/common_emitter
- Common emitter using darlington npn via custom subcircuit in /example/common_emitter
- Common emitter with 24x amplification using BC547 in /example/common_emitter
- Optimizing volume pot linearity and range in a condenser mic preamp in /example/condenser_mic_optimizing_volume_pot
- Mosfet_n current mirror in /example/current_mirror
- Using current mirror as a resistor-controlled constant current source in /example/current_mirror
- Compare VA characteristic, timing and AC of 2 diodes in /example/diode_va
- 4-stage diode based voltage multiplier in /example/diode_voltage_multiplier
- Converting 22kHz 9V square wave (0-9V) to 32V using four voltage doubler subcircuits in /example/diode_voltage_multiplier
- Another dynamic mic preamp with npn and pnp in /example/dynamic_mic
- Using dynamic_mic device in /example/dynamic_mic
- Making a dumb spice model of dynamic microphone in /example/dynamic_mic
- Show that electret mic is passive and needs some power in /example/electret_mic
- This file will be included by other examples in /example/emitter_follower
- Emitter follower chart of gain by input offset for different loads in /example/emitter_follower
- Changing R1 and R2 and R3 measure gain, current and thd and find the best score in /example/emitter_follower
- Changing R1 and R2 and measure gain, current and thd in /example/emitter_follower
- Simple emitter follower test in /example/emitter_follower
- Emitter follower with constant current source, chart THD by input amplitude for different loads in /example/emitter_follower
- Emitter follower chart THD by input amplitude for different loads in /example/emitter_follower
- Active envelope follower (AM signal) in /example/envelope_follower
- Active envelope follower using opamp in /example/envelope_follower
- Active envelope follower (beeping signal) in /example/envelope_follower
- Passive envelope follower (AM signal) in /example/envelope_follower
- Show that string throw has no stack trace in /example/exceptions
- Show that exception can be caught and program continues in /example/exceptions
- By using new Exception() the stack will be deep in /example/exceptions
- By using new Error() the stack will be deep in /example/exceptions
- Show how object Exceptions affect the speed compared to faster string exceptions in /example/exceptions
- FFT of various non-trivial vectors (real inductors, ammeters, floating voltmeters) in /example/fft_of_ammeter_current
- FFT of an ammeter current in /example/fft_of_ammeter_current
- 4-transistor amplifier for 8ohm load in /example/four_bjt_8ohm_amplifier
- Weird french 2 BJT amplifier in /example/french_amp
- Calculate even and odd harmonics ratios using fft in /example/harmonics
- High-gain (5000x) 2-stage common emitter in /example/high_gain_ce_ce_amplifier
- How pickup inductance affect AC characteristic in /example/high_gain_ce_ce_amplifier
- Shows how to include other files in /example/include
- Calculate BETA from Idss and Vto in /example/jfet_matcher
- Testing different jfets in jfet matcher jig in /example/jfet_matcher
- Joule thief powering white LED from single 1.5V AAA battery in /example/joule_thief
- Maximize gain of common emitter with 3.3V battery, manually tuned to 22.1x, then optimized to 22.8x in /example/maximize_gain
- Maximize gain of common emitter, manually tuned to 217, then optimized to 224 in /example/maximize_gain
- Maximize gain of common emitter, manually tuned to 209x, then optimized to 224x in /example/maximize_gain
- Maximize gain of common emitter, manually tuned to 22.0, then optimized to 22.8x in /example/maximize_gain
- NPN Amplifier to amplify signal to measure LC ringing in /example/measure_lc_ringing
- Pulse-induction metal detector, after decay the output V(19) is in the middle of 3.3V range in /example/metal_detector_pi
- JFET colpitts oscillator in /example/metal_detector_transmitter
- 20kHz colpitts oscillator using n-channel mosfet BS170 and extra resistor from gate to drain in /example/metal_detector_transmitter
- Colpitts oscillator with NPN in /example/metal_detector_transmitter
- Peltz oscillator in /example/metal_detector_transmitter
- This is oscillator used in metal detectors by Charles D. Rakes in /example/metal_detector_transmitter
- Oscillator from treasure witcher metal detector in /example/metal_detector_transmitter
- Two NPN colpitts oscillator in /example/metal_detector_transmitter
- N-channel mosfet 10x amplifier with high inductance source, 50k load and reasonably flat AC in /example/mosfet_n_10x_amplifier
- Compare different mosfets for switching 5R load powered from 5V supply using only 3V3 control voltage in /example/mosfet_switching_with_3V3
- Using motor driver L272M in /example/motor_driver_L272M
- Trying to find if hFE can be measured accurately in /example/npn_characteristics
- Can hFE be reliably measured using 2 resistors? in /example/npn_characteristics
- Measuring hFE using opamp in /example/npn_characteristics
- Measure hFE using two ammeters in /example/npn_characteristics
- Measure forward early voltage VAF in /example/npn_characteristics
- Show Ic/Vce in chart_xy with multiple series in one chart in /example/npn_characteristics
- Measure Vce:Ic characteristic of NPN transistor using current source in /example/npn_characteristics
- NPN phase splitter in /example/npn_phase_splitter
- Testing DC coupled gain using opamp and single supply in /example/opamp_dc_gain
- Testing DC coupled gain using opamp, symmetrical supply in /example/opamp_dc_gain
- Differential opamp is less susceptible to common mode noise in /example/opamp_diff_amp_common_mode_noise
- Show usage of voltmeter for measuring between 2 nodes in /example/opamp_differential_amplifier_floating_input
- Dual opamp voltage controlled current source, voltage = 0-2.5V, current=0-25mA in /example/opamp_howland_current_pump
- Opamp howland current pump in /example/opamp_howland_current_pump
- Single opamp as vccs, p-channel mosfet and grounded load in /example/opamp_howland_current_pump
- Voltage controlled current source using sensing resistor in /example/opamp_howland_current_pump
- 3-opamp instrumentation amplifier, gain controlled by single resistor R2 in /example/opamp_instrumentation_amplifier
- Testing DC coupled gain using opamp in /example/opamp_piezo_buffer
- Summing 100Hz and 700Hz sinewave using opamp in /example/opamp_summing_amplifier
- Opamp with virtual ground made of resistor divider and caps in /example/opamp_virtual_ground
- Use 3.3V to switch 150R load using BJT, optimize values of R1,R2,R3 to have maximal on current and minimal off current in /example/optimize_bjt_switch
- Pedal alembic stratoblaster, output, ac, pot sweep (thd and gain) in /example/pedal_alembic_stratoblaster
- Pedal AMZ mosfet booster in /example/pedal_amz_mosfet_booster
- Show how resistor value affects sustain with NPN and darlington in /example/pedal_bazz_fuss_sustain
- Pedal dallas rangemaster treble boost in /example/pedal_dallas_rangemaster_treble_boost
- Guitar pedal "Electra distortion" with original 1N4148+1N34A vs 1N4148+1N4148 in /example/pedal_electra_distortion
- FFT with beta-correct AC128s and comparison with two identical 2N3906 in /example/pedal_fuzz_face
- Observing THD of fuzz face pedal with changes in pot P1 position in /example/pedal_fuzz_face
- Save output of fuzz face to wav file in /example/pedal_fuzz_face
- Pedal harmonic percolator, measuring correct CE voltages in /example/pedal_harmonic_percolator
- Pedal metal simplex, sweep pot 1 and pot 2 and show THD in 2D colored chart in /example/pedal_metal_simplex
- Decent octave up pedal with input jfet buffer in /example/pedal_octaver
- Dan Armstrong Green Ringer octaver in /example/pedal_octaver
- Octave pedal made of 4 ideal diodes in /example/pedal_octaver
- Pedal opamp fuzz and THD related to R2 value in /example/pedal_opamp_fuzz
- Opamp based narrow band pass filter without inductor in /example/pedal_opamp_inductorless_wah
- Jfet preamp with strange LC network across source resistor and corresponding weird AC in /example/pedal_ovation_preamp
- Pedal peppermill and sweep of P1 and P2 pot and corresponding THD in /example/pedal_peppermill
- Pedal Anderton Wah-Antiwah - filter has two peaks, one is going down, second is going up in /example/pedal_wah
- Pedal bespeco weeper wah, AC and pot sweep vs peak frequency in /example/pedal_wah
- Pedal colorsound wah in /example/pedal_wah
- Pedal crybaby wah in /example/pedal_wah
- Pedal maestro boomerang wah wah in /example/pedal_wah
- Pedal VOX Wah 847 in /example/pedal_wah
- Pedal wah filter in /example/pedal_wah
- Pedal wox wah swell and peak frequency by pot position in /example/pedal_wah
- Guitar pickup clamped by two antiparallel diodes to get some distortion in /example/pickup_and_two_antiparallel_diodes
- Piezo pickup 40x jfet amplifier in /example/piezo_pickup_amplifier
- Piezo pickup 40x amplifier in /example/piezo_pickup_amplifier
- Piezo pickup with mosfet source follower as input buffer in /example/piezo_pickup_amplifier
- Piezo pickup and sziklai follower in /example/piezo_pickup_amplifier
- Measure PNP's hFE in /example/pnp_characteristics
- Convert NPN common emitter to PNP common emitter with these 2 simple tricks in /example/pnp_common_emitter
- Switching between different PNP and observing gain in /example/pnp_common_emitter
- How linear pot curve changes when parallel resistor is placed across pot in /example/pot_curves
- Using single pulse source in /example/pulse_source
- Piece-wise linear source in /example/pwl_source
- AC analysis of ideal and real LC circuit in /example/quality_of_lc_filter
- Ripple rejection circuit using NPN as "capacitance multiplier" in /example/ripple_rejection_using_npn
- Sallen-key filter monte carlo parameter optimization in /example/sallen_key_monte_carlo
- Amplitude modulated voltage source in /example/signal_sources
- Beeping voltage supply in /example/signal_sources
- Single pulse supply in /example/signal_sources
- Sawtooth supply in /example/signal_sources
- Sinewave supply in /example/signal_sources
- Square wave supply in /example/signal_sources
- BJT emitter follower with low input inpedance is muddy in /example/singlecoil_pickup_ac
- BJT emitter follower with high input inpedance and 14uA consumption in /example/singlecoil_pickup_ac
- JFET source follower has much flatter AC than BJTs in /example/singlecoil_pickup_ac
- Find C0 and L0 of a singlecoil pickup from frequency responses measured using different bypass capacitors in /example/singlecoil_pickup_ac
- Response of LC filter to square wave signal (the "ringing") in /example/square_wave_and_lc_filter
- Square wave signal in /example/square_wave_signal
- Show using switch timeline feature where switch states are defined for different times in /example/switch
- Switching between 2 different resistors using 1P2T switch changes the current in /example/switch
- Three phase astable multivibrator in /example/three_phase_oscillator
- Transformer from 230V to 12V in /example/transformer
- Using TL431 to turn on low-battery indicator in /example/undervoltage_protection
- Using TL431_2 as 2.5V reference in /example/undervoltage_protection
- Using TL431 as 2.5V reference in /example/undervoltage_protection
- 3.7V undervoltage protection using P-channel mosfet transistor and TL431 voltage reference in /example/undervoltage_protection
- 3.7V undervoltage protection using PNP transistor and TL431 voltage reference in /example/undervoltage_protection
- Using existing subckt model in /example/using_subckt_model
- Using voltmeter to measure voltage of capacitor and inductor in RLC circuit in /example/voltmeter
- Using voltmeter in transient analysis to measure average voltage in /example/voltmeter
- Using voltmeter in transient analysis in /example/voltmeter
- White noise generator using unconnected collector on NPN transistor in /example/white_noise_generator_npn
- Find model attributes for the zener diode C9V1ST from the measured reverse voltage and current values in /example/zener_diode_model_maker
- Find model attributes for the zener diode C9V1ST from the measured reverse voltage and current values in /example/zener_diode_model_maker
- Draw V-A curve using ZENER_C2V4ST model from the model/DIODE/ZENER_C2V4ST.sub in /example/zener_diode_model_maker
- Draw V-A curve using ZENER_C9V1ST model from the model/DIODE/ZENER_C9V1ST.sub in /example/zener_diode_model_maker
- How to use small singal AC analysis in /howto/ac
- How to use AM source (amplitude modulation) in /howto/am
- How to use ammeter in /howto/ammeter
- How to find peak frequency in AC data in /howto/arrays
- How to remove previously set device attribute in /howto/attributes
- How to use wav audio source in /howto/audio
- How to write .m sample file in /howto/audio
- How to use battery sensitivity analysis in /howto/battery_sensitivity
- How to set battery internal resistance in /howto/battery
- How to use beeping source in /howto/beeps
- How to use capacitor in /howto/capacitor
- How to use current controlled current source in /howto/cccs
- How to use current controlled voltage source in /howto/ccvs
- How to make scatter chart in /howto/chart_scatter
- How to make XY chart from custom data in /howto/chart_xy
- How to use XY chart in /howto/chart_xy
- How to use current source in /howto/current_source
- How to change diode model in /howto/diode
- How to change diode parameters in /howto/diode
- How to use diode in /howto/diode
- How to use dynamic mic in /howto/dynamic_mic
- How to use electret mic in /howto/electret_mic
- How to use engineering prefixes in /howto/eng
- How to do FFT of a current running through ammeter in /howto/fft
- How to do FFT in /howto/fft
- How to use pnp in /howto/how_to_use_pnp
- How to use inductor in /howto/inductor
- How to use inductor coupling with ugly inductor names in /howto/inductor
- How to use jfet in /howto/jfet_n
- How to print all available models in /howto/model
- How to use bjt model in /howto/model
- How to use diode model in /howto/model
- How to use jfet model in /howto/model
- How to use mos model in /howto/model
- How to use resistor model in /howto/model
- How to use spice model in /howto/model
- How to use vdmos model in /howto/model
- How to use mosfet in /howto/mosfet_n
- How to print ngspice netlist in /howto/netlist
- How to print ngspice commands in /howto/netlist
- How to use npn in /howto/npn
- How to make dual opamp DIP8 subcircuit in /howto/opamp
- How to use pickup in /howto/pickup
- How to use potentiometer in /howto/pot
- How to use pulse source in /howto/pulse
- How to use piece-wise linear source (pwl) in /howto/pwl
- How to use resistor in /howto/resistor
- How to use sawtooth voltage source in /howto/sawtooth
- How to make a sinusoidal signal generator? in /howto/sinewave
- How to use spice directly in /howto/spice
- How to use square voltage source in /howto/square
- How to make a subcircuit with equations in /howto/sub
- How to make a subcircuit in /howto/sub
- How to use subcircuit in /howto/sub
- How to control 1P2T switch using timeline, how to set on and off resistance in /howto/switch_1p2t
- How to do transient analysis in /howto/tran
- How to estimate frequency in tran data in /howto/tran
- How to use voltage controlled current source vccs in /howto/vccs
- How to use voltage controlled voltage source vcvs with poly in /howto/vcvs
- How to use voltage controlled voltage source vcvs in /howto/vcvs
- How to measure average voltage using voltmeter in ngspicejs in /howto/voltmeter
- How to measure maximal voltage using voltmeter in ngspicejs in /howto/voltmeter
- How to measure minimal voltage using voltmeter in /howto/voltmeter
- How to measure voltage swing using voltmeter in /howto/voltmeter
- How to set voltmeter internal resistance in /howto/voltmeter
- How to use floating voltmeter that is not connected to ground in /howto/voltmeter
- How to use voltage reference vref in /howto/vref
You can support development on Patreon or you can hire me via Upwork