I am looking to simulate circuits on Falstad: http://www.falstad.com/circuit/circuitjs.html . One of the features on the website is the ability to add an external voltage source programmable with JavaScript (Can be found under the drop down toolbar > Inputs and Sources > Add External Voltage Source (Javascript)). How can I do this? Is there some documentation available?


1 Answer 1


The falstad website has an "example" here, although it doesn't explain much outright: https://www.falstad.com/circuit/jsinterface.html

The circuit simulator runs in an <iframe>. Elsewhere on the page, there are two input fields:

  • extsin frequency:
    <input id="freq" value="10">
  • extsin amplitude:
    <input id="ampl" value="10">

Note their id values of freq and ampl.

extsin is the name of the external voltage source within the simulation. The name can be changed by editing it:
falstad javasript circuit simulator external voltage source edit name

Further down in the page source is the javascript block. Below is the whole code block for reference.
(Unfortunately I don't think StackExchange has a way to format line numbers in code blocks)


// get iframe the simulator is running in.  Must have same origin as this file!
var iframe = document.getElementById("circuitFrame");

var sim;
var freq, ampl;

function round(x) {
  return Math.round(x*1000)/1000;

// called when simulator updates its display
function didUpdate(sim) {
  var info = document.getElementById("info");
  info.innerHTML = "time = " + round(sim.getTime()) + "<br>running = " + sim.isRunning();

  // get voltage of labeled node "vsense"
  var vsense = sim.getNodeVoltage("vsense");
  info.innerHTML += "<br>V(vsense) = " + round(vsense);

  freq = parseFloat(document.getElementById("freq").value);
  ampl = parseFloat(document.getElementById("ampl").value);

  var bstr = "";
  var bval = 0;
  var i;
  for (i = 7; i >= 0; i--) {
    var v = sim.getNodeVoltage("D" + i);
    if (v > 2.5) {
      bstr += "1";
      bval = 2*bval+1;
    } else {
      bstr += "0";
      bval = 2*bval;
  info.innerHTML += "<br>counter value = <tt>" + bstr + "</tt> = " + bval;

// called every timestep
function didStep(sim) {
  var t = sim.getTime();
  var q = ampl*Math.sin(freq*Math.PI*2*t);

  // set voltage of external voltage "extsin"
  sim.setExtVoltage("extsin", ampl*Math.sin(freq*Math.PI*2*t));

// callback called when simulation is done initializing
function simLoaded() {
  // get simulator object
  sim = iframe.contentWindow.CircuitJS1;

  // set up callbacks on update and timestep
  sim.onupdate = didUpdate;
  sim.ontimestep = didStep;

// set up callback
iframe.contentWindow.oncircuitjsloaded = simLoaded;


Here are the key parts for setting the external voltage:

The values from the frequency and amplitude inputs on the page are read here and stored:

  freq = parseFloat(document.getElementById("freq").value);
  ampl = parseFloat(document.getElementById("ampl").value);

Further down, the voltage of external source of name extsin is set using those freq and ampl values:

// set voltage of external voltage "extsin"
  sim.setExtVoltage("extsin", ampl*Math.sin(freq*Math.PI*2*t));
  • \$\begingroup\$ The real key is sim.setExtVoltage(name, value), either on-change if it's not dependent on the sim time or in an ontimestep callback if it is. The rest is a lot of boilerplate for this specific setup. \$\endgroup\$
    – hobbs
    Oct 13, 2021 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.