CEC 222 Digital Electronics Lab Spring 2015 Lab 10: Driving a Servo-Motor with PWM Lab Overview: DC servomotors are found in a wide variety of applications from home electronics (DVD player, …) to RC planes (control surfaces, throttle, …) and various robots. Effectively, the servomotor includes a DC motor and electronics to allow the device to control the angle of the motor’s shaft based on the pulse width of an input signal. In this lab, you will be generating the Pulse Width Modulated (PWM) signal required to drive a Tower Pro micro servo 9g model SG90 servomotor Pre-Lab (10%) Familiarize yourself with the datasheet of the servomotor which we will be using. The servomotor has three input terminals. Orange lead Red lead Brown lead => PWM input => Vcc (+5V) => GND We will be using the Analog Discovery to provide power (i.e., +5V) for our servomotor and our FPGA board to generate the PWM signal. The Pulse Width Modulated (PWM) signal, which we will be generating, must have a period of 20 ms (i.e., frequency of 1/20ms = 50 Hz). Unlike a typical square waveform wherein the signal is high for half of the period (and low for the other half) we seek to control the “Duty Cycle” (i.e., modulate the width of the pulse) of the waveform by varying the interval over which the signal is high (i.e., ton). Note that Period = ton + toff. Duty Cycle = ton / Period … ton toff … Period = 20 ms Lab 10: Driving a Servo-Motor with PWM Page 1 of 4 CEC 222 Digital Electronics Lab Spring 2015 To generate the PWM signal we can design a counter which “wraps around” every 20 ms. Based on the desired Duty Cycle we can drive an output high for part of the count and low for the remainder of the count. The BASYS2 board provides an onboard 50 MHz clock which can be used to drive our counter. Question 1. Using this 50MHz clock, how many clock cycles occur during a 1 ms interval? Question 2. How many cycles are there in 20 ms using the same clock? Note that your counter should “wrap around” after this many counts!! The SG90 servomotor will responds to varying duty cycles as follows: ton Duty Cycle 1.0 ms 1.5 ms 2.0 ms 1.0 / 20 = 5% 1.5 / 20 = 7.5% 2.0 / 20 = 10 % Motor Shaft Angle 0 deg 90 deg 180 deg Number of clock cycles ??? ??? ??? For this lab you will be entering a desired motor angle (in units of integer degrees) using the eight switches (SW7 … SW0) and then generate the appropriate PWM signal required to drive the servomotor to the desired motor shaft angle. Switches (SW7 … SW0) Desired Motor Shaft Angle VHDL code PWM Signal Servomotor One way to solve this problem is to determine the number of 50 MHz clock cycles (i.e., counts) corresponding to a desired motor angle (given in degrees). y=Mx+C where: y is in counts (i.e., the number of 50 MHz clock cycles), x is in degrees (desired angle), M is a scale factor in counts / degree, and C is the number of counts corresponding to a desired angle of 0 degrees. Question 3. What is the value of C in counts (i.e., number of 50 MHz clock cycles) and integer approximation of the coefficient M? Question 4. What is the equation that describes the number of counts required to achieve a desired angle? Use integer values for M and C. y = ??? x + ??? Lab 10: Driving a Servo-Motor with PWM Page 2 of 4 CEC 222 Digital Electronics Lab Spring 2015 Main Experiment (90%) Step 1.a: Build a Counter To generate the PWM signal you will first need to create a VHDL file to count upwards for the duration of the signal’s period (i.e., 20 ms) and then restart at the end of the period. You should now have a counter that wraps around every 20 ms. Step 1.b: Step 2: Modulate the Pulse Width Add a line of VHDL code which computes the count corresponding to a desired motor shaft angle (i.e., y = M x + C). To vary the duty cycle add a segment of code to your existing counter that will accept an 8 bit vector (the desired angle in binary from SW7 to SW0) as an input, and output a ‘1’ for the duration of the pulse width with a “0” for the remaining period of time. Count = 0 Desired angle in counts … HIGH … LOW Question 5. What is the Duty Cycle of the PWM for the following motor shaft angles: 0 degrees, 15 degrees, 30 degrees, and 90 degrees? Step 1.c: Power the servomotor using the Analog Discovery (i.e., 5V) and use your FPGA to drive it to desired shaft angles. Please recall the motor’s wiring convention, given above, as driving the PWM input with 5V will destroy the motor’s electronics!!! Lab 10: Driving a Servo-Motor with PWM Page 3 of 4 CEC 222 Digital Electronics Lab Spring 2015 Deliverables 1. Address all of the questions/tasks described in the pre-Lab (much of this should go into Section 2. Intro and/or Section 3. Theory within your lab report). 2. Formal Lab Report a. Show your commented VHDL code and explain how it works (place in the Appendix) b. Provide a screen capture of waveform for the following angles (place in Section 4. Results): i. 0 degrees, ii. 45 degrees, iii. 90 degrees, and iv. 135 degrees. c. Determine the Duty Cycle for each of the above (Hint: You can measure this with the Analog Discovery) d. Include photos of the Servo Motor at angles of (print the angle chart below and place your motor on top of it for each photos): i. 0 degrees, ii. 45 degrees, iii. 90 degrees, and iv. 160 degrees. 3. Extra-Credit: Display the motor’s desired shaft angle on the 7-segment displays Lab 10: Driving a Servo-Motor with PWM Page 4 of 4