Logic Gate Simulator on Kitronik ARCADE for Micro:bit in MakeCode Arcade

by kevinjwalters in Circuits > Microcontrollers

179 Views, 0 Favorites, 0 Comments

Logic Gate Simulator on Kitronik ARCADE for Micro:bit in MakeCode Arcade

edupico-kitronikarcadeformicrobit-65.jpg

This article shows a logic gate simulator written in MakeCode Arcade. MakeCode Arcade is commonly used to write small games but the controller buttons and graphical features are also useful for other types of software. This program allows a simple circuit to be drawn and then the "SIM" action runs a simple logic simulation creating a truth table. Additionally, the program cycles through the truth table rows on the three output pins at a rate of 1 row per second.

The logic gate simulator is shown on the new Kitronik ARCADE for micro:bit - this uses the microcontroller on a BBC micro:bit (V2 only). The five large pads from the micro:bit are conveniently replicated on the device making it easy to use the three GPIO pads. Kitronik also have a different SAMD51-based ARCADE device which could be used but the GPIO on that device requires soldering and the through-holes are covered by the normal acrylic top.

The EDU PICO running the logic gate analyser from Instructables: Logic Gate Analyser With Cytron EDU PICO or Adafruit CLUE Demonstrated With MH-EDU-Electronics LogicBoard and Kitronik Inventor's Kit has been enlisted to show the GPIO outputs from the logic gate simulator.

Supplies

Introduction to the Kitronik ARCADE for Micro:bit

kitronik-arcade-for-microbit-front-1.jpg
kitronik-arcade-for-microbit-back-1.jpg

The Nordic nRF51822 microcontroller on the BBC micro:bit V1 has 16k of memory which is too small for MakeCode Arcade. The micro:bit V2 uses the Nordic nRF52833 with 128k enabling MakeCode Arcade to run. There are currently four boards from various manufacturers which use the micro:bit V2, listed below.

This article uses the Kitronik one. It features:

  • A 1.77" colour screen using the MakeCode Arcade standard resolution of 160x120 pixels.
  • Powered by USB connector on micro:bit V2 or 3 AA batteries (alkaline or rechargeable NiMH).
  • The five micro:bit pads, 3 GPIO, 3V and GND are replicated at the bottom.
  • It uses the small speaker on the back of micro:bit V2.

Installing the Logic Gate Analyser on the Kitronik ARCADE for Micro:bit

browser-makecodearcade-selectingdevice-1-rc.png
  1. Connect the MakeCode Arcade device over USB.
  2. Click on lgs-v1.7 to open the software in a web browser. The software is also available on GitHub as code embedded in png, JavaScript (TypeScript) and a hex file.
  3. Click on Edit Code button at the top.
  4. Click on Download button at bottom left. This will require a hardware device to be selected the first time you use it. If you are using the suggested device, scroll down to the Kitronik ARCADE for micro:bit - this is highlighted with a red circle in the screenshot above. This is likely to be near the bottom and you will have to scroll past the other Kitronik ARCADE device.
  5. The code takes a few seconds to compile and then a file selector will appear. Select the MICROBIT drive. This will save the hex file onto the device installing the program.

This program uses the arcade-sprite-data and edge-connector extensions. It was developed and tested with:

  • Arcade version: 1.12.53
  • MakeCode version: 8.5.63

The v1.7 has the bug fix for incorrect NOT gate image in v1.6.

Using the Logic Gate Simulator

Logic Gate Simulator in MakeCode Arcade on new Kitronik ARCADE for micro:bit with Cytron EDU PICO

The video above shows the logic gate simulator being used.

  • 00:03 The Kitronik ARCADE for micro:bit is turned on, powered by batteries underneath.
  • 00:09 The USB power to the EDU PICO is turned on (off screen).
  • 00:15 A simple circuit is created with an AND gate connected to a NOT gate (the v1.6 image is incorrect and shows a buffer rather than an inverter) and two inputs and one output.
  • 00:40 The circuit is automatically connected with wires - the are coloured cyan but the colour is difficult to see in the video.
  • 00:45 The circuit is simulated by a click on SIM from the menu - a truth table appears on the display but the text is unfortunately not visible in the video.
  • 00:49 The logic gate analyser running on the Cytron EDU PICO completes the truth table by reading the values from the pads and identifies the whole circuit as a NAND gate.

The connectivity from the micro:bit V2 pads to the EDU PICO is:

  • P0 to GP26 (white).
  • P1 to GP27 (yellow).
  • P2 to GP6 (green).
  • GND to GND (black).

A strip of header is being used in the video to convert the female jumper wires to male to allow the attachment of crocodile (alligator) clip leads. The long strip allows good spacing to prevent inadvertent shorts. Is is important to connect the ground as there will be no common (shared) ground between the two microcontrollers without this.

A Closer Look at the Logic Gate Simulator

lgs-16-sim-startscreen.png
logicgateanalyser16-1800x1500-nand-connectedgates.jpg
logicgateanalyser16-1800x1500-nand-menu.jpg
logicgateanalyser16-1800x1500-nand-truthtable.jpg

The simulation screenshot and three photos above show the display more clearly.

  1. The start screen which briefly explains how to use the simulator.
  2. A two gate circuit drawn in the simulator with connected gates.
  3. The menu showing the SIM option about to be selected to run the simulation. Note: the v1.6 code has the incorrect image for the NOT gate.
  4. The truth table based on the simulators output for all input combinations.

The Code

lgs-16-editor-onapressed.png
lgs-16-editor-onblrpressed.png
lgs-16-editor-runsimulationfn.png
lgs-16-editor-forever-mainloop.png

The program is written in block-based MakeCode. This program is just at the boundary of size and complexity where a transition to JavaScript (TypeScript) may make sense.

The screenshots above showing the on button A pressed and on button B pressed present a good overview. The B button toggles between the menu and the circuit display using switchToMenu and switchToGates, respectively. The A button varies according to the mode calling various functions described below.

  • placeGate - the gate selected from the menu is placed in the circuit fixing its position.
  • makeConnections - connections from outputs to inputs are made automatically by choosing the nearest pairs.
  • runSimulation - the simulation runs with the following steps.
  • Setting each combination of input voltages and then
  • repeatedly propagating voltages and setting gates if there's a complete set of inputs until no more gates change;
  • storing the output value which could be low (0), high (1) or indeterminate (?).
  • Displaying the truth table and outputting the values to the pins.

It's fairly common for MakeCode programs to have the majority of the code in or called from a main forever loop. In this program most of the code is called from the button events. The forever loop only implements the cycling digital outputs to the pins while the truth table is shown on the display with show long text.

Going Further

56116_additional-media-kitronik-arcade-bbc-micro-bit-makecode-animation_600x.gif

Here's some potential enhancements for the logic gate simulator:

  • Add more logic gates.
  • Add support for other hardware devices with different pins.
  • Improve the wire drawing to use the more traditional mix of horizontal and vertical line segments rather than direct, angled lines.
  • Improve the connectivity options. The easiest way to do add fan-out with the current implementation would be to introduce a wire (or buffer gate) with one input and multiple outputs.
  • Add a second simulation mode where the first two pads are used for inputs allowing the simulator to be used as a drop-in replacement for a gate. This needs to be very distinct from the current all outputs mode to avoid confusion.

Some (non-industrial) browser-based logic gate simulators:

Further reading: