## Digital Logic, Gates, Clocks, and Memories

Working with digital circuits is easy in CircuitLab. Scroll through the screenshots below to learn how to build and simulate digital and mixed-mode circuits.

Scroll the toolbox down to Digital Primitives:

Click and drag a NAND gate and two Digital Values (1/0) from the toolbox onto your schematic:

Hover over the NAND gate to see its computation:

Drag to highlight and select both digital values:

Press Spacebar to toggle the value of selected elements, and hover over the NAND gate again:

Hold Shift while clicking one of the inputs to deselect it:

Press Spacebar to toggle the remaining selected input, and hover again over the NAND gate to see the result:

This technique works for combinational logic, and of course you can build more complicated combinations of gates and apply the same concept.

But now, let's look at digital circuits with memory.

Click and drag a D Flip-Flop (basically a 1-bit memory) from the toolbox to the schematic:

Click and drag a Digital Clock from the toolbox to the schematic:

Click and drag wires to connect the clock to the flip-flop, and from the NAND gate's output to the flip-flop's D input:

Click and drag a wire right and up from the flip flop's Q output:

Click to select one of the digital values and press Backspace or Delete to remove it:

Click and drag as needed to continue looping that wire back to the NAND gate's now-empty input:

Press N and click to insert a node name. Place one on the NAND gate's output, one on the register's output, and one on the clock's output:

Double-click on each node name and call them A, B, and CLK as shown:

We're ready to simulate! Click Simulate at the bottom of the window, and then click Time-Domain:

Enter "10u" as Stop Time (since our clock is 1 MHz, this will show 10 clock cycles). Enter "10n" as Time Step:

Click + Add Expression and add three outputs to your list: "D(A)", "D(B) + 2", "D(CLK) - 2":

Click Run Time-Domain Simulation. A plot window appears:

Notice that after the first rising clock edge, the register copies its input A to its output B. After that, the signals don't change.

Now, click Build to return to build mode:

Click to select the digital value:

Press Spacebar to toggle it from a 0 to a 1:

Press F5 to run the simulation again. A plot window appears:

Now, something much more interesting happens! On the first rising clock edge, the register copies its input A (1) to its output B. But now this feeds back through the NAND gate and since ~(1&1)=0, now A changes to a 0. Subsequently, on the 2nd rising clock edge, the signal inverts again.

We've just built a digital clock divider, where the A or B signals now represent a clock running at half of the frequency of the original clock source.

As a bonus, it's easy to mix digital and analog circuits in CircuitLab.

Click Build to return to build mode. Press / (forward slash) to begin a toolbox search and type "LED", and then click and drag an LED to your schematic:

Press / (forward slash) again and type "330", and then click and drag a 330-ohm resistor to the schematic:

Press G and click to insert a ground node at LED's cathode:

Click and drag to wire up the resistor and LED to the register's output:

Click Simulate to open the simulation settings window. In the Outputs list, click + Add Expression and enter "I(D1.nA)" to request the current going into the LED's anode:

Click Run Time-Domain Simulation. A plot window appears:

The LED turns on and off with D(B). Note that there are spikes in the current signal because of the LED's capacitance.

That's it! Now you can simulate digital and mixed-mode circuits in CircuitLab.

Click below to open the final circuit, or try it yourself from scratch (recommended).