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.  

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.

NOT Gate

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.

AND Gate

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.

OR Gate

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.

Exclusive OR Gate

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.

NAND Gate

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.

NOR Gate

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!

Exclusive NOR 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.

NAND from AND and NOT

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).

NOT from NAND

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.

AND from NAND

With three NAND gates, we can make an OR gate.

OR from NAND

And, of course, if we invert the output of that we’ll have a NOR gate.

NOR from NAND

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!

Buffer

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.

Buffer fanout

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.

Tri-state Buffer

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. 

Tri-state 0

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.

Tri-state 1

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.

Tri-state 2

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.

Schmitt Trigger

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.

Schmitt Trigger noise-1

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.

Schmitt Trigger noise-2

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”.

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:

7400 Logic Family

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.

TTL Totem-Pole Output

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.

TTL Open-Collector Output

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:

General Pinout

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.

74LS00

74LS08

The 74LS08 is a Quad AND gate with the same pinout.

74LS08

74LS32

The 74LS32 is a Quad OR gate.

74LS32

74LS86

The 74LS86 is a Quad Exclusive OR or XOR gate.

74LS86

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.

Logic Demo Hookup

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. 

Logic Demo

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:

Arduino Logic Emulator Hookup

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:

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.

Arduino Logic Emulator

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:

  1. We could use an Arduino Mega 2560, which has six interrupt inputs.
  2. 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.

74LS132 Hookup

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:

Alarm Demo Hookup

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:

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”.

Alarm 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”.

Alarm cannot reset

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!

 

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.

 

 

Using Basic Logic Gates – With & Without Arduino
Summary
Basic Logic Gates - With & Without an Arduino
Article Name
Basic Logic Gates - With & Without an Arduino
Description
Learn to use basic logic gates in your designs, both on their own and in tandem with an Arduino Uno. We will cover all of the theory behind elementary logic gates and perform a few experiments before building our alarm.
Author
Publisher Name
DroneBot Workshop
Publisher Logo

If you have a question...

Comments about this article are encouraged and appreciated. However, due to the large volume of comments that I receive, it may not be possible for me to answer you directly here on the website.

You are much more likely to get answers to technical questions by making a post on the DroneBot Workshop Forum. Your post will be seen not only by myself, but by a large group of tech enthusiasts who can quickly answer your question. You may also add code samples, images and videos to your forum posts.

Having said that, please feel free to leave constructive comments here. Your input is always welcome. Please note that all comments may be held for moderation.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Marian Kall
2 months ago

FANTASTICO, SIR!

1
0
Would love your thoughts, please comment.x
()
x