get any
Free ebooks from Code With OJ
I thought it would be fun to start this book with a story, so please bear with me. In January of 1880, Thomas Edison filed a patent for a new type of device that created light by passing an electric current through a carbon- coated filament contained in a sealed glass tube. In other words, Edison invented the light bulb. (Students of history will tell you that Edison didn’t really invent the light bulb; he just improved on previous ideas. But that’s not the point of the story.)
Edison’s light bulb patent was approved, but he still had a lot of work to do before he could begin manufacturing a commercially viable light bulb. The biggest problem with his design was that the lamps dimmed the more you used them. This was because when the carbon-coated filament inside the bulb got hot, it shed little particles of carbon, which stuck to the inside of the glass. These particles resulted in a black coating on the inside of the bulb, which obstructed the light.
Edison and his team of engineers tried desperately to discover a way to pre- vent this shedding of carbon. One day, someone on his team noticed that the black carbon came off of just one end of the filament, not both ends. The team thought that maybe some type of electric charge was coming out of the filament. To test this theory, they introduced a third wire into the lamp to see if it could catch some of this electric charge.
Before you can understand even the simplest concepts of electronics, you must first understand what electricity is. After all, the whole purpose of elec- tronics is to get electricity to do useful and interesting things.
The concept of electricity is both familiar and mysterious. We all know what electricity is, or at least have a rough idea, based on practical experience. In particular, consider these points:
The 4011 Quad Two-Input NAND Gate is a popular CMOS logic gate IC. As its name suggests, this IC contains four two-input NAND gates. The pinouts for this IC are shown in Figure 3-10, earlier in this chapter, along with the pin- outs for several other quad two-input gate chips. Unlike most of the components used in this book, a 4011 IC isn’t available at your local RadioShack. Thus, you’ll have to find a local electronics store that carries parts such as CMOS logic chips, or you’ll have to order them from an online source. I list several reputable sources in Book I, Chapter 3.
In Book VI, Chapter 2, you learned that NAND gates (along with NOR gates) are universal gates, which means that you can construct any other type of gate by using nothing but NAND gates combined in various ways. Projects 3-4 through 3-7 walk you step by step through the process of building various types of gate circuits by using only NAND gates. Project 3-4 uses just one of the NAND gates in a 4011. The two inputs of the NAND gate are connected to pushbuttons, and the output is connected to an LED. When you build this project, you’ll be able to visualize the operation of a NAND gate: The LED will be on unless you press both buttons. Figure 3-11 shows Project 3-4 assembled on a solderless breadboard. This figure should give you a good idea of how to connect the components to the breadboard. The project description itself provides detailed instructions. The circuits for projects 3-5, 3-6, and 3-7 are similar enough in appearance that you can use Figure 3-11 as a guide for the overall appearance of your finished projects. Project 3-5 uses two of the NAND gates on the 4011 to create an AND gate. Because a NAND gate is nothing more than an AND gate whose output is inverted, you can create an AND gate from a NAND gate by inverting the NAND gate’s output. This inversion works because of one of the fundamental rules of logic: If you invert a value twice, you get the original value. Thus, if you invert an AND gate once, you get a NAND gate; if you invert it again, you’re back to an AND gate. As luck would have it, you can easily turn a NAND gate into a single-input inverter (that is, a NOT gate) by connecting the single input to both inputs of the NAND gate. This connection causes the two inputs to always be the same: Either both are HIGH, or both are LOW. In a NAND gate, if both inputs are HIGH, the output is LOW, and if both inputs are LOW, the output is HIGH. Thus, wiring the inputs of a NAND gate together has the effect of inverting the input. In Project 3-6, you see how to create an OR gate by using three NAND gates. Book VI, Chapter 2 shows that a NAND gate is the same as an OR gate whose inputs have been inverted. Thus, to create an OR gate by using NAND gates, you invert the two inputs with NAND gates configured as inverters (that is, with their inputs wired together). The output from these inverters is sent to the inputs of the third NAND gate. The final project in this chapter, Project 3-7, uses all four of the NAND gates on the 4011 chip to create a NOR gate. A NOR gate is nothing more than an OR gate whose output has been inverted. Thus, you first create an OR gate by using the technique in Project 3-6; then you configure the fourth NAND gate on the 4011 chip as an inverter to invert the output of the OR gate. Because these projects build on one another, I recommend that you don’t tear down your breadboard after completing each project. Instead, you can use each assembled project as a starting point for the next one. If you choose to build the projects in this way, you can just scan the steps to see what resistors and jumper wires need to be moved for each project. (The 4011 IC itself, the LED, and the two switches are in the same locations for all four projects.)
Until a few years ago, all computers came equipped with a parallel port, which was used mostly to connect to a printer. Today, most printers con- nect to computers via USB (Universal Serial Bus) ports. USB has many advantages over parallel port, the most significant being faster data transfer and smaller cables. The lowly parallel port has one advantage over USB ports, however: It makes it easy to create your own circuits that interface directly with the port. These circuits can control low-current devices such as LEDs, or they can activate transistors or even mechanical relays that in turn can activate high- current devices such as motors, incandescent lamps, or sound systems. After you’ve created the circuits to connect to a parallel port, it’s a simple matter to create a software program on the computer that sends data to the parallel port. When you run this program, your circuit can detect the data sent to the parallel port to control such things as LEDs or other low-power circuits.
A standard parallel port has eight data pins, which are essentially TTL logic outputs, with +5 V HIGH representing 1 and 0 V LOW representing 0. In fact, the first parallel printer ports designed for the IBM PC back in 1980 actually used 7400-series logic chips. Because of the TTL logic levels used by the parallel port, it’s a simple matter to create logic circuits that interface with the output from a parallel port. And after you build a circuit that connects to a parallel port, it’s a relatively simple matter to use software on the computer to send data to the parallel port. Then your circuit can respond to the data you send. Unfortunately, few computers today come with a built-in parallel port. Before you waste your time building circuits to interface with a parallel port, you need to find a computer that has one. Your best bet is to scavenge for an old laptop computer. Almost any laptop computer 10 years old or so will do the job, because most computers that age have a parallel port. If you can’t find a computer with a parallel port, you can buy an inexpensive parallel-port card that adds a parallel port to any computer. Online retailers such as Newegg (www.newegg.com) sell them for less than $15 apiece. In fact, it’s a good idea to use an add-on parallel port card even if your com- puter has a built-in parallel port. That’s because if you incorrectly wire the circuit that interfaces with the parallel port, you risk damaging the comput- er’s internal circuitry. Better to fry a $15 add-on card that your computer’s motherboard.
A parallel printer port uses a standard type of connector called a DB25 con- nector, which has 25 pins, each of which serves a different purpose in the port’s communication with a printer. Like most data connectors, DB25 connectors come in both male and female variants. The male DB25 connectors consist of 25 pins, and the female con- nectors have 25 holes. Figure 4-1 shows how the pins of a DB25 female and male connector are numbered. As you can see, the connector consists of two rows of pins. The top row has 13 pins, and the bottom row has 12. The pin in the top-right corner of the female connector is designated as pin 1; the pin at top left is pin 13; pin 14 is at bottom left; and pin 25 is at bottom right. Note that the pin numbers for a male connector are the mirror images of the numbers of the pins in the female connector. This arrangement is necessary so that when the male connector is plugged into the female connector the pins connect properly (e.g., pin 1 connects to pin 1, pin 2 connects to pin 2, and so on.)
For purposes of this chapter, the pins you’re most interested in are pins 2 through 9 — the eight data pins that are collectively called the data port. When the data port is connected to a printer, its eight pins are capable of sending 1 byte of data at a time to the printer. When the data port is con- nected to a circuit of your own design, its pins operate as eight separate logic outputs, which you can use as inputs to your own logic circuits. A parallel port also features four additional output pins called the control port, which you can also use for output. When the control port is connected to a printer, these pins are used to control the operation of the printer. One of them, called the strobe, indicates that a new byte of data is available on the data pins; when the strobe pin goes HIGH, the printer reads a byte of data from the data pins. Another control-port pin resets the printer. Finally, the five pins that make up the status port allow the printer to send information back to the computer. One of the status-port pins lets the printer tell the computer that it’s ready to receive data via the data port. Another pin lets the printer know that it has finished reading data from the data port. A third pin informs the computer that the printer is out of paper. The other status pins have similar functions. When the status port is connected to a circuit of your own design, its pins can be used to send information to the computer. You could use the status pins to tell the computer that a switch has been closed, that a light sensor detects light, or that a water-level sensor has reached a certain level. Unfortunately, the programming required to detect the presence of input on one of the status-port pins is a bit too complex for this chapter. If you’re an experienced C or C++ programmer, you’ll have no trouble writing programs to read the status-port pins. C and C++ programming are beyond the scope of this book, however, so I’ll focus for the rest of this chapter on sending output to the parallel-port data pins. As I’ve already mentioned, the output pins of a parallel port use a +5 V HIGH signal to represent 1 and 0 V to represent 0. The amount of current that each pin can source is relatively small — typically, around 10–12 mA. That cur- rent is enough to drive an LED, but for anything more demanding, you need a way to isolate the output load from the parallel port itself. To do that, you can use individual transistors or an IC designed specifically for this purpose. For details, see “Using Darlington Arrays to Drive High-Current Outputs,” later in this chapter.
Each of the eight data output pins provides 5 V DC, which can source about 10 mA or 12 mA — enough to drive an LED directly. Alternatively, you can connect the data output to the base of a switching transistor, which allows you to control circuits that require more current. Figure 4-2 shows these two methods for connecting output devices to a parallel-port data pin. The first circuit in this figure drives an LED directly. A current-limiting resistor is required to prevent the LED from pulling too much current and damaging the LED, the parallel port itself, or both. The second circuit in Figure 4-2 shows how to use a transistor to switch a circuit by using the output from a parallel-port data pin. As you can see, the data pin is connected through a resistor to the transistor’s base. When the data pin goes high, the transistor is turned on, allowing current to flow through the collector-emitter circuit. When you build Project 4-1 later in this chapter, you’ll see an example of a circuit that uses the first method to connect to parallel-port output. This project connects eight LEDs to the eight data pins. With the cable to connect this circuit to a computer’s parallel port and the right software in place on the computer, you can write simple scripts that will flash the LEDs on and off in just about any sequence you desire.
To build your own circuits that interface with a parallel port, you need to use a DB25 connector that attaches to your circuit. The standard parallel port on a computer uses a DB25 female connector, which means that you need to provide a DB25 male connector for your circuits. You have several ways to fashion a DB25 connector for your circuits. The simplest way is to find an old parallel printer cable, cut it a few inches past the male end, strip back the insulation, and separate the 25 wires that run inside the cable. Strip the ends of each of these wires and then use your ohmmeter to match up wire with the individual pins on the connector. Keep good notes so that you’ll know which wires to use for your circuits. Alternatively, you can purchase a male DB25 connector and solder wires to each of the pins. Figure 4-3 shows a connector that I made to use along with Project 4-1 and Project 4-2, which appear later in this chapter. I soldered eight red wires to pins 2 through 9 (the data pins) and one black wire to pin 25 (one of the ground pins). If you plan to complete Project 4-1 or Project 4-2, you need to assemble a similar cable yourself. All you need are a male DB25 connector, some 18-gauge solid wire, some solder, a soldering iron, and a vise or third-hand tool to hold the connector while you solder. You may also want to get some magnifying goggles. Although they’ll make you look like Christopher Lloyd in Back to the Future, they make the job much easier by helping you see the project more closely.
Doug Lowe