200 likes | 350 Vues
Lab 5 EGR 262 – Fundamental Circuits Lab. EGR 262 Fundamental Circuits Lab Presentation for Lab #5 Pulse Width Modulation. Instructor: Paul Gordy Office: H-115 Phone: 822-7175 Email: PGordy@tcc.edu. Lab 5 EGR 262 – Fundamental Circuits Lab. v(t). V. t. 2T. 3T. T.
E N D
Lab 5 EGR 262 – Fundamental Circuits Lab EGR 262 Fundamental Circuits Lab Presentation for Lab #5 Pulse Width Modulation Instructor: Paul Gordy Office: H-115 Phone: 822-7175 Email: PGordy@tcc.edu
Lab 5 EGR 262 – Fundamental Circuits Lab v(t) V t 2T 3T T 0 TH Pulse Width Modulation A periodic waveform can be described by v(t) = v(t + T) for some positive value of T, called the period of the waveform. The pulse waveform below is a periodic waveform with period T. The frequency, f, of the waveform is 1/T. T is measured in seconds and f is measured in Hertz. A pulse-width modulated (PWM) signal is one where THcan vary. The duty cycle, D, of the waveform is defined below. D is usually expressed as a percentage. VAVG or VDC is the average or DC value of the waveform.
Lab 5 EGR 262 – Fundamental Circuits Lab Examples: Determine the period, frequency, duty cycle, and average value of each waveform below. v(t) T = ____________ F = ____________ D = ____________ VDC = ____________ 5V t (ms) 6 10 8 0 2 4 v(t) T = ____________ F = ____________ D = ____________ VDC = ____________ 5V t (us) 0 4 20 24 40 44 v(t) T = ____________ F = ____________ D = ____________ VDC = ____________ 5V t (ns) 65 80 145 160 205 0
Lab 5 EGR 262 – Fundamental Circuits Lab v(t) Motor turns fast V t v(t) Motor turns slow V t 80% power setting V t v(t) 20% power setting (defrost) V t Applications: Pulse-width modulated signals are used in many applications. A few are listed below. Many devices essentially respond to the average value of the waveform. Motor control. Motor speeds up or slows down as pulse width (or duty cycle) changes. Microwave Oven. The microwave is essentially turned on and off as the power setting is changed. v(t)
Lab 5 EGR 262 – Fundamental Circuits Lab v(t) Turn Left V t v(t) Turn Right V t Servo control. A servo is a combination motor & gear box where the motor position can turn 0 to 180 degrees (typically) as pulse width varies. Servos are used for steering RC cars. Digital-to-Analog Converter. In Lab 6 we will improve on the DAC built earlier using an R-2R ladder network by using the PWM signal to charge a capacitor to the desired analog voltage. v(t) Capacitor charges to larger analog voltage V t v(t) Capacitor charges to smaller analog voltage V t
Lab 5 EGR 262 – Fundamental Circuits Lab • Generating a PWM signal using the Arduino UNO • PWM signals can be generated using the analogWrite( ) command: • form: analogWrite(pin, value) • Available on digital outputs 3,5,6,9,10,11 (note the ~ symbol on the UNO below) • 0V to 5V square wave has duty cycle based on value • value varies from 0 (off) to 255 (on) • Uses set frequency of approximately 490 Hz • It is not necessary to use pinMode( ) to specify a pin as an output when it will be used with analogWrite( ) • PWM waveform will be maintained until the next occurrance of analogWrite( ), digitalWrite( ), or digitalRead( ). • The analog value is the average • value or DC value of the waveform
Lab 5 EGR 262 – Fundamental Circuits Lab Example: Create a PWM signal on D11 with a 25% duty cycle. Arduino UNO v(t) 5V D11 VDC = 1.255 V t (ms) 0V 0 0.51 2.04 2.55 4.08 4.62
Lab 5 EGR 262 – Fundamental Circuits Lab Example: (continued) The sketch on the previous slide was compiled and uploaded to an Arduino UNO in lab. The output on D11 was observed using an oscilloscope. The values shown are very close to the expected results. 5 V Ground (0V) for CH1 0 V VDC = 1.29 V T = 2.044 ms f = 489.234 Hz 1V/div so waveform is 0V to 5V
Lab 5 EGR 262 – Fundamental Circuits Lab Example: (continued) The duty cycle, D, can be measured on another oscilloscope screen using two cursors. The values shown are very close to the expected results. Cursor 1 Cursor 2 Delta = Cursor 2 – Cursor 1 = TH = 800.0 us Input to program: value = 64 D predicted by program: 25.1% D observed on oscilloscope: D = TH/T*100% D = 500us/2.044ms*100 D = 24.46% f = 489.234 Hz T = 1/f = 2.044 ms
Lab 5 EGR 262 – Fundamental Circuits Lab Suppose that we wanted the user to enter the value for a PWM signal (from 0 to 255)? It is useful to begin with a little background on reading and writing using serial communication. • Serial Communication using the Arduino UNO • Serial communication is used by the Arduino UNO to communicate with the computer and other devices. • Serial communication uses pins D0 (RX or Receive) and D1 (TX or Transmit). • Serial communication takes place at a rate of 9600 baud (bits per second). The following command should be included in setup( ): • Serial.begin(9600); • Serial communication takes place via the USB cable and requires no additional wiring. • The Arduino software has a built-in monitor for displaying information sent to or from the computer. • Serial communication is also used when you compile and upload a sketch to the Arduino UNO.
Lab 5 EGR 262 – Fundamental Circuits Lab Serial Communication (continued) Data is transmitted one byte (8 bits) at a time using serial communication. When a key is pressed on a keyboard, an 8-bit ASCII code is transmitted. The ASCII code for letter A is 010000012 = 4116 = 6510 If you enter the characters ABC on the keyboard, 3 bytes of information are transmitted to the Arduino UNO as illustrated below. C A B 01000011 01000010 01000001 USB cable
Lab 5 EGR 262 – Fundamental Circuits Lab • Serial Communication (continued) • Several useful functions that work with Serial are: • Serial.write(x); // writes character corresponding to the ASCII code so the argument should be from 0 to 255 • Serial.print(x); // displays the corresponding symbol(s) based on the ASCII value • Serial.println(x); // similar to print( ) but advances to the next line • Serial.print(“Text”); // displays the exact text shown in double quotes • Serial.write(“Text\n”); // same as the line above • \n and \t can be used within text for tabs and newlines, respectively • Some of these functions may not work as you would expect, so examples are shown on the following slide.
Lab 5 EGR 262 – Fundamental Circuits Lab Example using Serial.print( ) and Serial.write( ): • Notes: • Serial.write( ) only worked correctly when the argument was text or a value between 0 and 255. Using a floating point argument yields a compiler error. • Serial.print( ) worked correctly for text or numerical values, but rounds off floating point values to 2 digits after the decimal point.
Lab 5 EGR 262 – Fundamental Circuits Lab • Serial Communication (continued) • Additional useful functions that work with Serial are: • Serial.available( ); // Used to check for input values from keyboard • Serial.read( ); // used to read one byte of information. The function returns this value. • Serial.parseInt( ); // used to read an integer number (1 or more digits) • Serial.parseFloat( ); // used to read a floating-point number (1 or more digits) • Some of these functions may not work as you would expect, so examples are shown on the following slide.
Lab 5 EGR 262 – Fundamental Circuits Lab Example using Serial.available( ), read( ), write( ) and print( ): Note: When 3 was entered on the keyboard, the ASCII code (001100112 or 5110 ) was read. Enter inputs here and then click Send or press Enter
Lab 5 EGR 262 – Fundamental Circuits Lab Let’s repeat the last example using a multiple digit input: • Notes: • When 345 was entered on the keyboard, each digit was read separately, so 3 different integers were read. We could try to build the number 345 from these digits (for example, int N = 3*100 + 4*10 + 5), but this is a lot of work. • A better solution: Use the parseInt( ) function
Lab 5 EGR 262 – Fundamental Circuits Lab Let’s repeat the last example using a multiple digit input: • Note: • Much better. Serial.parseInt( ) allows us to correctly read an integer input from the keyboard.
Lab 5 EGR 262 – Fundamental Circuits Lab Now let’s return to our earlier topic of PWM. Recall that we wanted to enter the width of the pulse. We now know how to do it using parseInt( ). Example: Create a PWM signal where the value (from 0 to 255) can be entered by the user to set the duty cycle. If pin D11 was connected to an oscilloscope, we would see the waveform change every time a new value was entered.
Lab 5 EGR 262 – Fundamental Circuits Lab Digital to Analog Conversion: Note that a PWM signal has a digital input (0 to 255) and has an analog output (or DC value), so it is essentially a Digital to Analog Converter (DAC). However, the output has a large ripple voltage, VR, which is not suitable for some applications. Example: An illustration is shown below for the last program (with value = 64): Arduino UNO v(t) Digital Input = 6410 = 010000002 Analog Output = 1.255 VDC (with VR = 5V or 398% ) D11 5V VDC = 1.255 V t (ms) 0V 0 0.51 2.04 2.55 4.08 4.62
Lab 5 EGR 262 – Fundamental Circuits Lab Looking ahead to Lab 6 In Lab 6 we will add an RC circuit to a PWM output and will be able to reduce the ripple significantly (we will design it for 10% max ripple voltage). Lab 4 Arduino UNO Arduino UNO Lab 4: 3-bit R-2R DAC – only 8 analog values, but VR = 0 Lab 5: 6-bit DAC – 256 analog values, but VR = 5V (or hundreds of percent in some cases) Lab 6: 6-bit DAC – 256 analog values with VR = 10% max D11 D11 Arduino UNO D13 V0= analog output V0 = analog output (PWM) 2R R Lab 5 D12 2R R V0 = analog output (PWM) D11 R 2R C 2R Lab 6 t t