Assignment #4 – Digital and Analog Conversion Assignment #4 – Digital and Analog Conversion 1.1 Problem Assignment 1. Construct a digital to analog converter and compare the output to your theoretical calculations. 2. Construct an analog to digital converter and use it to measure the position of a potentiometer. 1.2 Assignment Goals At the end of this assignment you should be able to: 1. Apply basic circuit analysis to understand how a digital to analog converter works. 2. Design and construct a digital to analog converter using a network of resistors. 3. Demonstrate knowledge of a digital to analog converter and operational amplifiers. 4. Design and construct an analog to digital converter using your digital to analog converter and an operational amplifier. 1.3 Digital vs. Analog As you have seen in the last two exercises, the BasicX is an example of a digital device. It is only capable of two different output states, on or off (zero or five volts). Computers also use digital logic, so all of the inputs to a computer are converted to a series of on or off signals. (This is why computer memory is based on binary logic.) The BasicX being a digital device leaves us with a problem. What if we want an output of two volts or three and a half volts? Also, what if we want to have an input to the BasicX that is something other than five volts? In this assignment we will explore how to convert digital signals to analog signals so that we can use the BasicX to output a voltage other than zero or five volts. We will also examine how to convert analog signals to digital so that the BasicX can read signals from analog devices. 1.4 Calculating the Output from the Digital to Analog Converter For project #1, we will construct an eight-bit digital to analog converter (D/A converter) using a network of resistors (see figure 1). Before building the circuit, examine the circuit diagram and use Kirkoff’s Laws to calculate the output voltage for all possible combinations of digital inputs. (The inputs to the D/A converter are actually the outputs from the three pins we are using from the Course Material for MECN 3200 – Mechatronics 1 Assignment #4 – Digital and Analog Conversion BasicX, so each can have a value of zero or five volts.) It may be helpful to use the simplifications for resistors in series and parallel when some of the inputs are set to zero. Write the values you calculate in the table given in figure 2 under “Calculated Output Voltages”. In the table, a value of 1 is equal to +5V and a value of 0 is equal to 0V. Notice that you can get eight different combinations by turning different pins on or off. This is why it is called an eight-bit D/A converter. You will find and example of one of the calculations following the figures. Output Voltage 2R R R 2R Pin 1 Output 2R + - Pin 2 Output 2R 2R + - Pin 3 Output + - No wire is necessary for these connections. They are internal in the BasicX. Figure 1: Resistor Network for D/A Converter Output Number 1 2 3 4 5 6 7 8 Pin 1 Pin 2 Pin 3 0 1 0 1 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 1 1 1 1 Calculated Output Voltage Actual Output Voltage Figure 2: Table for Output Voltages Course Material for MECN 3200 – Mechatronics 2 Assignment #4 – Digital and Analog Conversion Example Calculation for “011” Before beginning the example calculation, we need to understand two important simplifications we can make for resistors in parallel and resistors in series. These two relationships are given below. Resistors in Parallel R1 1 R2 1 R2 1 R1 Resistors in Series R1 R2 R2 R1 Now we can solve for the output voltage as follows: 1) First, simplify the resistors on the left end using the equation for resistors in parallel as shown below. Output Voltage R R 2R 1 R 1 2R 2R 2R 1 2R + - Course Material for MECN 3200 – Mechatronics +5V + - 3 Assignment #4 – Digital and Analog Conversion 2) Next, once again simplify the resistors on the left end this time using the equation for resistors in series as shown below. Output Voltage R+R=2R R 2R 2R + - 3) 2R + - +5V We can now use Kirkoff’s Current Law to set the sum of the currents going into node A and node B equal to zero. Output Voltage i1 2R i2 i3 A 2R +5V i4 R i6 B i5 2R 2R + - +5V + - Node A Node B i1 i2 i3 0 i4 i5 i6 0 0 V A 5 V A VB V A 0 2R 2R R V A VB 5 VB 0 VB 0 R 2R 2R VA 5 VA 2VB 2VA 0 2VA 2VB 5 VB VB 0 Solving the two equations simultaneously, we can solve for the voltage at the output point. V A 2.5 VB 2.5 OutputVoltage Course Material for MECN 3200 – Mechatronics 4 Assignment #4 – Digital and Analog Conversion 1.5 Constructing the Digital to Analog Converter Now construct the D/A converter as shown in figure 1. Notice the actual value of each resistor is not important, it is only important that you have a two to one ratio in the resistance values. Also, make sure that you connect the circuit to ground as shown in the lower right hand corner. You do not have to make a physical connection with a wire from the pins of the BasicX to ground because the output voltage from the pins is already measured referenced to ground (think of it as an internal connection in the BasicX). After the circuit is constructed, write a short program for the BasicX to turn on and off the output from the three pins that you used in the circuit. Then, use your multimeter to verify that each value you calculated is indeed the correct output voltage of the circuit. To measure the voltage, you should connect the black lead from your multimeter to ground and connect the right lead to the place labeled “output voltage” on the circuit diagram. You can write the voltages you measure in the table in the column labeled “Actual Output Voltage” to compare them to the calculated values. Notice that using three pins we can get eight different outputs. The eight possible outputs are the reason we call this an eight-bit D/A converter. If we used four different output pins, we would have sixteen different outputs, thus creating a sixteen-bit D/A converter. It follows that five pins would give us a thirty-two-bit D/A converter. 1.6 Designing an Analog to Digital Converter We should now have a good understanding of the difference between analog and digital, and we have discovered how to take the digital output from the BasicX and convert it to an analog voltage. The next step is to determine how to read an analog device using the digitally based BasicX, so we must create an analog to digital converter (A/D converter). 1.6.1 The Potentiometer as a Voltage Divider The A/D converter will be used to determine the output from a potentiometer that is used in a voltage divider circuit. A potentiometer, or simply a “pot, is an analog device that changes resistance as a knob, wheel, or screw slot is rotated. A typical pot has three leads. The resistance between the outside terminals is the maximum (total) resistance of the pot (e.g. 100 K) and always remains the same. The resistance between the middle terminal and either of the outside Course Material for MECN 3200 – Mechatronics 5 Assignment #4 – Digital and Analog Conversion terminals varies as the knob is turned. If you construct a circuit using a potentiometer that supplies positive five volts to one of the outside leads and connects the other outside lead to ground, the center lead will have an output of between zero and five volts depending on the position of the potentiometer knob (see figure 3). This is commonly referred to as a voltage divider. For project #2 we will construct a circuit with a potentiometer set up as a voltage divider and compare the output from the potentiometer to that of your digital to analog converter. The comparison will allow us to determine the position of the potentiometer using the digital BasicX even though the potentiometer is an analog device. +5V R1 Rpot 0V Vout 5V R2 Rpot= R1+ R2 a) Physical Appearance b) Voltage Divider Circuit b) Equivalent Circuit Figure 3: The potentiometer and Voltage Divider Circuit 1.6.2 The Operational Amplifier To construct our A/D converter will need to use an operational amplifier (op amp) to multiply the output of our D/A converter and compare it to the output from the potentiometer. First we must understand how an op amp works, how to construct a circuit that can be used for voltage comparison. As the word amplifier suggests, the function of an operational amplifier is to amplify a voltage. However, the operational amplifier does much more than that. It also functions as a buffer and as a cascader—which are two functions that enable simple circuits to be assembled into complex circuits to create higher level functions which are called operations hence the name operational amplifier. For project #2 we will be using the amplification function of the op amp to Course Material for MECN 3200 – Mechatronics 6 Assignment #4 – Digital and Analog Conversion multiply the output of the D/A converter. Recall that from project #1 the output range of the D/A converter is from zero to a little over two volts. If we are going to compare the output from the D/A converter with the potentiometer, we are going to need to multiply the output of the D/A converter. Then, to compare the voltage output from the potentiometer to that of our D/A converter we will use another op amp as a comparator. If the D/A converter is outputting a higher voltage, the op amp will amplify the voltage difference to positive five volts so that it can be read by the BasicX. If the potentiometer is outputting a higher voltage, the op amp will output 0 volts. Single op amps have five terminals that are important. The voltage that is amplified is the difference between the voltage at the ‘+’ terminal vp and the voltage at the ‘-’ terminal vn, as shown in figure 4. The amplified voltage is the output voltage vo. Unlike the resistor and capacitor, which are both “passive” (unpowered) devices, the op amp is an “active” device. Indeed, the op amp needs a voltage supply for the amplification. The vs+ and the vs- terminals are the positive and negative supply voltages, respectively. There are many different designs of op amps that will provide a variety of output characteristics. A good general-purpose op amp that is widely used today is the 741 op amp (shown in figure 4). markings to indicate pin 1 vs+ offset adj 1 vn 2 8 unused 7 vs+ vn vo vp vp 3 vs- 4 6 vo 5 offset adj a) 741 op amp chip configuration vsInput Supply Output b) The op amp symbol Figure 4: The 741 Op Amp Schematic and Chip Configuration. 1.6.3 The Op Amp as a Multiplier The first thing we will need to do is to multiply the output from our D/A converter to match the output range of our potentiometer voltage divider. To achieve Course Material for MECN 3200 – Mechatronics 7 Assignment #4 – Digital and Analog Conversion multiplication using an op amp, you can use the circuit diagram shown in figure 5. Notice that you must choose the values of R1 and R2 carefully to get the desired result from your multiplication. R1 R2 vout vin R vout 1 2 vin R1 Figure 5: Using an Op Amp to Multiply Voltage 1.6.4 Using an Op Amp as a Comparator In order to use the op amp as a comparator, we will be constructing a circuit that takes advantage of the “open-loop gain” of the op amp (as opposed to a “feedback” circuit that we used to do multiplication). The equation that governs the output of the op amp as shown is as follows in figure 6: vs+ vn vo vp vsInput Supply Output v o A (v p v n ) Figure 6: Using an Op Amp as a Comparator Course Material for MECN 3200 – Mechatronics 8 Assignment #4 – Digital and Analog Conversion where A is usually on the order of about 50,000. However, the output voltage (vo) cannot be larger than the supply voltage (vs+ or vs-). You can assume from the equation that the output voltage (vo) is usually equal to either the positive supply voltage (vs+) or the negative supply voltage (vs-) depending on weather vn or vp is larger. For example, suppose the positive input voltage (vp) is 3.000 volts and the negative input voltage (vn) is 2.999 volts. Then, according to the equation, the output voltage should be positive 50 volts [vo = 50,000 (3.000 – 2.999)]. However, to get the op amp to output positive 50 volts, you would need a positive supply voltage (vs+) of 50 volts. Suppose you have a positive supply voltage of 10 volts. Then, the output voltage would be 10 volts, not the 50 volts that is calculated using the equation. In this case, we say that the op amp is saturated. If the situation is reversed and you have a positive input voltage (vp) of 2.999 volts and a negative input voltage (vn) of 3.000 volts, then your output voltage would be negative 50 volts or equal to the negative supply voltage (vs-) if it is smaller. The saturation condition of an op amp can be seen symbolically in figure 7. vo vs+ A(vp - vn) vsSaturated Region Linear Region Saturated Region Figure 7: The Operating Regions of the Op Amp While the 741 op amp is a very good general purpose op amp, but there is a small problem that will not allow us to use it for project #2. Theoretically speaking, op amps are able to output up to the value of the supply voltage. However, there are a few losses in the circuitry of the 741 and the maximum output voltage is actually about 1.4 volts lower than vs+ and the minimum output voltage is about 1.4 volts higher than vs-. This would cause a problem for us because we want the negative output to be equal to exactly zero and the positive output to be exactly five volts so that we can use them as inputs to our digitally based BasicX. (If we supply the 741 with a negative supply voltage of zero volts, the smallest voltage output we would get is about 1.4 volts.) To overcome this problem, we will use the LMC660 quad op amp because it has the characteristic of rail-to-rail output. Course Material for MECN 3200 – Mechatronics 9 Assignment #4 – Digital and Analog Conversion Rail-to-rail output means that you can achieve an output voltage equal to the supply voltage. Although it is possible to get an output voltage equal to the supply voltage, there is still one small problem with using the LMC660. What is called the “common mode range” of the LMC660 is from zero volts to about 2.5 volts below your positive supply voltage. The common mode range is the range in which the op amp will perform as you expect it to (outside that range you can not be sure how it will act). To make sure that the LMC660 is giving us the output that we desire, we will need to keep it in the common mode range. Therefore, we will supply the op amp with a negative supply voltage of zero volts and a positive supply voltage that is greater than 7.5 volts (in our case a 9 volt battery will do nicely) to insure that we are in the common mode range. When specifying an op amp for a project you are working on, you will need to check the datasheet to determine its characteristics (common mode range, rail-torail output, etc.). You will also need the circuit diagram to determine the pin configuration. To find the circuit diagram for the LMC660, and to read the specifications, you will need to go to the world wide web. The LMC660 is manufactured by National Semiconductor, so you can go directly to their web page at www.national.com. Then, enter LMC660 in the search window and click on “datasheet” to get the chip configuration. Another way that you can get datasheets for integrated circuits, if you do not know the manufacturer, is to simply use a general search engine with the number of the integrated circuit. Try going to www.google.com and entering LMC660. The first link that it finds will take you directly to the datasheet. You can download a copy of the datasheet, or just open it as an html document. Notice that there are actually 4 op amps available on the LMC660 (hence the name “quad op amp”). We will only need to use two of the op amps available. 1.6.5 Constructing the Analog to Digital Converter We now have all the pieces and are ready to construct our A/D converter. The assignment for problem #2 is to construct a circuit that will measure the position of a potentiometer. First, we will take the output from the D/A converter that you constructed in problem #1 and multiply it using an op amp to get output from zero to five volts. Then, we will compare that signal to the output from the potentiometer (configured as a voltage divider with an output between zero and five volts) using another op amp. We want to determine which one of seven possible regions the output from the potentiometer falls into. The seven possible regions are defined by the output of the D/A converter as shown in figure 8. After you have determined which region the output of the potentiometer falls into, you Course Material for MECN 3200 – Mechatronics 10 Assignment #4 – Digital and Analog Conversion will display the number of the region using the seven-segment display from assignment #3. 0V 0.7 V 1 Output #1 (0 0 0) 1.4 V 2 Output #2 (0 0 1) 2.1 V 3 Output #3 (0 1 0) 2.9 V 4 Output #4 (0 1 1) 3.6 V 5 Output #5 (1 0 0) 4.3 V 6 Output #6 (1 0 1) 5V 7 Output #7 (1 1 0) Output #8 (1 1 1) Figure 8: Possible Output Regions for the Potentiometer The circuit diagram for the circuit you will construct is given in figure 9. Remember from our discussion about the LMC660 op amp that you must provide it with a positive supply voltage that is greater than 7.5 volts. A standard 9-volt battery will work nicely to supply the positive supply voltage. To connect the battery correctly, you must connect the negative terminal of the battery to the ground on your breadboard and the positive terminal of the battery to pin 4 of the op amp. Then, connect pin 11 of the op amp to the ground on your breadboard. Notice the use of the diodes in figure 9. Because we are using nine volts for the positive supply voltage of the op amp, the op amp is capable of outputting nine volts. However, if we connect nine volts to one of the input pins of the BasicX we could damage the processor. The diodes are configured in such a way that they limit the voltage entering the BasicX to a maximum of five volts. When constructing the circuit, you must place the diodes so that white stripe around the diode is on the side of the diode where current will be leaving. This is shown in figure 9. Course Material for MECN 3200 – Mechatronics 11 Assignment #4 – Digital and Analog Conversion +5V +5V 100k 1N4007 Pot +9V R2 Diode Comparator 10k R1 +9V 0V Multiplier D/A Converter 1N4007 Diode 0V 100k Figure 9: Circuit Diagram for Analog to Digital Converter +5V White Stripe 1N4007 +9V Diode Comparator 10k 0V 1N4007 Diode White Stripe 100k Figure 10: Orientation of Diodes in A/D converter Course Material for MECN 3200 – Mechatronics 12 Assignment #4 – Digital and Analog Conversion After you have constructed the circuit, you must program the BasicX to determine which region the output from the potentiometer falls into and display it using the seven-segment display. To keep the program as simple and efficient as possible, you can use an algorithm that used the method of bisection. First, set the D/A converter to output #4 (we are bisecting the total distance). Then, check to see if the output from the potentiometer is higher or lower. (If the output from the potentiometer is higher, the comparator will output 0 volts. If the output is lower, the comparator will output 5 volts.) Next, write one subroutine if the output is higher and a separate subroutine if the output is lower. Each subroutine should bisect the distance left and again move to one subroutine if the potentiometer output is higher and another if it is lower. For example, the first subroutine for if the potentiometer is higher will next set the D/A converter to output #6 and move on to the next subroutine accordingly. A flowchart for the bisection algorithm can be seen below in figure 10. Once you have determined in which region the output of the potentiometer is in, display the number of the region on the seven-segment display using the subroutines that you developed in assignment #3. Set D/A converter to output #4. Is the pot output higher? Yes No Set D/A converter to output #6. Is the pot output higher? Yes No Set D/A converter to output #7. Is the pot output higher? Yes Pot is in region #7 Set D/A converter to output #2. Is the pot output higher? No Pot is in region #6 Yes Set D/A converter to output #5. Is the pot output higher? Yes Pot is in region #5 No Pot is in region #4 Set D/A converter to output #3. Is the pot output higher? Yes Pot is in region #3 No Pot is in region #1 No Pot is in region #2 Figure 10: Flowchart for Bisection Algorithm Course Material for MECN 3200 – Mechatronics 13