Lab 3
Lab 3
Winter 2021
There are actually two waves and two signals being generated simultaneously. There is a phase shift
between the two that allows us to identify the direction that the motor is rotating. For this lab, we will
control the motor speed in the positive direction.
2 Lab Safety
This lab has potential safety concerns due to electric current and the rotating motor. When the power
adapter is connected, make sure to not touch any wires or circuit components as there is a risk of electric
shock. Before touching circuit components, make sure to disconnect the power adapter. If you must
leave the room, you can unplug the power adapter which eliminates most risks. Finally, be careful when
handling parts with sharp edges including the pins on integrated circuits.
page 1 of 6
Control Systems Winter 2021
high or low) no current will flow through the motor. When the first PWM signal is high and the second
PWM signal is low, current will flow in one direction and the motor will rotate. When the first PWM
signal is low and the second PWM signal is high, current will flow in the opposite direction and the motor
will rotate in the opposite direction. You can find more information and the datasheet for the motor
driver at the following link.
https://www.adafruit.com/product/807
Figure 1: The L293D motor driver can be connected in this way to control the current through the motor.
1. Build the circuit in Figure 1 with the motor driver, four 1N4007 diodes, motor (indicated with an
M in a circle), and the 9V power adapter.
2. Connect Vcc1 to the 5V supply on the Arduino. Connect the Arduino ground to any ground
connection on the motor driver.
3. Also connect pin 1 to the 5V source coming from the Arduino. This is an enable input that must
be high to enable the motor driver.
4. Connect pins 2 and 7 to the PWM signals coming from the Arduino (Arduino pins D2 through
D13).
5. The two motor supply pins (- and +) are indicated in Figure 2. These should be connected to pins
3 and 6 of the motor driver.
6. Make sure to connect all pins as indicated. There are four pins that must be grounded. Verify that
all grounds are connected together in the circuit.
page 2 of 6
Control Systems Winter 2021
7. Connect Vcc2 to the 9V supply from the power adapter using the barrel jack to bare wire adapter.
The ground from this source must be connected to one of the ground connections on the driver.
8. With this circuit, the maximum current through the motor will be near 1A. It is important to never
touch the circuit components or wires when the power adapter is connected.
9. Attach jumper wires to the motor encoder wires as extensions to the pins. The motor encoder
requires 5V and ground (from the Arduino). It provides two encoder signals (A and B) that can
be connected to the digital pins on the Arduino (pins D2 to D13). You can see the encoder pins
labeled in Figure 2.
Figure 2: This image shows the wiring for the motor and encoder.
2. Use Figure 2 to connect the +5V and ground from the Arduino to the encoder. Connect the encoder
signals (A and B) to two digital pins on the Arduino. In the example below this is D2 and D3.
page 3 of 6
Control Systems Winter 2021
4. You can test the encoder object by running readCount(encoder) and readSpeed(encoder).
Assuming the motor is not rotating, the speed will read zero RPMs.
5. Select two Arduino digital pins to be used as PWM pins to send signals to the motor driver. Use
the function configurePin() to set both pins to be a PWM signal.
6. Use the function writePWMDutyCycle() to set the PWM cycles for the two pins. Test setting one
pin high (1) and the other low (0) and note the direction of the motor rotation. Test the opposite
configuration and note that the motor rotates in the opposite direction.
7. For each motor direction, run readSpeed(encoder) to identify the direction of positive rotation.
It is recommended to use the positive rotational direction in this lab.
8. As a check on your work, with the max PWM signal supplied to the motor, you should see 80-120
RPMs for the motor.
1. Select three PWM duty cycles between 30% and 100%. For each case, you will run a test where
you will set one PWM signal to the chosen duty cycle and record the speed in RPMs in a vector for
about 10 seconds. Average this vector to get an approximate speed.
2. For each test, calculate the effective voltage applied to the motor.
3. For each test, calculate the gain KG as the average RPMs divided by the effective voltage.
4. These are three estimates of the system gain. You can average these to get a reasonable approxi-
mation of the system gain.
6. When finished, make sure to set both Arduino PWM signals to 0% duty cycle.
page 4 of 6
Control Systems Winter 2021
1. Design a proportional controller that achieves a closed loop time constant of less than 0.2 seconds.
2. In Simulink, create a block representing the motor as a single integrator with a gain KG that you
found previously.
3. Add a gain block for the proportional controller and set the calculated value.
4. Create a feedback loop with a step input, summing block, and scope to show the output.
5. Test the feedback loop with a step input with desired final value of 60 RPM to verify that closed
loop control is working as intended.
6. Add a saturation block between the controller and plant that limits the applied voltage to ±9V.
7. At this point, you may want to change the gain KP to improve performance. If you do change the
gain, document your reasoning for doing so.
8. Simulate the control system with a reference step input with initial value 0 RPM and final value 60
RPM. Plot both the reference input and system output on the same set of axes and include in your
report.
9. Simulate the control system with a square wave input that alternates between 40 RPM and 80 RPM.
Adjust the period to make sure the output settles for 2-5 seconds before switching. Run for three
full periods, i.e. three times at each setpoint. Plot both the reference input and system output on
the same set of axes and include in your report.
10. Test with a ramp input that increases the desired RPM from 30 to 90 linearly over about 20 seconds.
When the desired RPMs hits 90, you can stop the test. Plot both the reference input and system
output on the same set of axes and include in your report.
1. Implement your proportional controller in Matlab. Recall, this requires calculating the error in
RPMs and multiplying by the proportional gain used in simulation.
2. Convert the calculated voltage to a PWM duty cycle and set the Arduino PWM duty cycles appro-
priately. Include checks for out of range duty cycles to avoid errors.
3. Ensure all wires are connected correctly as you did earlier in this lab.
page 5 of 6
Control Systems Winter 2021
4. Run the control system with a step input with initial value 0 RPM and final value 60 RPM to
ensure that your controller works. If you would like to change your gain, you may, but you must
document the reasons for making the change. As a note, you may have some steady state error and
some output jitter due to noise. It is recommended to prioritize low noise jitter over low steady
state error. Whatever you decide, comment in your report.
5. Repeat the step input test and run for about 10 seconds. Collect the reference input and system
output, plot on the same set of axes, and include in your report.
6. Test with a square wave input that alternates between 40 RPM and 80 RPM. Adjust the timing to
allow the output to settle at each setpoint for 2-5 seconds before switching. Allow the simulation
to run for three full periods of the reference input. Collect the reference input and system output,
plot on the same set of axes, and include in your report.
7. Test with a ramp input that increases the desired RPM from 30 to 90 linearly over about 20 seconds.
Plot this data and include in your report.
8. Be sure to compare all test results to your simulation results. Use the language you are learning in
class including performance concepts, steady state error, sensitivity, noise, disturbance, etc.
8 Lab Report
This lab is the first part of a two part lab. You do not need to write and turn in a lab report for this lab.
Instead, you should document your observations and save your data. You will write a portion in your
next lab report that summarizes your work on this lab. You should especially focus on what you did for
system identification, control design, control simulation, and the physical control test. Make sure to save
all data, code, and figures that you generated in this lab. Your lab report will summarize what you did
for Lab 3 and Lab 4.
page 6 of 6