Table of Contents
The fundamental building block of digital logic devices is the logic gate, and today we will take a close-up look at these devices. Even in this age of microcontrollers, basic logic gates are still in use, so knowing how they work and how to use them is a valuable skill to acquire.
We will examine the most basic of these devices today. Future articles and videos will expand our coverage to other, more complex, logic gates.
Introduction
We live in a world whether virtually everything is digital. Our radios, televisions, and audio equipment are no longer analog devices, we all own computers and smartphones, and our appliances and even our light bulbs are now microcontroller-based.
Unlike analog signals, which can exist at an infinite number of levels, digital signals exist at only two levels – zero and one. Our digital devices use two distinct voltage levels to represent zeroes and ones. And these signals are modified using basic logic gates.
Sometimes LOTs of basic gates. Microcontrollers and microprocessors use thousands, even millions, of these gates. Just as simple atoms form molecules which in turn can form crickets, elephants, or people, basic gates are the underlying foundations of all digital electronic devices and components.
But it’s the 21st Century!
With the availability of inexpensive microcontrollers, one might think that basic logic gates are no longer used for new designs. That thought is incorrect.
Basic logic gates are still widely used, both with and without microcontrollers. For simple circuits, they can offer both cost and performance advantages. They can also be used along with CPU’s and MCU’s to create very complex designs or to simplify programming.
Even if you don’t design our circuits using basic gates you can still benefit from learning about them. You’ll be able to troubleshoot existing circuits and you’ll also gain a better understanding of the inner working of modern electronic components.
So let’s dive in!
Basic Gates
When you examine a catalog of basic digital components you’ll run across devices with interesting names like “monostable multivibrators” or “edge-triggered flip-flops”. While these devices are certainly elementary digital “building blocks” they are not basic gates. Actually, they are composed of basic gates.
A “basic gate” is defined as a component with one or more inputs and one output. The inputs and outputs are all digital.
There are three fundamental gates and a total of seven basic logic gates (plus a number of derivatives).
The gate will set its output to either zero or one, based upon the state of the input signals. It uses the rules of boolean algebra to determine the output condition.
Truth Tables
The relationship between the input and output logic levels on a gate can be best illustrated using what is known as a “truth table”.
A truth table is a table or chart that lists all of the possible input combinations and the resulting output logic level. Here is an example of a truth table:
A | B | Y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
The inputs to our gate are labeled “A” and “B”, and the output is labeled “Y”.
- When the inputs to our gate are both zero the output is zero.
- When A=0 and B=1 the output is one.
- When A=1 and B=0 the output is one.
- When both inputs are one then the output is zero.
By looking at the truth table you can determine the logic of the gate. Incidentally, the above truth table is for an “Exclusive OR” gate. We’ll examine that gate, and six others, right now.
NOT Gate
The NOT gate is the simplest of all the gates, it is also the only gate that only has one input. It is the first of the three elementary gates.
This type of gate is sometimes referred to as an “Inverter”, and from the truth table, it’s easy to see why. The output of the NOT gate is the inverse of its input, in other words, it changes zeros into ones and ones into zeros.
The boolean algebra description of the NOT gate is also shown, the line above the “Y” indicates that the output is inverted.
While it may indeed be a simple gate the NOT gate is very useful, as inverting a digital signal is a common requirement when designing logic circuits.
AND Gate
The AND gate is the second of the three elementary gates, and its operation is illustrated below.
As the truth table shows the AND gate has an output of zero unless all of its inputs are at one.
You’ll also note that the boolean symbol for an AND operation is a “dot”.
OR Gate
The third of the three elementary gates is the OR gate, illustrated below.
The output of the OR gate is one whenever any of its inputs are set to one.
The boolean symbol for the OR operation can be a bit confusing, as it is the same “+” symbol used for addition in standard mathematics. In boolean algebra 1 + 0 does indeed equal 1, but 1 + 1 also equals 1.
Exclusive OR Gate
The remaining four gates are mainly derivatives of the first three.
The Exclusive OR, or XOR gate, is shown here. Pay attention to its truth table, as it explains the operation best.
As you can see the output of an Exclusive OR gate is set to one when only one input is set to one. Setting more than one input to one will cause the output to become zero.
The boolean symbol for Exclusive OR is the “+” sign (a boolean OR) with a circle around it.
NAND Gate
A NAND Gate is simply an AND gate with an inverted output.
In this case, the operation is exactly opposite to that of the AND gate. The output is one unless all of the inputs are one.
This is a very common gate, and in a moment I’ll explain why.
NOR Gate
As with the NAND gate, a NOR gate is just an OR gate with an inverted output.
The only situation in which a NOR gate will output a one is if both of the inputs are zero.
Note the boolean formula for a NOR gate uses a line to indicate an inversion.
Exclusive NOR Gate
You guessed it, and Exclusive NOR gate is an inverted Exclusive OR gate!
You’ll sometimes see this written as an “XNOR” gate. Its output is a one providing all of the inputs are the same, either all zeros or all ones. Any other combination results in an output of zero.
NAND Gates as “Universal” Gates
As we just saw, a NAND gate is essentially an AND gate with an inverted output. In fact, you could easily construct a NAND gate by combining an AND gate and an Inverter, as shown here.
As it turns out NAND gates are about the most common gate around. This is because you can create any of the other basic logic gates using a combination of NAND gates.
To make an inverter or NOT gate you can just tie all of the inputs of a NAND gate together (this same trick will work with a NOR gate as well).
Now since you can make an inverter with a NAND gate it becomes easy to create an AND gate. Just invert the output of another NAND gate, as shown here.
With three NAND gates, we can make an OR gate.
And, of course, if we invert the output of that we’ll have a NOR gate.
Some chip designers only use NAND gates and create all of the other logic gates with them. It may seem wasteful, but with chip densities being what they are these days it’s not a big deal, and the chip is easier to design if all of the gates are the same.
Buffers & Schmitt Triggers
There are a few other logic components you should know about. These are not really “gates”, rather they are elements that either enhance the operation of the gates or allow them to be interconnected in the ”real world” where you have to worry about things like maximum current capabilities and noisy logic signals.
Buffers
At first glance, the buffer seems like the most useless “gate” there is. That’s because logically it does nothing at all!
The input to the buffer equals its output. And while that seems about as useful as just using a piece of wire, it actually has a very good use.
In the “real world” that I referred to, a logic gate has a finite number of other gates it can drive before the current demands are too great. The number of gates a chip can drive is referred to as its “fan-out”. So a gate with a fan-out of 2 can only drive two other gates.
Buffers typically have a higher fan-out and can therefore be used to drive several other gates.
In the following illustration, a portion of a logic circuit is shown. The buffer is driving the inputs on the XNOR, NOT, and AND gates, as the NAND gate is not capable of doing that itself.
In addition, buffers can be used to drive high-current devices, such as LEDs. More on that in a bit.
Tri-state Buffer
A TRi-state buffer is illustrated here. As you can see, it’s essentially a buffer with another input.
The first thing you likely will notice is the odd truth table, it has two outputs labeled “H” instead of one or zero. What’s up with that?
The extra input, labeled “E”, is an Enable line. It essentially lets you turn the buffer on or off.
When the buffer is on or enabled, it essentially acts just like a normal buffer, allowing the data to pass through unaltered.
But when the buffer is off, or disabled, it puts the output into a high-impedance state. In other words, it is disconnected, there is no output at all. This is very different from just a zero.
Tri-state buffers are used extensively when driving a common data bus. Only one component at a time can drive the data bus, so by employing tri-state buffers we can ensure that.
This can be illustrated using three simple diagrams.
The first diagram shows our circuit. It has multiple inputs in two sections. The top section uses a NAND gate, the bottom one uses an Exclusive NOR (XNOR) gate. Both these sections feed a tri-state buffer, and the output of both buffers are connected together and fed into the input of a standard buffer.
In the first illustration both buffers are disabled, so neither section sends its output to the final buffer.
In the second illustration, we have enabled the top tri-state buffer. The output of the NAND gate is now allowed to pass to the input of the output buffer.
In the final illustration, the lower tri-state buffer is enabled, while the top one is disabled. The output of the XNOR gate is not presented to the input of the final buffer.
Of course, it is imperative that you never enable both of the tri-state buffers simultaneously. Doing that would send both signals to the output buffer, an undesirable situation both logically and electrically.
Incidentally, there are other gates available that have tri-state outputs, essentially they are a fusion of a gate and a tri-state buffer.
Schmitt Triggers
The Schmitt Trigger is not really a gate, although I have shown it as a NOT gate.
As a gate, the Schmitt Trigger operates like any other. The special property of this device lies within its electrical characteristics.
Back in that “real world” which I mentioned previously electrical signals are not always perfect. Connections made with thin PCB traces running next to other connections in an electrically “noisy” environment can result in degraded signals.
A Schmitt Trigger can “clean up” this signal. Internally a Schmitt Trigger contains a couple of comparators, devices that sense whether a voltage is above or below a specified level. These comparators treat every signal above a specified threshold as a one, and signals below that as a zero.
Every basic gate is also available as a Schmitt Trigger.
In the following illustration, we have a NAND gate driving a buffer using a long line, which is picking up electrical noise. This results in an output from the buffer that doesn’t match the output of the NAND gate, a situation to be avoided at all costs.
If we replace the buffer with a buffer that contains a Schmitt Trigger then the situation is resolved. Although the signal still gets degraded the Schmitt Trigger cleans it up and restores it back to its original condition.
A handy component indeed, at least in the “real world”!
Logic Gate Families
There are thousands of logic chips, and they can be divided into groups based upon their underlying technology. These groups are referred to as “logic families”.
The original logic chips were constructed over half a century ago using bipolar technology. There were many bipolar-based families:
- DL – Diode Logic
- RTL – Resistor-Transistor Logic
- DTL – Diode-Transistor Logic
- TTL – Transistor-Transistor Logic
Of these only TTL chips remain today. The digital “breakthrough products” of the 1960s and 1970s were all built with TTL chips, for a long time they were the dominant logic chip family.
But TTL chips were not perfect. While they are fast and reliable they also had a couple of big drawbacks.
One issue was that they consumed a lot of current due to their bipolar transistor design, so battery-powered equipment was not usually possible (and remember, battery technology in the 1960s was not what it is today).
Also, they required a strictly regulated 5-volt power supply, in fact, they are what gave rise to 5-volts becoming the “standard” logic voltage for a long time. And, in turn, they made the 7805 linear voltage regulator a very popular component!
Other families were created using Metal Oxide Semiconductors, or MOS devices.
- PMOS – Positive Metal Oxide Semiconductor.
- NMOS – Negative Metal Oxide Semiconductor.
- CMOS – Complementary Metal Oxide Semiconductor.
All of these technologies are still in use today, with CMOS logic chip families being very popular for battery-powered devices. CMOS chips consume minimal power, have large fan-out capabilities, and work on a wide range of power supply voltages.
There are also families based upon BiMOS, a fusion of bipolar and MOS technology. Bipolar devices still have speed advantages over MOS, so combining these on a single chip allows for the best of both worlds.
TTL Logic – 7400 Series
The most common logic devices are the 7400 series of TTL logic chips.
The TTL or Transistor-Transistor Logic chips are constructed using bipolar transistors, although some newer variations use MOSFETs. They are fast and efficient and most of them require a pretty well-regulated 5-volt power supply.
The most common TTL logic chips are variations of the 7400-series chips. This series contains all of the basic logic gates as well as several more complex logic elements.
The original 7400-series TTL logic chips were developed by TRW Electronics in 1961, and they were first produced and marketed for general consumption by Sylvania in 1963.
There are many variations of 7400-series integrated circuits, some of the more common ones are shown in this chart:
The original 7400 series chips are no longer manufactured, but all of the variations produced have maintained the same pinouts.
The 74LS-series are very common and very easy to obtain. We will be using them in our experiments today.
The 74HC-series makes an excellent choice for new designs. Although they maintain the same pinouts and similar specifications, these chips are actually of CMOS construction and therefore consume a lot less current and can operate on a wide range of supply voltages that don’t necessarily require regulation.
If you want to intermingle newer CMOS-based designs with existing 74LS-series bipolar chips a good choice is the 74HCT-series, which are CMOS but are compatible with the bipolar TTL chips.
There are also a 5400-series of logic chips, these are the military-spec versions of the 7400-series chips. They are pin-for-pin compatible and can operate over a wider range of temperatures. Despite being military-grade they are easily obtainable and are excellent choices when designing devices that need to operate outdoors.
TTL Totem-Pole Output
There are a couple of different design techniques used in the output section of TTL gates, and you need to know a bit about this to choose the correct chip for your application.
The Totem-Pole style output is the most common, consisting of two transistors, along with a current limiting resistor and a diode.
In operation only one of the transistors is switched on, bringing the output to either ground or near the supply voltage.
This arrangement is fast and consumes a low amount of current.
The same design can be used for tri-state logic gates. By turning both transistors off the output is at a high-impedance.
TTL Open-Collector Output
The Open-Collector output is used on TTL chips that are meant to drive external loads that would exceed the capabilities of standard chips.
In this arrangement, only one transistor is used, and it just switches a connection directly to ground. The load must be applied between the output and the power supply voltage, which does not necessarily have to be 5-volts.
Open-collector chips are often used to drive LEDs. small relays and other low-current devices.
CMOS Logic – 4000 Series
On the CMOS side of things, the 4000-Series of logic chips have been a standard since they were introduced by RCA in 1968.
These chips became popular for battery-powered designs as they required much less current than their TTL counterparts and could operate on a wide variety of supply voltages.
They also have an increased fan-out capability, making buffers unnecessary for most designs.
One drawback, however, is that the 4000-series of chips are slower than the 7400-series chips, however, newer versions have narrowed that gap and many designs don’t require blinding speeds.
Despite being over half a century old these chips are still in use today and can be used in new designs.
Working with Logic Gates
OK, enough theory – time to get down to work!
We are going to do a very simple experiment using some basic logic chips, all from the TTL 74LS family and series. These are probably the easiest logic chips for hobbyists to obtain.
When you started with the Arduino it was traditional to run the “Blink” sketch. Your first Python program was likely a simple “hello world” program.
So to follow that pattern of starting with something basic we will test out a few basic logic gates. And we will take advantage of the fact that several TTL chips with different gates have the same pinout.
74LS Series Chips with Common Pinouts
We will be working with four chips that are packaged in 14-pin DIP packages. The 14-pin DIP can be easily used on a solderless breadboard.
All of these chips have the same pinout, as shown here:
You’ll note that the VCC (5-volt) connection is on pin 14 and the ground connection is on pin 7. This diagonal placement of power and ground is common to most (but not all) TTL chips.
The first gate in the chip, gate 1, has its A input on pin 1 and its B input on pin 2. Its Y output is on pin 3. The pattern is repeated for the other three gates in the chip.
74LS00
The 74LS00 is a Quad NAND gate, meaning that there are four NAND gates in the package.
74LS08
The 74LS08 is a Quad AND gate with the same pinout.
74LS32
The 74LS32 is a Quad OR gate.
74LS86
The 74LS86 is a Quad Exclusive OR or XOR gate.
Testing the Logic Gates
We are going to hook up a very simple circuit to test out our logic gates. You can wire this up on a small solderless breadboard. Try and route the wires around the chip so that you can easily replace it.
Our circuit consists of two pushbuttons, along with two 2.2k pulldown resistors. These are arranged so that pushing one of the buttons will cause it to send 5-volts to its respective input, which of course is a digital one. Otherwise, the pulldown resistors will keep the logic at zero.
An LED is tied to the gate’s output, through a 330-ohm dropping resistor. Note that since we are only testing one of the gates in the package we can drive an LED directly. If you wanted to use all four gates to drive LEDs you would likely want to use a buffer or some transistors, as the total current consumption could exceed the chips rating.
The power and ground are connected to pins 14 and 7 respectively.
Insert a chip, say a 74LS00, and power up the board using a 5-volt power supply.
Now observe the state of the LED. If you inserted a 74LS00 Quad NAND gate then the LED should be illuminated, indicating a digital one on the output. This is correct, as both inputs are at digital zero.
Press one of the pushbuttons, the output will remain unchanged. Release it and press the other button, again the output LED will remain lit.
But if you press both buttons, which sends a digital one to both inputs, the LED will extinguish, as the output of a NAND gate is zero when both inputs are ones.
Experiment with the other three chips and test your results against the truth tables for those gates.
Simulating Gates with an Arduino
All of the boolean algebra functions performed by the basic logic gates can also be emulated on an Arduino.
With that in mind, we are going to construct a logic gate emulator that will emulate six of the seven basic gates.
We won’t be including the NOT gate, although it wouldn’t be a problem to add it. I just didn’t include it because (a) it only has one input whereas the other gates have two and (b) the function of a NOT gate is so simple that we really don’t need an emulator for it!
Why would we want to do this? A couple of reasons spring to mind.
- It is a simple but worthwhile programming exercise. You’ll learn about all the boolean algebra functions in the Arduino C++ language, which in turn may help you see opportunities to replace a bunch of code with a logic chip. Plus most of us don’t know the character to use for an Exclusive OR, so if anything you’ll learn that!
- It makes a great trainer. A good exercise is to not label to output LEDs and have someone try and determine which LED represents which logic function. The user will get very familiar with logic gates after using this.
So let’s wire this up and program it!
Arduino Emulator Hookup
Here is the hookup for our logic emulator:
The LEDs can be any color you like, I actually used green LEDs for the logic outputs but I’ve shown yellow ones on the schematic simply because they are easier to see on the blue background!
The dropping resistors for the LEDs are all 220 ohms, any value from 150 to 470 ohms will work fine.
The two pulldown resistors for the pushbutton switches are 2.2k each, any value near here will work.
Emulator Code
Here is the sketch that we will use for our logic gate emulator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
/* Logic Emulator logic-emulator.ino Emulates 6 basic 2-input logic gates Input from 2 pushbutton switches Output to LEDs and Serial Monitor DroneBot Workshop 2020 https://dronebotworkshop.com */ // Define boolean inputs boolean inA; boolean inB; // Define boolean outputs boolean outXNOR; boolean outXOR; boolean outNOR; boolean outOR; boolean outNAND; boolean outAND; // Define the pushbuttons int pbA = 4; int pbB = 5; // Define the logic output LEDs int ledXNOR = 6; int ledXOR = 7; int ledNOR = 8; int ledOR = 9; int ledNAND = 10; int ledAND = 11; // Define the logic input LEDs int ledA = 12; int ledB = 13; void setup() { // initialize serial port at 9600 bps: Serial.begin(9600); // Define pushbuttons as inputs pinMode(pbA, INPUT); pinMode(pbB, INPUT); // Define LEDs as outputs pinMode(ledXNOR, OUTPUT); pinMode(ledXOR, OUTPUT); pinMode(ledNOR, OUTPUT); pinMode(ledOR, OUTPUT); pinMode(ledNAND, OUTPUT); pinMode(ledAND, OUTPUT); pinMode(ledA, OUTPUT); pinMode(ledB, OUTPUT); } void loop() { // Read the pushbuttons and assign values to input booleans inA = digitalRead(pbA); inB = digitalRead(pbB); // Display the pushbutton inputs on LEDs digitalWrite(ledA, inA); digitalWrite(ledB, inB); // Compute the logic outputs outXNOR = !(inA ^ inB); outXOR = inA ^ inB; outNOR = !(inA | inB); outOR = inA | inB; outNAND = !(inA & inB); outAND = inA & inB; // Display the results Serial.print("IN-- A:"); Serial.print(inA); Serial.print(" B:"); Serial.print(inB); Serial.print(" --- OUT-- AND:"); Serial.print(outAND); Serial.print(" NAND:"); Serial.print(outNAND); Serial.print(" OR:"); Serial.print(outOR); Serial.print(" NOR:"); Serial.print(outNOR); Serial.print(" XOR:"); Serial.print(outXOR); Serial.print(" XNOR:"); Serial.println(outXNOR); digitalWrite(ledXNOR, outXNOR); digitalWrite(ledXOR, outXOR); digitalWrite(ledNOR, outNOR); digitalWrite(ledOR, outOR); digitalWrite(ledNAND, outNAND); digitalWrite(ledAND, outAND); // Short delay delay(50); } |
This is a very simple sketch, the most interesting part being the code that emulates the gates.
We first define a number of boolean variables to represent the logic states of both the two inputs and six outputs.
Next, we define some integers to represent the connections to the Arduino from the LEDs and pushbuttons.
We will also display our logic states on the serial monitor, so in the Setup routine, we initialize the monitor at 9600 bps. The rest of the Setup is used to define the LEDs as outputs and pushbuttons as inputs.
In the Loop, we start by reading the state of the two pushbuttons, and then displaying the results on the two Red LEDs marked “A” and “B”.
Now we do the actual boolean math, consisting of the following four characters:
- NOT = !
- AND = &
- OR = |
- XOR = ^
We use the NOT function (!) to create the NOR, NAND, and XNOR gates.
After determining the boolean results they are sent to both the serial monitor and the LEDs. Following a short delay the whole Loop repeats.
Load the sketch and give it a try. Cycle through all four combinations of the two pushbuttons and observe the LED statuses, as well as the status on the serial monitor.
This would be a great training tool for testing your knowledge of digital logic.
Using Gates with an Arduino – Intruder Alarm
In the last experiment, we emulated the basic logic gates with an Arduino. Now we’ll do something much more practical, combine a TTL logic chip with an Arduino to increase its capacity.
We have actually done this already with some TTL shift registers. In that case, we used the shift registers to expand upon the number of input and output connections to our Arduino.
This time we’ll be using a simple logic chip to expand the number of interrupt inputs. And we will use this arrangement to construct a simple intruder alarm.
Intruder Alarm Specs
Our alarm will have three inputs, plus an emergency pushbutton switch. There is also a reset switch to clear the alarm.
Two of the inputs are Closed-Loop inputs. This type of input requires a connection between the two terminals. If the connection is broken the alarm is activated.
This is a very common type of alarm connection. Sensors could include magnetic reed switches on doors and windows. In this arrangement, the reed switch (mounted on a door or window frame) is held closed by a magnet mounted on the door or window. If the door or window is opened the magnet moves away and the connection is broken.
Another common closed-loop alarm sensor is simple conductive foil tape, placed on windows. If the window is broken the tape is cut and it sounds the alarm.
Closed-loop alarm systems have a big advantage in that they are hard to thwart. Cutting the wire to a sensor will activate the alarm, instead of disabling it. Of course, you need to ensure that your connections are really closed, otherwise you’ll experience false alarms.
The third input is Open-Loop. In this case, the alarm will sound if a connection is made between the contacts. Any type of switch or relay could be used to activate the alarm. A popular sensor for this type of alarm is a pressure switch hidden under a doormat or carpet.
Unlike the Closed-Loop system, you can easily thwart an Open-Loop sensor by cutting its wires.
The alarm actually does have a second Open Loop connection, it’s used by the emergency switch.
Multiple Interrupts
Both of the Open-Loop and Closed-Loop connections will be used to generate an interrupt to trigger our alarm code. As we have four inputs (including the emergency stop switch) we require four interrupt inputs.
But an Arduino Uno only has two interrupt connections. How can we resolve this?
There are a couple of ways:
- We could use an Arduino Mega 2560, which has six interrupt inputs.
- We could add a couple of interrupt inputs to our Arduino Uno.
Obviously, for the purpose of this article, we will be doing the latter.
In our design, we will activate the alarm when an interrupt goes high. We will use one interrupt for the Open Loop alarms and one for the Closed Loop section.
For the Closed-Loop section, we need to detect when one of the two inputs goes low (zero) and send a high (one) to the interrupt pin when that happens. That sounds like a good job for a NAND gate, as it’s output is low as long as both inputs are held high.
For the Open-Loop section we want the opposite, trigger the alarm with a high interrupt input if any of the inputs goes high. A perfect job for an OR gate.
So we will require one NAND gate and one OR gate to build our alarm.
74LS132 Quad NAND Gate with Schmitt Trigger
I’m going to use one chip to build both of the gates we need.
As you’ll recall, a NAND gate is also used as a “universal gate”, meaning you can build all of the other gates using NAND gates. You can construct an OR gate using three NAND gates, meaning a quad NAND gate device like the 74LS00 I used in the earlier experiment would work fine.
I opted to use a 74LS132 instead. It’s a quad NAND gate with the same pinout as a 74LS00, but it also has internal Schmitt Triggers. That should help clean up noise on the alarm sensor lines and reduce false triggering. But if you can’t find one you can certainly use the 74LS00.
Here is the hookup of the 74LS132 quad 2-input NAND gate with Schmitt Trigger.
Look at the diagram and see if you can follow the logic behind it.
Alarm Hookup
For our alarm demo, we will just be using an LED as the output, but of course, on a real alarm, you’d likely want to use a transistor or relay to drive a high-powered buzzer.
Also, remember this is only a demo. If you want to you can use it as the heart of a proper alarm project. As it is you’ll likely be able to only run the loops a few meters at most before the electrical noise becomes too great for even the Schmitt Triggers to handle. Optoisolators or relays would assist with this.
And the alarm also prints out status messages to the serial monitor. In a production unit, you’d likely want to replace that with an LCD or OLED display.
For our experiment we will hook things up as follows:
In addition to the 74LS132 (or 74LS00) you’ll need the following parts:
- An Arduino Uno
- An LED to use as the alarm indicator
- A 330 ohm dropping resistor for the LED
- Two SPST pushbutton switches (Emergency and Reset)
- A 10k pulldown resistor for the Reset switch
- Four 2.2k pulldown resistors for the logic gates
Hook up the circuit as shown in the diagram, making sure to close the two Closed Loop connections (CL1 and CL2) and leave the Open Loop connection (OL1) open.
Alarm Code
Here is the sketch we will use for our intruder alarm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
/* Alarm Demo with TTL Logic alarm-demo-ttl.ino Uses 74LS132 Quad NAND Gate with Schmitt Trigger Monitors 2 open loop and 2 closed loop alarm circuits Output to LEDs and Serial Monitor DroneBot Workshop 2020 https://dronebotworkshop.com */ // Alarm status variable volatile int alarmState = 0; // Reset button state variable int buttonState = 0; // Alarm loop pin values int openLoopVal; int closedLoopVal; // LED Alarm Indicator int alarmLED = 13; // Alarm Reset Switch int alarmReset = 12; // Alarm Interrupt inputs int openLoop = 2; int closedLoop = 3; void setup() { // Initialize serial port at 9600 bps: Serial.begin(9600); // Define inputs and outputs pinMode(alarmLED, OUTPUT); pinMode(alarmReset, INPUT); // Attach alarm interrupt handlers attachInterrupt(0, ol_ISR, CHANGE); attachInterrupt(1, cl_ISR, CHANGE); } void ol_ISR() { // Open Loop intrusion detected // Change alarm state alarmState = 1; // Activate alarm LED digitalWrite(alarmLED, HIGH); } void cl_ISR() { // Closed Loop intrusion detected // Change alarm state alarmState = 2; // Activate alarm LED digitalWrite(alarmLED, HIGH); } void clearAlarm() { // Clear alarm LED digitalWrite(alarmLED, LOW); // Reset the alarm state variable alarmState = 0; // Print to Serial Monitor Serial.println("Alarm Reset"); } void loop() { // Print alarm status to serial monitor if (alarmState == 0) { Serial.println("OK"); } else if (alarmState == 1) { Serial.println("Open Loop Alarm"); } else if (alarmState == 2) { Serial.println("Closed Loop Alarm"); } // Check for reset pushbutton buttonState = digitalRead(alarmReset); if (buttonState == LOW) { // Reset has been pressed // Read current loop status values openLoopVal = digitalRead(openLoop); closedLoopVal = digitalRead(closedLoop); if (openLoopVal == HIGH) { // Open Loop condition still exists Serial.println("Cannot reset - Open Loop Alarm still exists"); } else if (closedLoopVal == HIGH) { // Closed Loop condition still exists Serial.println("Cannot reset - Closed Loop Alarm still exists"); } else { // Clear the alarm clearAlarm(); } } // Slight delay to end loop delay(100); } |
We start by defining integers to represent the alarm and button states. Note that the integer used for the alarmState variable is volatile, you need to do that to let the Arduino compiler know that the value may change during an interrupt service routine.
We also define integers for the values of the interrupt pins, as well as integers to represent the pins used for the switch, LED and two interrupt connections.
In the Setup, we initialize the serial monitor and define the LED pin as an output and the pushbutton pin as an input.
We also attach interrupt service routines to the two interrupt handlers, ol_ISR for Open-Loop interrupts, and cl_ISR for closed-loop interrupts. They trigger on a change of state, in our case going from low to high.
Those two interrupt handlers are defined next. These are the snippets of code that will be run when an interrupt is processed. They both do the following:
- They set the alarmState variable value. A value of “1” indicates an Open-Loop condition, a value of “2” is for a Closed Loop.
- They activate the alarm, which in our demo means they illuminate the LED.
The next function, clearAlarm, resets the alarm. It sets the alarmState value back to “0”, turns off the LED, and prints to the serial monitor.
Now to the Loop, where all of this comes together.
We start by checking the current value of alarmState. Remember, this variable is set to “0” when there is no alarm condition, “1” for an Open-Loop alarm, and “2” for a Closed-Loop.
If the value is zero then there is nothing to do, and we print “OK” to the serial monitor.
But if the value is a 1 or 2 then we are in an alarm state. We determine if it is an open or closed loop and print the results accordingly.
Next, we check for the reset pushbutton. This will be used to clear the alarm, but before we clear it we need to see if the alarm condition still exists. We do that, again by getting the value of alarmState.
If the alarm condition has been cleared then we call the clearAlarm function to clear it and set the alarmState variable back to 0.
But if we are still in an alarm condition we print the status to the serial monitor and then exit, as we cannot clear the alarm.
After a short delay the loop repeats.
Testing the Alarm
Load the code, open your serial monitor, and get ready to test the alarm!
If all is working properly the LED will be off and the serial monitor will display “OK”.
Now press the emergency pushbutton. The LED should turn on and the serial monitor will display “Open Loop Alarm”, as the pushbutton is in the open-loop section.
Press the reset button and everything should go back to normal.
Now try breaking one of the Open-Loop connections. The alarm should be activated again and the serial monitor will display “Closed Loop Alarm”.
Now try pressing the reset button. This time the alarm should not reset, and the serial monitor should display “Cannot reset – Open Loop Alarm still exists”.
Close the open-loop and try and reset again. This time it should reset.
This circuit is just experimental but it can make the basis of an actual alarm. You could use the Arduino’s I/O pins to attach sensors and add extra features,
Conclusion
Basic logic gates may be simplistic, but put enough of them together and you can build just about anything. They can be used on their own, or to supplement a microcontroller or microcomputer.
Knowledge of the operation of logic gates and an understanding of Boolean algebra will give you more insight into how the electronic devices that surround us and shape our lives operate internally.
And it’s pretty cool to think that all of this works by manipulating a bunch of zeros and ones!
Parts List
Here are some components that you might need to complete the experiments in this article. Please note that some of these links may be affiliate links, and the DroneBot Workshop may receive a commission on your purchases. This does not increase the cost to you and is a method of supporting this ad-free website.
COMING SOON!
Resources
Code & Spec Sheets – All of the Arduino code used in this article, along with spec sheets for the TTL logic gates used in the experiments.


FANTASTICO, SIR!
Awesome! Nice video Sir!
Very complex things are explained very simple language.
I don’t have any knowledge in electronics, so could please share basic video by you which explains drop down/pull up resistors? I got confused the way it is used.
Thanks
–Alex
I’m with you!
Great presentations, I was looking for info on rotary encoders the other day and stumbled onto your Youtube videos. I am enjoying your videos and presentations very much. I am a retired career IT guy, on the data, networking and software side, but I am learning a lot about the the little hardware pieces that make it all work from your site. On that point, I have acquired some Arduino UNO, NANO and related breakout boards and a collection of sensors. But the deeper I get into this hobby, the more electronic gear I need, like shift registers and logic… Read more »
Hi Kevin There are a couple of other Montreal area stores, but like Abra the shipping gets expensive (and Abra takes forever to ship it out). Like yourself, I am also confined to my home until the pandemic is over, which realistically looks like another couple of years. Addison Electronics is probably the biggest in the City, down by PiIX and the Metropolitan, and we have one near me called Access Electronique. They both have websites, but again the shipping is the issue. Another nearby supplier that I like is RobotShop, their Canadian location is in Mirabel so it’s close… Read more »
Thanks for the suggestions Bill, I will check them out, I vaguely remember Access. I didn’t know that RobotShop has Canadian location. Today, I ordered from Abra, Amazon and BuyaPi.
Also, after ordering a couple hundred dollars worth of “inexpensive” parts (this is like my other inexpensive hobby of sailing – only the wind is free), I realized that I still forgot to order an FTDI serial adapter for your ATmega328 tutorial.
I found project this particularly interesting and helpful because at some point the project has to move from breadboard to a PCB board. Thank you.
A very clear and informative explanation. I am incorporating a nand gate with arduino and it has helped solve an issue with my programming. I am new at this.
Hi Bill,
I asked this question in the YouTube comments but here might be a better place. What is the capacitor between the VCC and ground for?
Thanks
Thank you for the explanation. Very well explained. I do have one question though? Can you specify few applications where we still use TTL chips? Also, do you have a web page talking about ECL logic families?
I have this problem to solve. Please help.. Two tanks store certain liquid chemical substances that are required in a manufacturing process, each tank has a sensor that detects when the chemical level drops to 25% full. The sensor produce “high level” of 5 volts when the tanks are more than 1/4(1 quarter) full. When the volume of the chemicals in a tank drops to 1/4 full, the sensor puts out a “low level” of 0 volts. it is required that a single green light emitting diode on an indicator panel shows hen both tanks are more than 1/4 full.… Read more »
I had my students build this and something strange happened to a couple of them. The output of the pushbuttons were around 1.2V when the buttons weren’t being pressed and 5.0V when they were.
Is this normal?
I traded out buttons for some nicer ones, but same result.
One student circuit worked, and one did not.
I tested the chips separately and they were fine. Just not when put into the circuit.
Thanks,
Really like your stuff