Lab 2: A/D, D/A, and Sampling Theorem Introduction The purpose of this lab is to explore the principles of analog-to-digital conversion, digital-to-analog conversion, and the sampling theorem. It will also serve as an introduction to the Q2 USB data acquisition system, which will be using for the remaining control labs. In-Lab Task #1: Introduction to Quanser Q2 USB Data Acquisition Device In our lab, we will use the Quanser Quarc Q2 USB data acquisition device to act as an interface between our computer and motor system. The Q2 board has two 16-bit count encoder inputs, two 12-bit analog inputs, two 12-bit analog outputs, and 8 digital I/O lines. It has an analog input/output range of 10V. Ground Bar Figure 1: Quanser Quarc Q2 USB Data Acquisition Board CAUTION: The Q2 boards are extremely sensitive to electrostatic discharge (ESD) and will be irrevocably damaged if subject to static shock. Consequently it is of critical importance you ground yourself before handling the boards. You can do this by touching the exposed metal ground bar on the board itself, as pictured above. RCA ends should be used to cover any unused analog I/O ports. When wiring devices to the Q2 board, make sure the Q2 board is the last thing you connect to. When disassembling your wiring, unplug RCA cables from Q2 boards first. Exposed wire ends that are connected to the analog I/O ports of the board are essentially direct lines for static electricity to enter the board. Task #2: Setting Up Simulink model to Work with the Quarc Q2 Board In order for MATLAB to communicate with the Q2 board, you need to take a few extra steps when setting up your Simulink model. These steps must be performed each time a Simulink model is built, both in this lab and future labs: 1. Create a new Simulink model and open the Simulink library browser. 2. Specify that you are using a Quarc Q2 board. This is done using the special Simulink block HIL Initialize. Within the Simulink library, navigate to QUARC Targets > Data Acquisition > Generic > Configuration, as pictured below, and drag-and-drop the HIL Initialize block into your Simulink model. Figure 2: Location of HIL Initialize Block in Simulink Library.
3. In the Simulink model, double-click the HIL initialize block and change the Board type: to q2_usb in the drop-down menu. 4. Next, navigate to the Simulation menu on the Simulink model toolbar and click on Model Configuration Parameters, as pictured below: Figure 3: Location of Simulation Menu. 5. In this configuration menu (shown below), set your simulation time by changing the Stop time value. For this lab a time of 20 seconds will work. In future labs it will be advantageous to set this to inf so it runs indefinitely. Next set your Fixed-step size (a.k.a. fundamental sample time) to.001 second. Next, select your solver type. For this lab we will use the discrete solver. Figure 4: Model Configuration Parameters Menu 6. Change the simulation mode from Internal to External using the drop-down menu on the Simulink model toolbar, as pictured below: Figure 5: Location of Simulation mode dropdown menu. 7. Keep this Simulink model open as we will use it in Task #3. Remember to save your file. Task #3: Analog-to-digital (A/D) Conversion In this section the principles of analog-to-digital conversion will be explored. This is accomplished by building and using a Simulink model that measures an external DC signal through one of the Q2 s analog-to-digital inputs. The DC power supply located at your station will be used to generate the signal. The steps are as follows: 1. Locate/open the Simulink model created in Task 2. If you do not have it, simply repeat the steps in Task 2 to setup a new Simulink model. 2. When using a Q2 board, special Simulink blocks provided by Quanser must be used to create inputs, outputs, and in future labs, encoder readings. The location of these blocks within the
Simulink library is pictured in Figure 6. Figure 6: Location of Quarc-specific I/O, Encoder Blocks. 3. Using the HIL Read Analog block from this library location along with a Display block, create the following block diagram (the HIL Initialize block is cropped out here): 4. Double-click on your HIL Read Analog block and confirm the channel listed is 0 (this sets it to ADC input 0 as opposed to ADC input 1 on the board). 5. Press the build model button in the Simulink model toolbar. 6. With the Simulink model setup, the DC power supply must now be wired to one of the analog inputs on the Q2 board. Remember to ground yourself and make sure the Q2 board is the last thing you connect to. Use the following steps: Step 1: Connect the Banana-Alligator wires to the DC power supply output terminals Step 2: Connect the Banana-Alligator wires to the RCA-tinned cable as shown in the diagram below. Do not connect the RCA-tinned cable to the Q2 ADC0 input yet. Step 3: Turn on the DC power supply and set the voltage to 10V. Step 4: After grounding yourself, connect the RCA end of the RCA-tinned wire to ADC 0 on the Q2 board. Remember that the Q2 board is very ESD sensitive. RCA Tinned Wire Banana to Alligator Wires 7. Return to your Simulink model. Press the Connect to Target Button on the Simulink toolbar, as pictured below:
8. Now press the play button located immediately to the right of the Connect to Target button on the Simulink toolbar. This will begin the simulation. 9. You will now see the power supply voltage being displayed in your Simulink model s Display block. Record this value for your lab report. If you want to end the simulation early, press the stop button on the Simulink toolbar. 10. Change the power supply voltage to 6V. Run the Simulink model again and record the voltage observed for your lab report. 11. Repeat step 10 for a power supply voltage of 2.5V, again recording the voltage. 12. Set the power supply voltage to -10V. Do this by first setting your output voltage to 10V. Then turn the power supply current knob all the way to zero. This in turn reduces the voltage to zero. Now switch the positive and negative leads at the power supply. Return the current knob to its original position. Run your simulink model, recording the voltage observed. 13. Repeat step 12 for a power supply voltage of -1.5V and record the voltage. 14. Turn off your power supply. Disassemble your wiring. Remember to ground yourself first and disconnect the the Q2 board before anything else. Task #4: Digital-to-Analog (D/A) Conversion: In this section, Simulink will be used to create a DC signal that is outputted through one of the Q2 s DAC outputs. This signal will then be measured using the oscilloscope (and/or multimeter) at your workstation. The steps are as follows: 1. Build the following block diagram. You can save setup time by reusing your Simulink model from Task 3. Simply delete the HIL Read Analog and Display blocks and add the following (the HIL Initialize block is cropped out here): 2. Double-click the Constant block and set its constant value to -5V. 3. Double-click the HIL Write Analog block and verify it is set to channel 0. 4. Press the Build button 5. Wire the oscilloscope to the DAC 0 output on the Q2 board using the diagram below (Remember to ground yourself and connect the Q2 board last). 1 of the 3 oscilloscope probes in your workstation s oscilloscope pack will have adjustable attenuation via a red switch on the side. Make sure this switch is set to 10x. 6. After turning on your oscilloscope, set it up to measure the voltage on channel 1. Do this by first pressing the Channel 1 button to make sure it is displayed (channel 1 will be yellow on screen while channel 2 will be blue). Press the Measure button. Select Ch 1 using the corresponding selection button located just right of the screen. This will open a submenu. Using the rotary knob, navigate to Maximum and press the rotary knob to select. Now press
the Menu On/Off button, located just off the bottom right edge of the screen, twice to exit the submenu and menu. You will now see the maximum signal voltage displayed in the lower lefthand side of the screen. 7. Return to your Simulink model. Press the connect to target button, and then run the model. Record the voltage observed on the oscilloscope for your lab report. You made need to use the Autoset button on the oscilloscope to bring the signal within range of the screen. 8. Repeat the procedure above for constant voltage values of -10V, 0V, 5V, and 10V. Record the oscilloscope readings for each. 9. You can leave your wiring intact as DAC 0 will still need to be connected to Channel 1 for the next task. Task #5: Demonstration of Sampling Theorem In this section we will demonstrate the principles of the sampling theorem. This will be accomplished by generating an analog signal using the function generator at your workstation and sending this signal to one of the analog inputs of your Q2 board. This signal will then be sampled by Simulink. This sampled signal will then be sent to your oscilloscope to be evaluated via one of the outputs on your Q2 board. The steps are as follows: 1. Create the following Simulink model. Again, you can save setup time by reusing your previous Simulink model and deleting/adding blocks as needed. 2. Make sure the HIL Read Analog block is set to ADC channel 0, while HIL Write Analog is set to DAC channel 0. 3. Set the simulation time to inf so it will run indefinitely until the stop button in the toolbar is clicked. 4. Check that your simulation step-size is still.001 (corresponding to a sample rate of 1000Hz) 5. Click Build button on Simulink model toolbar 6. Wire the oscilloscope, function generator, and Q2 board together by following the steps and diagram below. Your oscilloscope should still be connected to DAC 0 from the previous task. Again, ground yourself first and always connect the Q2 board last. Step 1: Connect a probe to channel 2 on the oscilloscope and another to the output of the function generator Step 2: Connect both of these probes to a single RCA-tinned cable. Do not connect this cable to the Q2 board yet. Step 3: Turn on the oscilloscope and function generator Step 4: Set your function generator to produce a sine wave with a frequency of 50 Hz and an amplitude of 1.5V RMS. You can use the oscilloscope s Measure button to display this RMS amplitude while you adjust it on the function generator. Step 5: Now connect the remaining RCA-tinned cable to the Q2 ADC 0 input.
7. Return to your Simulink model, connect to target, and run. 8. Using the oscilloscope, estimate the frequency of the signal outputted by the Q2 board. Record it in Table 1. If you want, you can save a screen capture of the oscilloscope readout using the Tektronix software on your computer. 9. Repeat step 8 for the remaining frequencies in Table 1 Table 1: Observed Frequency vs. Input Frequency 10. Turn off the function generator and oscilloscope. Disassemble your wiring. Remember to ground yourself first and disconnect the RCA cables from the Q2 board before anything else. Lab Report Answer following questions: 1. What is the resolution of an analog-to-digital converter with a word length of 12 bits and an analogue signal input range of 100V? Show work. 2. Consider an 8-bit (including sign bit) A/D converter with a voltage range of +/-1V. What is the resolution of the A/D converter? 3. A sensor gives a maximum analog output of 5V. What word length (i.e. the number of bits) is required for an analog-to-digital converter if there is to be a resolution of 10mV? Show work. 4. Consider the analog signal: Xa(t) = 3cos(300t). Determine the minimum sampling avoid aliasing. If this signal is sampled at 300 Hz, what is the frequency of the signal reconstructed from the sampled data points? If this signal is sampled at 200 Hz, what is the frequency of the signal reconstructed from the sampled data points? 5. Attach the results you have gotten in task #2, task #3, and the actual vs.observed frequency table that you created in task #4.