Exploring ternary logic: building ternary inverters using complementary MOSFETs

R. X. Seger
15 min readNov 20, 2016

--

Since the early days of digital computers, binary (base 2) has ruled supreme. Yet other numeral systems for computing are possible, including ternary (base 3), used in Setun, or decimal (base 10), as used by Babbage.

In this article, we’ll build one of the most basic ternary logic gates possible: an inverter.

The binary inverter takes a high or low input voltage, and outputs low or high voltage, respectively. Ternary inverters are similar, but output positive/negative for negative/positive, and zero for zero (within some threshold):

Ternary logic hasn’t caught on for many practical usages, but since binary logic and Boolean algebra is fundamental to modern computing, it can be a fun pastime to consider replacing it with ternary. Anyways, let’s get started.

Designing a Simple Ternary Inverter

For comparision, here is a a standard CMOS inverter for binary signals:

This circuit can be enhanced to support three states by adding resistors as shown in Ternary logic circuits with CMOS integrated circuits (US4107549A) by Hussein T. Mouftah (1977):

Other techniques are detailed in CMOS ternary logic circuits, X.W. Wu, IEE Proceedings, Vol. 137, Pt. G, No. I, February 1990 — including varying the length/width ratio of the transistors, which could be useful for custom silicon but are not the most practical for circuit construction from discrete components. For now, we’ll be using using two resistors.

MOSFET Part Selection

For this complementary MOS circuit, we need both N-channel and P-channel MOSFET transistors.

There is a large selection of such transistors available, but to meet the goal of designing something easy to build, I consulted Octopart: Common Parts Library. This is a library of well-known reasonably-priced parts easy to source useful for new designs. Nothing too obscure or unobtainable, even if it is interesting. Logic level N-channel FETs include: BSS138, 2N7002,215, CJ2303, and logic level P-channel FETs: BSS84, BSS84LT1G, and CJ2305.

The BSS138 and BSS84 from Fairchild Semiconductor are suitable, a good initial choice but may change later. Let’s see how they perform in simulation.

Simulating with LTspice

There are a variety of circuit simulation tools available, but LTspice IV from Linear Technologies is a good choice. It is cross-platform, relatively easy to use, and there is even an unofficial ltwiki.org community.

I watched the tutorials to learn how to use LTspice, then cooked up this simple circuit:

The BSS84 PMOS transistor model is built-in, but BSS138 is more problematic, so I’m documenting here how I solved this missing transistor since it was non-obvious. There are a small number of part models distributed with LTspice, including those from Linear Technologies, but it is nowhere near complete. LTwiki Components Library and Circuits offers “A Large LTspice Folder from Bordodynov”, LargeCollection.zip, but it is not complete either!

To actually find the BSS138 model, browsed in http://ltwiki.org/?title=Vendor_List to Fairchild/BS/BSS138.lib. Here it is:

*BSS138 ELECTRICAL MODEL (SOT-23 Single N-Ch DMOS)
*-----------------------
.SUBCKT BSS138 20 10 30
Rg 10 1 1
M1 2 1 3 3 DMOS L=1u W=1u
.MODEL DMOS NMOS (VTO={1.3*{-0.002*TEMP+1.05}} KP={-0.0014*TEMP+0.685}
+ THETA=0.086 VMAX=2.2E5 LEVEL=3)
Cgs 1 3 40p
Rd 20 4 0.2 TC=0.0065
Dds 3 4 DDS
.MODEL DDS D(BV={50*{0.00084*TEMP+0.979}} M=0.36 CJO=23p VJ=0.8)
Dbody 3 20 DBODY
.MODEL DBODY D(IS=1.4E-13 N=1 RS=40m TT=100n)
Ra 4 2 0.3 TC=0.0065
Rs 3 5 10m
Ls 5 30 .5n
M2 1 8 6 6 INTER
E2 8 6 4 1 2
.MODEL INTER NMOS(VTO=0 KP=10 LEVEL=1)
Cgdmax 7 4 68p
Rcgd 7 4 10meg
Dgd 6 4 DGD
Rdgd 4 6 10meg
.MODEL DGD D(M=0.3 CJO=68p VJ=0.4)
M3 7 9 1 1 INTER
E3 9 1 4 1 -2
.ENDS BSS138

Using this model is another matter. It is a sub-circuit model, built from other fundamental components, not a transistor model in itself like the BSS84 (or at least how it is modeled in LTspice). To place an BSS183:

  1. right-click > Draft > Component > choose “nmos” > OK
  1. Click to place the nmos
  2. Shift-right-click on the component, bringing up this sheet:

It is crucial to shift-click. If you right-click without shift, you get this instead:

The “Pick a new transistor” button lets you choose 2N7002, BSS123, BSS145, and many others but not our custom BSS138 subcircuit. So go back to shift-right-click, then (this is documented in LTspice Help > Frequently Asked Questions > MOSFET Models > Can I add my own MOSFET models?:

) — change prefix from MN to “X” (for subcircuit), and “value” to the subcircuit name, BSS138 in our case:

Then right-click > Draft > SPICE Directive, and copy-and-paste the full .SUBCKT BSS138 text. It will appear on your schematic as text, which is fine. The NMOS transistor should now correctly be modeled using BSS138. Note: if you get model not found errors, try deleting the component and re-adding it, or deleting the SPICE directive and re-adding it, there may be some order dependency requirement here for the component model to be recognized.

Without further ado, perform the simulation and probe the output:

Ternary inverters with BSS84 + BSS138, 10 kΩ resistors, ±2.5V supply

And it works! The inverter is clearly inverting, as shown in the last plot. When input voltage is less than about -1.2V, it outputs 2.5V, from -1.2V to 0.4V it outputs zero volts, there is an undesirable linear region, but then above 0.6V it outputs -2.5V. The logic level thresholds are not all that great, but not too bad either for common off-the-shelf components.

The resistors (here, 10 kΩ) can be adjusted to change the sharpness of the transitions, but at the cost of shrinking the logic level spans. 10 kΩ is fine.

2N7002 NMOS vs BSS138

How does the alternative 2N7002 N-channel transistor fare, replacing the BSS138? To test it, since the 2N7002 is a built-in transistor model in LTspice, whereas BSS138 is a subcircuit model, first I had to delete the nmos component and re-add it, then right-click (without shift) to choose 2N7002:

Fortunately, it is built-in, so no extra steps are needed to add it. Delete the BSS138, plop down the 2N7002 in the same place, and rerun the simulation:

Ternary inverters with BSS84 + 2N7002, 10 kΩ resistors, ±2.5V supply

The logic level transition from + to 0 isn’t as sharp with the 2N7002 vs BSS138, more similar to the 0 to - transition with the BSS84. This creates an unsafe region from about -1.1V to -0.9V (in addition to the existing +0.45V to +0.60V) where logic levels will not be unambiguously properly recognized.

Is it worth it? The 2N7002,215 is about 1¢ cheaper than the BSS138: $0.094 vs $0.095 on Digi-Key. BSS138 is very commonly used in logic level shifters such as Adafruit’s 4-channel I2C-safe Bi-directional Logic Level Converter — BSS138, or potentially the similar 8 Channel I2C IIC Logic Level Converter Module Bi-Directional for Arduino 1pc from Aliexpress (not visible from their product page, but the transistors read “J1 Y”, compared to Adafruit’s board with “J1 8” — possibly J1 Y is the SMD part code for KSA1298? but that’s a bipolar NPN transistor).

Expanding the search to Aliexpress, the price difference between the 2N7002 and BSS138 widens further:

This makes the 2N7002 a more attractive option, except where the characteristics of BSS138 absolutely are required (StackExchange: What can I replace a BSS138 N-Channel MOSFET with?

That level shifter circuit is trickier and more critical of part characteristics than most simple circuits are. The BSS138 is an “N Channel MOSFFET”. It has a much lower turn on voltage (Vgson) than most similar MOSFETs and this is important in this circuit.

) but for this circuit I’ve decided to go with the 2N7002. It even has its own Wikipedia page: 2N7000, and has been called an “absolutely ideal hacker part” by Richard Ross in Modern Electronics (1986), reinforcing this decision.

Plus, I can now delete the ugly .SUBCKT BSS138 directive from LTspice. New:

How well do these transistors work in practice? The 2N7002 parts I ordered arrived in 21 days, soldered it to a surface-mount prototyping board and tested with the MTester component tester I covered in Organizing electronic components:

Threshold voltage measured at 2.1 V, just as expected from the datasheet:

and suitable for logic-level switching in this N-channel enhancement-mode MOSFET.

As for the P-channel enhancement-mode MOSFETs, ordered the BSS84 and it arrived in 25 days. Soldering on the same protoboard and using the component tester, measured 1.9 V threshold (see the datasheet):

Dual Power Supply

3.3V is more common e.g. on the Raspberry Pi, but 2.5V has the advantage of being 1/2 of 5.0V. Plus the TL431 has a built-in 2.5V precision reference. To get our +2.5V and -2.5V we could split the 5.0V voltage. However, this is lesser than the forward voltage drop of the indicator LEDs we’ll see below, so I decided to go with ±5 V. Built a ±5 V split power supply from 24 V input in Surface-mount electronics for hobbyists: easier than you think.

Surface Mount: SOT-23 transistors and 0805/0603 resistors

As a budding hobbyist you may be inclined to stick exclusively with through-hole components, presumably easier to solder. But in some ways they are actually easier. The advantages of surface mount are hard to ignore: smaller physical dimensions, wider availability, lower cost.

The BSS84 and 2N7002 transistors come in SOT-23–3 packages:

SOT = small outline transistor, SOT-23 is “(TO-236–3) (SC-59): 2.9 mm × 1.3/1.75 mm × 1.3 mm body: three terminals for a transistor.” You can learn to hand-solder SOT-23. How hard can it be?

As for resistors, they are available in much smaller packages:

1206 resistors exist but seem to be falling out of favor. On the other end, smaller isn’t always better: very small components may require a microscope to position and solder correctly by hand, or more expensive automated equipment. According to EEVblog #127 — PCB Design For Manufacture Tutorial — Part 1, 0402 requires more modern equipment, so consider 0603 instead when designing for manufacturing. I’m not, so the choice comes down to 0603 or 0805.

Both 0805 and 0603 can be hand soldered. Travis Goodspeed GoodFET Facedancer21 uses 0603. So I’ll go with 0603 (imperial) and take the opportunity to continue practicing with small surface-mount components.

Here it is worth mentioning it can be useful to have a “sample book” of SMD components, an assortment of values to test with. Narrowed down to:

These kits also have capacitors, not needed for this particular circuit but could come in handy later so might as well get them in the same order. The first set is easy to eliminate, since it contains a smaller set of resistors, only up to 820 kΩ; having up to 2 MΩ would be more versatile. Is it worth getting the tighter 1% tolerance resistors instead of 5%?

Resistor tolerance: 1% vs 5%

Dave Jones in EEVblog #215 — Gaussian Resistors measures the distribution of 1% metal film resistors, observing a Gaussian distribution:

No truncated Gaussian was observed with these resistors, as the artist’s rendition in Dangerous Prototypes: Actual values of 10% tolerance resistors hypothesizes:

However I haven’t seen any actual testing results proving this. An insightful comment from that discussion thread:

Evan says: July 1, 2010 at 4:09 pm

Carbon resistors are not generally available at 1% or better, only 5% and 10%, so I doubt you will see a 1% gap in the 5% models. 1% resistors are made with processes that are more repeatable to begin with and also have lower temperature coefficient and less aging problems.

Additional testing of surface-mount resistors by @RossS:

There has been discussion of out-of-spec resistors, possibly counterfeit. What if I splurge on the 1% but they are way off? Better off with 5%?

Nonetheless there are some good arguments in favor of buying 1% resistors StackExchange: choosing surface mount resistor/capacitors:

1% parts are only slightly more expensive than their 5% counterparts, but they have the advantage of better reproducibility; individual devices (assemblies, not only components) are more likely to behave exactly the same.

1% resistor tolerance is good enough for most applications, select 0.1% Nichrome resistors if accuracy is needed

Since the price difference is not all that great, it may be that 1% tolerance SMD resistors are the new de facto standard over 5% through-hole:

Yaego datasheet: General Purpose Chip Resistors, RC0402, 5%, 1%, RoHS compliant & Halogen Free

The default tolerance in Octopart resistors search is 1%. Their Common Parts Library only has 1% resistors. 1% it is.

Scratch that: I found a sample book with 5% more readily available:

The sample components would of course be used in prototyping more than in production, if (and that’s a big if) this went to production, probably going with 1% would be a fine choice, but I don’t mind 5% for sampling.

Circuit board design

For designing a custom printed circuit board, I moved to KiCad and redrew the schematic (TODO: can netlists/schematics from LTspice import into KiCad eSchema?):

Following the KiCad Quick-Start Tutorial for tips. When assigning footprints, I used the “handsoldering” variants:

These have lightly longer pads, for easier soldering with an iron:

To keep your options open when ordering from PCB fabrication houses, as described in Notes on prototyping circuit boards: breadboards, perfboards, and beyond, it’s a good idea to keep the dimensions within 5 cm x 5 cm. Create exact 50 mm length construction lines using “E”: How layout 5cm x 5cm rectangle in KiCad?

This size fits about 9 x 2 inverters. How to repeat the circuit? One solution is to create a hierarchical circuit, then right-click > Copy Block, see How do I copy a hierarchical module in KiCAD?. This confuses the annotations, so it has to be cleared and rerun (Annotate Schematic Components > Clear Annotation), per the pointer from Multiple Sheet Schematics in KiCad.

Ended up going with 6 inverters in a row, after too much manual laying and alignment (TODO: possible to automate repetitive PCB KiCad layout?)

Routing the tracks and vias, then rendering in webGerber (double-sided):

The board is shaping up. Now it only needs headers, for:

  • Vss: negative power supply
  • Vdd: positive power supply
  • Vin_1–6: inputs for each of the six gates
  • STI_1–6, NTI_1–6, PTI_1–6: outputs for each of the six gates

but, inverters are not all that interesting by themselves.

Before moving forward with getting this board manufactured, I’d want to add a selection of other more interesting circuitry out of the scope of this blog post, so I’ll put this board on hold for now. It isn’t worth fabricating a custom board only for this one logic gate just yet.

Instead, for the time being I’ll build the circuit on one of these surface-mount protoboards, you may recall from Surface-mount electronics for hobbyists: easier than you think:

Constructing a simple ternary inverter prototype

Built the circuit using a 2N7002 N-channel MOSFET (7002), BSS84 P-channel MOSFET (P-D), two 10 kΩ 0603 5% resistors, and wired it up:

Here is a closeup of the logic circuit:

Graphing the full range of input voltage vs output voltage would be interesting to see, but with only a multimeter I was able to measure the output voltage for each logic level input voltage:

  • Input -5.030 V → output +5.060 V
  • Input 0.000 V → output +0.057 V
  • Input +5.053 V → output -5.030 V

This corresponds to the behavior of a ternary inverter, as we expect.

Ternary logic signal visual output

To add some visual flair to the circuit, and to easily see what logic levels the signals are at, it would be useful to have some indicator LEDs.

Ordered these bicolor surface-mount LEDs:

Arrived in 9 days and I carefully soldered them onto a board. Very first time surface-mount soldering (even before I posted Surface-mount electronics for hobbyists: easier than you think), use a lot of flux, ESD-safe precision tweezers (a Wiha 44501), quality solder and a steady hand. These 0603 LEDs have 4 leads, tied them together back-to-back then it indicates red/green by polarity:

To use in a real circuit, it is important to note the red and green have a different forward voltage drop (Vf): 2.0–2.2 V red, 3.0–3.2 V green (my multimeter displays “overload” in diode mode). Both expect 20 mA current, at most (250 Ω current-limiting resistor). These LEDs will light up even with 0.05 mA (100 kΩ), albeit dimly. 1 kΩ, 10 kΩ? To keep it simple by using the same value as the pull-middle resistors, and to choose a middle ground for moderate brightness, I went with 10 kΩ:

Connecting the logic input to positive and negative rails lights up red and green, respectively. We’ll use these LEDs on the inverter input and output. Here it is showing a negative (green) inverting into a positive (red):

If the input is changed to a positive (red), then the inverted output is negative (green), and if the input is off the the output is off, as you would expect from a ternary logic inverter, and as shown in the first picture of this article. We have successfully built and tested the ternary logic inverter.

Positive and negative ternary inverters

A plethora of one-input ternary logic gates are possible (a total of 3³ = 27, much more than binary’s 2² = 4). The “simple ternary inverter” is arguably the simplest, but there are also two trivial variations on this gate, changing the output when an input of zero is given. Annotating Mouftah’s diagram:

The same components can be used for these extra inverters, we simply take the output on either side of the pull-middle resistors. Adding two more bicolor LEDs for the positive and negative ternary inverters, there is no difference when a negative input is given — all inverters (positive, simple, negative) output positive:

conversely, positive input results in negative output for all three inverters:

Here is where the inverters differ. When given zero input, the simple ternary inverter (shown in the middle) also outputs zero, but the positive ternary inverter (left) outputs positive, and the negative inverter outputs negative:

Conclusions

We have seen the design and implementation of simple, positive, and negative ternary inverters, built using a P-channel MOSFET (BSS84), an N-channel MOSFET (2N7002), and two 10 kΩ resistors. Powered by a ±5 V power supply, the ternary logic inverters are constructed entirely from surface-mount components, including bicolor LEDs for indicating the gate’s input and output logic levels.

This is only a basic introduction to ternary logic circuits, but many of the same concepts could be expanded further into more complex circuitry.

--

--