The Digital I/O Handbook – Chapter 3
The Digital I/O Handbook
A Practical Guide to Industrial Input & Output Applications
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 computers 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 ports 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. Weve 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
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 ports 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
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
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
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 ports TTL inputs as shown in Figure 3-5. An external circuit powers the light-emitting diode (LED).
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 D7D4.
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
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 heres 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 sensors 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 manufacturers specifications for details. Also, always ensure the voltage you plan to use at an input port meet the board manufactu