The Digital I/O Handbook – Chapter 3

The Digital I/O Handbook

A Practical Guide to Industrial Input & Output Applications

Image: The Digital I/O Handbook

Digital I/O Explained

Renowned technical author Jon Titus and the President and CEO of Sealevel Systems, Tom O’Hanlan, clearly explain real-world digital input/output implementation from both a hardware and software perspective. Whether you are a practicing engineer or a student, The Digital I/O Handbook will provide helpful insight you will use again and again.

  • Covers a wide range of devices including optically isolated inputs, relays, and sensors
  • Shows many helpful circuit diagrams and drawings
  • Includes software code examples
  • Presents common problems and solutions
  • Detailed glossary of common industry terms

“What I like most is its mix of hardware and software. Most pages have a bit of code plus a schematic. All code snippets are in C. This is a great introduction to the tough subject of tying a computer to the real world. It’s the sort of quick-start of real value to people with no experience in the field.” – Jack Ganssle, The Embedded Muse, January, 2005.

You can purchase the Digital I/O Handbook for $19.95 by clicking here. The Digital I/O Handbook is FREE with any qualifying Sealevel Digital I/O product purchase.

Chapter 3 – Digital Inputs

Topics Covered

  • Introduction to input ports
  • Basic TTL inputs
  • Circuit isolation
  • Current sinks and sources
  • LED considerations
  • Monitor high voltages
  • Sense bits with software
  • Flags
  • Put it all together
  • A final note about I/O ports

Introduction to input ports
Few computers can operate without connections to external devices such as sensors, switches, or other equipment that informs software about external conditions. Computers also receive data from keyboards, disk drives, touchscreens, and similar devices, all of which transfer their information to a computer through a device called an input port.

You can think of many practical uses for input ports. Imagine a controller that counts parts on a conveyor belt. An electronic counter in the controller would provide data that a computer could obtain from an input port. Similarly, an input port connected to a digital thermometer would let a computer read temperature values at any time. The computer simply retrieves the specified data as requested by a software command.

At its simplest, an input port acts like a “gate” that lets information pass from an external device to a computer’s data bus at a specific time. The central processing unit (CPU), or its control circuits, provide a unique strobe pulse for each input port. That pulse “tells” a port when to transfer data onto the data bus so the CPU can
capture it. To control the transfer of data from an input port to the computer, the computer requires a port-control command that identifies a specific port. Only one device can use the bus at a time.

Figure 3-1 shows a typical input port. Any information present at the port’s eight inputs gets transferred to the CPU when the strobe pulse, IN303*, arrives. The single 74LS244 IC used in the example contains two independent 4-bit circuits. We’ve used them to form a complete 8-bit input port. Other TTL devices work equally well. (Our port-number assignments carry no significance and simply serve as examples. You can use port numbers within the ranges specified for the computer and software you plan to use. Some computers reserve I/O port numbers for internal and future use.)

The input port shown in Figure 3-1 might accept signals from on-off switches or other devices that produce TTL-compatible signals. As always, the external signals, the input-port circuits, and the computer must share a common ground.

Figure 3-1

Figure 3-1

A simple 8-bit input port places information on the bus only when the computer places a short logic-0 pulse on the IN303* line. At other
times, the port “disconnects” from the bus using three-state logic.

To avoid conflicts on the CPU bus, input ports must “connect” themselves to the CPU data bus only when they receive the proper strobe pulse. At all other times, they must “disconnect” from the bus. The disconnect operation requires special gates with three-state outputs. These gates provide the normal logic-1 and logic-0 outputs, and they also provide a disconnected or third state. In this state, they appear electrically disconnected from the data bus. You may hear designers refer to similar devices called three-state bus drivers or three-state buffers. These devices provide the capability to disconnect outputs from a bus or other conductor that carries signals from several sources.

Transfer of data from an external device through an input port to a computer requires a software command. This command causes the computer to generate the needed strobe pulse at the input port so that the port data flows onto the bus:

portdata = inportb(input_port_number)

The general command above addresses a specific port (input_port_number) and assigns the data from the port to a variable, in this case, portdata.

The following command would obtain data from the port shown earlier in Figure 3-1:

abcxyz = inportb(303)

Software examples in this chapter illustrate byte transfers, and they assume, unless shown otherwise, that a programmer has defined variables, such as input_port_number, portdata, and abcxyz to hold values. After acquiring the data from an input port, additional software commands can use the information to make decisions.

In essence, an input port takes a “snapshot” of the information present at the port when the port’s strobe pulse arrives from the CPU. The computer does not wait for data to arrive at the port from an external device; it simply says to the input port, “Give me what you have now.”

The inportb command shown above does not exist within some programming languages such as Visual Basic. Each manufacturer of add-in cards or devices supplies its own driver software. Drivers come in a library of routines that link a programming language to special operations, such as those that control I/O ports. Thus, drivers define new commands that a programmer can include in code to transfer data from an input port to a CPU.

You must follow instructions included with an I/O board to properly set up accompanying driver software. The setup process lets your application program know how to find and use the drivers on the computer hard drive. (The instructions that accompany a board and its drivers provide installation information and information about how to use drivers in your application program.)

Basic TTL inputs
An 8-bit input port can obtain information from on-off switches, encoded switches, sensors, keyboards, and other devices that produce TTL-compatible signals. As with output ports, you can use the input bits in any way you wish, perhaps using four input bits to get data from a hexadecimal keypad, and the remaining four bits for on-off switches, as shown in Figure 3-2.

Figure 3-2

Figure 3-2

An input port can accept data from many sources, such as individual switches and a hexadecimal keyboard. These external devices provide
TTL-compatible logic signals and a common ground to the computer.

CAUTION: Digital signals drawn in books often look perfect, but real-world signals usually include some noise and may not meet the electrical specifications for logic-1 and logic-0 signals in a given logic family. So, external digital signals may require some conditioning prior to connecting them to the TTL-compatible inputs at an input port.

You can use a Schmitt trigger circuit, available in most TTL families, to provide some signal conditioning. But first check the specifications for all input ports you plan to use. Some ports may come with built-in Schmitt triggers.

CAUTION: If you plan to connect simple on-off switches or pushbuttons to an input port, you may need to “debounce” the switch contacts. When a pair of mechanical contacts closes, they have a tendency to bounce for a short time. While doing so, the contacts may open several times as shown in Figure 3-3 for a closing SPST switch. Although these bounces end within a few milliseconds, circuits may detect them as several switch closures in rapid succession. In a double-throw switch, the movable contact DOES NOT bounce back and forth between the two stationary contacts; it simply opens and closes the connection at the contact it was switched to.

Figure 3-3

Figure 3-3

If a mechanical switch bounces momentarily, an attached logic circuit may see each bounce as a logic transition.

If necessary, you can build a switch-debounce circuit using a single-pole double-throw (SPDT) switch or pushbutton, a pair of NAND gates, and two resistors, as shown in Figure 3-4. As soon as the movable contact in the switch touches a NAND gate input, the circuit changes its state and remains in that state until the movable contact touches the other fixed switch contact.

Figure 3-4

Figure 3-4

Two NAND gates form a switch-debounce circuit that offers complementary outputs, Q and Q*.

Because the debounce circuit produces two complementary outputs, one from each NAND gate, you can choose whether you want a logic 1 or a logic 0 to represent the normal output of the switch. Remember, the NAND gate circuit and any circuit it connects to, such as an input port, must share a common ground.

Circuit isolation

Although some sensors, instruments, and other devices produce TTL-compatible signals that can directly connect to an input port, many devices do not. You can convert these non-TTL-compatible signals into TTL levels, or you can buy input-port cards that provide the proper “translation” circuits. The easiest and most flexible translation involves using optical isolators, and many input-port boards and modules include these devices. At an input port, the phototransistor connects to the input port’s TTL inputs as shown in Figure 3-5. An external circuit powers the light-emitting diode (LED).

Figure 3-5

Figure 3-5

An optical isolator IC electrically isolates an external circuit from the signals at an input port. This example shows only one bit, and for clarity, the input port shows only bits for D7–D4.

Because the LED offers no direct electrical connection to the input port, it isolates the port (and the computer it connects to) from external devices. Thus, an external circuit can power the LED without regard to specific TTL levels and without a ground in common with the port and the computer. But the external circuit must supply a voltage and current within a specified range to properly operate the LED. Specifications that accompany an optically isolated input-port board or module will help you determine the maximum voltage and current an LED can accept. Because LEDs operate based on current flow, they are less susceptible to noise than standard TTL-compatible inputs.

Some optical isolators provide two “head-to-tail” LEDs in parallel. This arrangement lets you use low-voltage AC or DC to power the LEDs, regardless of the direction of current flow. Remember, LEDs act as diodes, so they allow current flow in only one direction. When an optical isolator requires a signal with a specific polarity (current-flow direction), the power source must match the required polarity.

CAUTION: DO NOT use small AC optical isolators to detect high-voltage signals, such as those in power-line (120V AC or 220V AC) circuits. Special devices can monitor signals at these voltages. Prior to use, always check suppliers’ specifications for LED voltage and current limits.

If you have difficulty getting an optically isolated input port to operate properly:

  • Ensure the circuit properly matches the polarity of the external circuitry and the polarity of the optical isolator.
  • Ensure you have a complete circuit to drive the LED.
  • Ensure the LED-drive circuit will deliver more than the minimum voltage and current needed to turn the LED on.

Current sinks and sources

Not all devices that connect to a computer provide TTL signals or mechanical switch contacts. A sensor may offer an output labeled as a current sink or current source, terms that may confuse users and lead to nonworking interface circuits. To further confuse the issue, some manufacturers use the term NPN sensor for a current sink, and PNP sensor for a current source. The designations NPN and PNP simply refer to the type of transistor the sensor uses as its on-off switch. The circuits in Figure 3-6 show how NPN and PNP sensor outputs can control optically isolated input ports and TTL-compatible inputs.

Figure 3-6

Figure 3-6

Sensors that furnish NPN or PNP transistors on their outputs can power optical isolators. The bottom diagram shows a circuit in which designers supplied an external power supply.

This nomenclature can get confusing, so here’s a way to keep the PNP and NPN designations straight. Think of the first letter in each abbreviation. A PNP device supplies power from the most Positive side of a circuit. On the other hand, an NPN device sinks current to the most Negative side of a circuit. NPN and PNP sensors operate only with DC power.

If you have a sensor that supplies a current-sink, or NPN output, the sensor provides an on-off switch to ground. An external circuit –
sometimes included by the sensor manufacturer — provides the current and voltage to drive an LED in an optical isolator. So, you can choose just about any available power source, say, 5V, 12V, 24V, and so on, depending on what power sources your system already includes. Or you can add an external power supply. The manufacturer of the optically isolated input port should include information about the minimum voltage and current specifications for optical isolators.

A sensor that provides a current source, or PNP, output usually provides a connection to a power supply. Unfortunately, you may not have a choice in the selection of the voltage this current source operates from. So, you must ensure that the sensor’s current capability and output voltage are within the limits specified by the input-port supplier.

LED considerations

All LED circuits require a resistor that controls the current through the LED. Many manufacturers of optically isolated input-port boards and modules include a resistor for each input-port bit. Some boards and modules will require an external user-supplied resistor. Check the manufacturer’s specifications for details. Also, always ensure the voltage you plan to use at an input port meet the board manufacturer’s specifications.

Whether a board provides a resistor or requires an external one, simple calculations will determine the needed resistance value for a given voltage. Here’s an example for the Sealevel Systems M240 Optically Isolated Input Adapter. Each of the eight inputs on the board has the same specifications:

Turn-on current (minimum): 3 mA (0.003 A)
Diode voltage drop: 1.1 V
Maximum resistor power: 1 W

turn-on voltage = (diode voltage drop) +  [(turn-on current) * (resistance)]

So, if a sensor provides a 12V DC source, calculate the needed resistance for a 3 mA current flow:

(turn-on voltage) - (diode voltage drop) = Resistance
(Turn-on current)
12 - 1.1 = 3633 ohms

The M240 board provides a built-in 3300W (1W) resistor that will work fine. (To ensure operation of the isolator, you can increase the current slightly. The optical coupler on the M240 board, for example, can handle a maximum LED drive current of up to 50 mA.)

Monitor high voltages
The small optical isolators furnished at input ports on commercial modules or boards will work with many types of sensors and data sources. But some applications require input ports that can monitor high voltages such as those that control pumps, solenoids, valves, and other devices. In this type of situation, designers rely on commercial plug-in modules specifically designed to offer optical isolation and to operate with high-voltage AC and DC signals.

Manufacturers of the output modules described in Chapter 2 also supply input modules that produce a TTL-compatible signal that indicates the presence or absence of a voltage on a corresponding input. LEDs on the modules indicate the state of each input.

The 1781-IA5Q module from Western Reserve Controls ( or the IAC5Q module from Opto 22 ( provides four circuits that monitor AC or DC signals between 90V and 140V. A module includes two pairs of input circuits and the two circuits in a pair share a common connection. If you need to monitor two devices that operate from the same 110V AC power source, and the devices share a common ground, the diagram in Figure 3-7 illustrates typical connections.

Figure 3-7

Figure 3-7

An optically isolated input module provides two pairs of separate circuits (right and left). Do not mix AC and DC signals in one section of a module, and do not mix signals that do not share a common connection.

But within a pair of inputs, you cannot mix circuits that do not share a common ground. Also DO NOT mix AC and DC signals in a pair. Separate pairs of circuits can handle AC and DC signals within a module, though. The sensing circuits in these two models produce a logic 0 when they detect a voltage within the manufacturer’s specified limits.

CAUTION: Always use an input module in parallel with a device; NEVER place a module’s inputs in series with the device. A module detects voltage across a device, not current passing through it. See Figure 3-8 for the types of connections you must avoid!

Figure 3-8

Figure 3-8

DO NOT connect an optically isolated input module in series with devices you want to monitor. The input modules operate in parallel with
devices, NOT in series.

If you wish to monitor lower DC voltages, devices such as the WRC 1781-IB5Q and 1781-IT5Q modules will handle inputs between 3.3V and 32V DC. You can use these types of modules to monitor DC voltages such as those found in power supplies or computer circuits. A DC-input module could alert a host computer to the lack of power in a subassembly, or it could let a computer know if a DC motor has power applied to it.

Like its sibling AC-DC input module, each DC input module includes two pairs of sensing circuits, and each pair shares a common signal. The WRC 1781-IB5Q module uses a common +DC input, and the WRC 1781-IT5Q module uses a common -DC input.

CAUTION: Although isolation modules often come with built-in LEDS that indicate the state of an input, not all modules clearly label each LED and the circuit associated with it.  The diagram shown previously in Figure 2-12 notes the proper relationship for the quad-circuit modules in the Opto 22 “Quad Pak” family or in the WRC 1781 Quad I/O line of AC-input and DC-input monitoring modules.

CAUTION: Manufacturers of modules and I/O boards may designate the inputs as “channels” 1—8, although the actual bit designations for the input-port bits usually carry the labels 0—7 (for data bits D0 through D7). Bit 0 represents the right-most of least-significant bit (LSB), and bit 7 represents the left-most or most-significant bit (MSB). Table 1 in Chapter 2 shows the relationships between the inputs, the bits, and the binary weights for each bit. To check the inputs at positions 8, 5, and 3 in a positive-logic system, for example, requires checking for a logic 1 at bits D7, D4, and D2.

Sense bits with software
External devices connected to a computer’s input ports have little value until programs can obtain data from them. As noted earlier, a simple software command such as:

portdata = inportb(input_port_number)

will obtain a byte of data from a selected input port. But because that data may include bits from a variety of devices, such as limit switches, fluid-level sensors, motor monitors, and so on, the software must select only the bits needed to make a decision. You can operate on individual bits by using the same sort of bit-wise logic you learned about in Chapter 2.

To illustrate how to obtain information from an input