Home /  Sketchbook /  p5.js - Logic Gates
  • Info

Overview

This is a p5.js sketch of Logic Gates.

Modeled concepts:

  • The 6-main Logic Gates
    • AND - True if all inputs are true
    • OR - True if at least one input is true.
    • NAND - True if one of the values is false.
    • NOR - True if no inputs are true.
    • XOR - True if 1 and only 1 input is true.
    • XNOR - True if the values are the same. (e.g equivalence gate)
  • Simple Input and Button
    • Fixed inputs with signals of 0 or 1.
    • Button which when pressed, toggles its original value.
  • Clock Signal
    • Modes a square wave, includes configuration for wave length and duty-cycle (% of time that the signal is sent).
  • Push Switch
    • Acts a link a spring loaded button, which returns to its default state when not pressed.
  • Switches (See Switches - Contact Terminology for details)
    • Single Pole, Single Throw
      • Single Pole - meaning there is a single input.
      • Single Throw - meaning there is a single output.
    • Single Pole, Double Throw
    • (Flipped) Single Pole, Double Throw

Controls

Limited controls in this sketch.

Keys:

  • 1 - 6 in Examples #1 and #4 cycle the ‘main gate’ through the 6-basic gates
  • p will save the current canvas as a PNG.

Mouse:

  • CLICK on the yellow-highlighted components toggles their states.

References / Inspiration:

  • Wikipedia - Logic Gate
  • Texas Instruments - IEEE Standard 91-1984
  • Spemco - Electronics 101
  • allaboutcircuits.com
  • electronics-tutorials.ws
  • logic.ly (Interactive circuit design)
  • academo.org - Logic Gate Simulator

Links:

  • Live View
  • Source on Github

Screenshot:

screenshot

Screen Captures

Example #1 - Demo of basic gates, Pressing 1-6 will cycle through the logic gate types.

screencap

Example #2 - Demo of Push Switches

screencap

Example #3 - Demo of Pole-Throw Switches

screencap

Example #4 - Demo of Clock Signals; includes 4 clocks, from top down:

  1. Has a wave-length of 180 frames (~3 seconds), and a duty-cyle of 50%.
  2. Has a wave-length of 90 frames (~1.5 seconds), and a duty-cyle of 50%. (so twice as fast as 1st clock).
  3. Has a wave-length of 100 frames (~1.5 seconds), and a duty-cyle of 20%.
  4. Has a wave-length of 100 frames (~1.5 seconds), and a duty-cyle of 80%, and offset of 80 frames (meaning it turns on at the 20th frame, just when the 3rd clock turns off).

screencap